Professional Documents
Culture Documents
T
.
Sohail Aslam Javed Aslam Yasmeen Aslam
D E F
G H I
•The left sub-tree of root A consists of the nodes B,D,E, and G, and
the right sub-tree of A consists of the node C,F,H, and I.
A non-tree Structure
A
B C
D E F
G H I
• In this figure we join the node G with D. Now this is not a tree. It is due to
the reason that in a tree there is always one path to go (from root) to a
node. But in this figure, there are two paths (tracks) to go to node G.
A non-tree Structure
A
B C
D E F
H I
G
A
B C
F
D E
H I
G
Strictly Binary Tree
B C
D E J F
G K H I
• Now all the non-leaf nodes (A, B, C, E and F) have their left and right children so
according to the definition, it is a strictly binary tree.
Some Terminologies
Father/Parent A
B C
D E F
Leaves/Terminal
Child/Son
G H I
Level
The level of a node in a binary tree is defined as follows:
Root has level 0,
Level of any other node is one more than the level of its parent (father).
Level of nodes of a tree
A 0 ----------------------------------------Level 0
B 1 C 1 ------------------- Level 1
D E 2 F 2 ------- Level 2
H 3 I 3-----Level 3
G 3
Complete Binary Tree
• Now, if there are left and right sub-trees for each node in the binary tree, it
becomes a complete binary tree.
• The definition of the complete binary tree is
“A complete binary tree of depth d is the strictly binary tree all of whose
leaves are at level d”.
A
B C
D E F G
H I J K L M N O
Complete Binary Tree
E Level 2: 22 nodes
F ------------- G
D -------------
H I M N O
J K L
The total number of nodes in a complete binary tree of depth d will be 2d+1 –
1.
If there is a complete binary tree of depth 4, the total number of nodes in it
will be?
24+1 - 1 = 25 – 1 = 32 – 1 = 31
Thus the total number of nodes in the complete binary tree of depth 4 is 31.
In a complete binary tree, all the leaf nodes are at the depth level d. So the
number of nodes at level d will be 2d . These are the leaf nodes.
Thus the difference of total number of nodes and number of leaf nodes will
give us the number of non-leaf (inner) nodes. It will be (2 d+1 – 1) – 2d i.e. 2d –
1.
Thus we conclude that in a complete binary tree, there are 2d leaf nodes and
2d – 1 non-leaf (inner) nodes.
Level of a Complete Binary Tree
• We can find the depth of a complete binary tree if we know the total
number of nodes.
• If we have a complete binary tree with n total nodes, then by the equation
of the total number of nodes we can write
• Total number of nodes = 2d+1 – 1 = n
• To find the value of d, we solve the above equation as under
2d+1 – 1 = n
2d+1 = n + 1
d + 1 = log2 (n + 1)
d = log2 (n + 1) – 1
• After having n total nodes, we can find the depth d of the tree by the above
equation. Suppose we have 100,000 nodes. It means that the value of n is
100,000, reflecting a depth i.e. d of the tree will be log2 (100000 + 1) – 1,
which evaluates to 20. So the depth of the tree will be 20. In other words,
the tree will be 20 levels deep.
Example: Expression Trees
• (a + b*c)+((d*e + f )*g)
• (5* ( 6 + 2)) – 12/4
• (2x+y)(5a-b)3
• (A* B-C)- ((D/E ^ F) * G)
Representing Binary Tree in
Memory
• Linked representation of binary tree
• Root: Contains the location of the root R of the tree
• Info: Contains the data at the node.
• Left: Contains the location of the left child of the node
• Right: Contains the location of the right child of the node
• Pre-order traversal
• node, left, right
• prefix expression
• ++a*bc*+*defg
• In-order traversal
• left, node, right.
• infix expression
• a+b*c+d*e+f*g
• Post-order traversal
• left, right, node
• postfix expression
• abc*+de*f+g*+
Breadth-First Traversal
• The breadth-first traversal of a tree visits the nodes in the order of their
depth in the tree. Breadth-first traversal first visits all the nodes at depth
zero (i.e., the root), then all the nodes at depth one, and so on. At each
depth the nodes are visited from left to right.
• A breadth-first traversal of the tree shown in Figure visits the nodes in the
following order:
• A,B,D,C,E,H,F,G,I
Question
• Derive Pre-order, Post-order and In-order expression
Algorithm
void preorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
cout << *(treeNode->getInfo())<<" ";
preorder(treeNode->getLeft());
preorder(treeNode->getRight());
}
}
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
Tree implementation
void Preorder(struct node* node)
{
if (node == NULL)
return;
cout<< node->data<<" ";
Preorder(node->left);
Preorder(node->right);
}
• In order and Post order Traversals do it by yourself
Applications of Binary Tree
Binary tree is useful structure when two-way decisions are made at each
point. Suppose we want to find all duplicates in a list of the following
numbers:
This list may comprise numbers of any nature. For example, roll numbers,
telephone numbers or voter’s list. In addition to the presence of duplicate
number, we may also require the frequency of numbers in the list. As it is a
small list, so only a cursory view may reveal that there are some duplicate
numbers present in this list. Practically, this list can be of very huge size
ranging to thousands or millions.
Binary Search Trees
• Stores keys in the nodes in a way so that searching, insertion and
deletion can be done efficiently.
Binary search tree property
• For every node X, all the keys in its left subtree are smaller than the key value
in X, and all the keys in its right subtree are larger than the key value in X
Binary Search Trees
15 14
14
15
4 15
15
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
A new node is created and number 4 put into it The node is added as the left child of the root node
14
4 15
4 15
3 9 18
7 16 20
5 17
30 30
Searching for Duplicates
One way of finding duplicates is to compare each number with all those
that precede it.
So, the solution lies in reducing the number of comparisons. The number of
comparisons can be drastically reduced with the help of a binary tree. The
benefits of linked list are there, also the tree grows dynamically like the
linked list.
Deletion in Binary Search Tree
• When we delete a node, we need to consider how we take care of
the children of the deleted node.
• This has to be done such that the property of the search tree is
maintained.
Deletion in Binary Search Tree
Three cases:
(1) the node is a leaf
• Delete it immediately
2 8
1 4
3
Deletion in Binary Search Tree
(2) the node has one child
• Adjust a pointer from the parent to bypass that node and connect to inorder
successor.
6 6 6
2 8 2 8 2 8
1 4 1 4 1 3
3 3
Deletion in steps
Deletion in Binary Search Tree
(3) the node has 2 children
• The strategy s to replace the data of this node containing the smallest data of
the right sub-tree and recursively delete that node.
• delete the minimum element
• Has either no child or only right child because if it has a left child, that left
child would be smaller and would have been chosen. So invoke case 1 or 2.
6
6 6 6 6
3 8
2 8 3 8 3 8 3 8
1 5
1 5 1 5 1 5 1 5
3
3 3 3
4
4
4 4 4
Inorder
successor
• else {
• if (root->left == NULL)
• {
• struct node *temp = root->right;
• free(root);
• return temp;
• }
• Implement for right same as left
Node with two children: