Introduction to Trees in Data
Structures
• • Trees are hierarchical data structures
consisting of nodes.
• • Root node is the topmost node; it has no
parent.
• • Nodes have parent-child relationships.
• • Key terms: Root, Parent, Child, Sibling, Leaf,
Path, Depth, Height.
• • Trees are used in file systems, databases,
and hierarchical data organization.
Binary Tree and Its Properties
• • A Binary Tree is a tree where each node has
at most two children.
• • Properties:
• - Maximum nodes at level i = 2^i.
• - Maximum nodes in a tree of height h =
2^(h+1) - 1.
• - Minimum height for n nodes = log2(n+1) .
⌈ ⌉
• - Perfect Binary Tree: All levels are
completely filled.
Types of Binary Trees
• • Full Binary Tree: Every node has 0 or 2
children.
• • Complete Binary Tree: All levels except the
last are filled.
• • Perfect Binary Tree: All internal nodes have 2
children, and all leaves are at the same level.
• • Degenerate Tree: Each parent has only one
child, resembling a linked list.
• • Balanced Binary Tree: Height difference
between left and right subtree is minimal.
Binary Tree Implementation
(Python)
• Algorithm:
• 1. Define a Node class with data, left, and right
pointers.
• 2. Initialize the root node.
• 3. Add methods for insertion and traversal.
• Code:
• class Node:
• def __init__(self, data):
Binary Tree Representation
• • Linked Representation:
• - Nodes are linked using pointers.
• - Efficient for dynamic memory allocation.
• • Array Representation:
• - Nodes are stored in an array sequentially.
• - Parent-Child relationships determined using
indices:
• * Parent = (i-1)/2
⌊ ⌋
Binary Tree Traversals
• • In-order Traversal (Left, Root, Right):
• - Used for retrieving sorted elements from
BST.
• • Pre-order Traversal (Root, Left, Right):
• - Used for creating a copy of the tree.
• • Post-order Traversal (Left, Right, Root):
• - Used for deleting the tree.
Traversals with Code (Python)
• Code:
• def inorder(root):
• if root:
• inorder(root.left)
• print(root.data, end=' ')
• inorder(root.right)
• def preorder(root):
• if root:

Complete_Binary_Trees_Presentation__.pptx

  • 1.
    Introduction to Treesin Data Structures • • Trees are hierarchical data structures consisting of nodes. • • Root node is the topmost node; it has no parent. • • Nodes have parent-child relationships. • • Key terms: Root, Parent, Child, Sibling, Leaf, Path, Depth, Height. • • Trees are used in file systems, databases, and hierarchical data organization.
  • 2.
    Binary Tree andIts Properties • • A Binary Tree is a tree where each node has at most two children. • • Properties: • - Maximum nodes at level i = 2^i. • - Maximum nodes in a tree of height h = 2^(h+1) - 1. • - Minimum height for n nodes = log2(n+1) . ⌈ ⌉ • - Perfect Binary Tree: All levels are completely filled.
  • 3.
    Types of BinaryTrees • • Full Binary Tree: Every node has 0 or 2 children. • • Complete Binary Tree: All levels except the last are filled. • • Perfect Binary Tree: All internal nodes have 2 children, and all leaves are at the same level. • • Degenerate Tree: Each parent has only one child, resembling a linked list. • • Balanced Binary Tree: Height difference between left and right subtree is minimal.
  • 4.
    Binary Tree Implementation (Python) •Algorithm: • 1. Define a Node class with data, left, and right pointers. • 2. Initialize the root node. • 3. Add methods for insertion and traversal. • Code: • class Node: • def __init__(self, data):
  • 5.
    Binary Tree Representation •• Linked Representation: • - Nodes are linked using pointers. • - Efficient for dynamic memory allocation. • • Array Representation: • - Nodes are stored in an array sequentially. • - Parent-Child relationships determined using indices: • * Parent = (i-1)/2 ⌊ ⌋
  • 6.
    Binary Tree Traversals •• In-order Traversal (Left, Root, Right): • - Used for retrieving sorted elements from BST. • • Pre-order Traversal (Root, Left, Right): • - Used for creating a copy of the tree. • • Post-order Traversal (Left, Right, Root): • - Used for deleting the tree.
  • 7.
    Traversals with Code(Python) • Code: • def inorder(root): • if root: • inorder(root.left) • print(root.data, end=' ') • inorder(root.right) • def preorder(root): • if root: