Lec9a BinaryTrees 18042023 090746am
Lec9a BinaryTrees 18042023 090746am
1
Introduction
2
A Tree
3
Examples: directory structure
4
Organizational organograms
5
Definition
6
ree Terminologies
Root
Level 1
Arc/Edge
Level 2
Level 3
Parent
Leaf Siblings
7
A
Tree Terminologies
B C
• Root
• It is the key node of a tree structure that does not have
parent. It is the first node in hierarchical arrangement.
• Node
• The node of a tree stores the data and its role is the
same as in the linked list. Nodes are connected by the
means of links with other nodes.
• Parent
• It is the immediate predecessor of a node. In the figure A
is the parent of B and C.
8
A
Tree Terminologies
B C
• Child
• All successor nodes are called child nodes. In the figure B
and C are the child nodes of A
• Sibling
• The child node of same parent are called sibling.
• Link / Edge
• An edge connects the two nodes. The line drawn from one
node to other node is called edge / link. Link is nothing but a
pointer to node in a tree structure.
• Leaf / Terminal Node
• This node is located at the end of the tree. It does not have
any child hence it is called leaf node.
9
Tree Terminologies
• Level
• Level is the rank of tree hierarchy. The whole tree structure is leveled. The level of
the root node is always at 0. the immediate children of root are at level 1 and their
children are at level 2 and so on.
• Depth
• The depth of a node n is the length of the path from the root to the node.
• The depth (or height) of a tree is the length of the path from the root to the
deepest node in the tree.
Root node
Leaf nodes
10
Binary Trees
root
O
M T
left subtree right subtree
of node O of node O
C E • P U
•
•
11
Binary Trees
If A is the root of a binary tree and B is the root of its left or right subtree,
then A is said to be parent of B, and B is said to be the left or right child
of A.
D E • F G
•
•
Left child of B right child of B
12
Binary Tree Examples
Binary Trees
76 99
26 85 26
21 50 80 99 21 76
26
50 85
76
80
85
99
13
Binary Trees
Level:
The root of the tree has level 0, and the level of any other node in the tree
is one more than the level of its father.
A
Level 0
B C Level 1
Level 2 D E F G
I J
14
Binary Trees
Depth:
B C
D E F G
Depth: 3 I J
15
Examples – familiarization with notation
16
Types of Binary Trees
17
Strictly Binary Tree
If every non-leaf node in a binary tree has nonempty left and right
subtrees, the tree is known as Strictly Binary Tree.
A A
B C B C
D E F G D E F G
H I J I J
B C
D E F G
19
Complete Binary Trees
B C
D E F G
20
Complete Binary Trees
B C
D E F G
. . . .
. . . .
. . . .
Total Number of nodes in a complete binary tree:
d+1
= 2 -1
21
Complete Binary Trees
d+1
N= 2 -1
d = log2(N + 1) -1
22
Binary Tree Implementation
23
Array-Based Implementation:
An array can be used to store some binary trees.
Number the nodes level by level, from left to right,
Max # nodes on level i:
0
2i
O
1 2 In array representation,
M T children of i are at:
2i + 1, 2i + 2
3 4 5 6
C E • P U Parent of i is at:
•
• (i - 1) / 2
24
E
But, unless each level of the tree
is full so there are no "dangling
C M
limbs," there can be much
wasted space in the array. U
left right
root
Left child Right child
75
60 80
60 80
58 65 92
58 65 92
26
C++ Implementation:
class Node
{
public:
int data;
Node *left, *right;
};
class BinaryTree
{
public:
Node *root; // pointer to root node
BinaryTree();
Node* insert(int);
//Node* search(int); //tree type dependent
//void delete_node(int); //tree type dependent
void traverse(); //pre, in, post order
};
27
BinaryTree::BinaryTree()
{
root=NULL;
}
Node* BinaryTree::insert(int val)
{
Node *p= new Node;
p->data=val;
p->left=NULL;
p->right=NULL;
return p;
}
//Driver.cpp 4
Void main()
{
BinaryTree b; 6 7
b->root = b.insert(4);
b->root->left = b.insert(6); 9
b->root->right = b.insert(7);
b->root->right->left = b.insert(9);
}
28
Implementing Tree
Traversals
29
BINARY TREE ADT
• Create
• Create an empty binary tree
• Empty
• Return true when binary tree is empty else return false.
• Tree Traversal
• Inorder Traversal
• Preorder Traversal
• Postorder Traversal
• Insert
• To insert a node
• Deletion
• To delete a node
• Search
• To search a given node
• Copy
• Copy one tree into another
30
Tree Traversal
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal
31
Traversal of a Binary Tree
32
Traversal of a Binary Tree
33
Tree Traversal
Preodrder:
34
Visit the root
Preorder Traversal Traverse the left subtree in preorder
Traverse the right subtree in preorder
32
13 95 16
Output: 32
Problem now reduced to traversal of two smaller binary trees.
35
Preorder Traversal
79
Visit root
Traverse left subtree
Traverse right subtree
13
Output: 32 79
36
Preorder Traversal
Visit root
13 Traverse left subtree
Traverse right subtree
Output: 32 79 13
37
Preorder Traversal
32
79 42
13 95 16
Output: 32 79 13
38
Preorder Traversal
42 Output: 32 79 13 42
Output: 32 79 13 42 95
Output: 32 79 13 42 95 16
95 16
95 16
39
Preorder Traversal
32
79 42
13 95 16
Output: 32 79 13 42 95 16
40
Preorder Traversal
32
79 42
13 95 16
32 79 13 42 95 16
41
Tree Traversal
Inorder:
42
Inorder Traversal
32
79 42
13 95 16
79 13 32 95 42 16
43
Tree Traversal
Postorder:
44
Postorder Traversal
79 42
13 95 16
13 79 95 16 42 32
45
Tree Traversal
Preorder:
Postorder:
Inorder:
46
Tree Traversal
A
Preorder: A B D G C E F H
Inorder: D G B A E C H F
B C
Postorder: G D B E H F C A
D
E F
G
H
47
Tree Traversal
15
16
5
3 20
12
23
10 13 18
6
Preorder:
Postorder:
Inorder:
left – root – right
49
Implementing Tree Traversals
}
50
Implementing Tree Traversals
51
Implementing Tree Traversals
52
Practice Tracing The Tree Using The Given Traversal Function
14 Traversal exercises:
finding output
15
4
3 18
9 14
20
7 9 16
5
17
4 5
14 4 3 3 9 7 5 4 4 5 5 5 7 9 9 9 4 15 14 14 18 16 17 17 16 20 20 18 15 14
void Traversal(Node* temp)
{
if(temp!=NULL)
{
cout<<temp->data;
Traversal(temp->left);
Traversal(temp->right);
cout<<temp->data;
}
}
53
Utility functions
75
• Counting the number of leaf nodes:
60 80
Int countleafnodes(Node *ptr)
{ 58 65 92
if(ptr==NULL)
return 0;
if((ptr->left==NULL)&&(ptr->right==NULL))
return 1;
else
return countleafnodes(ptr->left)+coutnleafnodes(ptr->right);
}
54