2. Binary Trees
Tree with no node having more than two children.
A finite set of nodes, one of which is designated as the root.
The root node may have at most two sub-trees, each of which
is also a binary tree.The two sub-trees of a given node are
ordered and we refer to them as left child and the right child.
Respectively.
3. Binary Trees
Nodes in a binary tree may
have zero, one or two
children.
The maximum number of
nodes at a given level/depth i
is
2i-1 for i ≥ 1
(Considering the level/depth of root
node as i = 1)
A
B C
D E F
G H
4. Binary Trees
The maximum number of
nodes for an entire
binary tree of depth k is.
2k-1 for k ≥ 1
A full binary tree of
depth k is a binary tree
with 2k-1 nodes.
That is the maximum
number of nodes a binary
tree can have.
A
B C
D E F
G H
Max. No of Node = 15
6. Binary Tree Implementation
ElementVariable will serve as our data field.
Left and Right points to the two sub-trees.
Value NULL indicates the absence of a sub-tree.
Root points at the root node of the tree.
Root == NULL indicates an empty tree.
7. Binary Tree Traversal
Visiting each node exactly once, is calledTraversal.
When positioned at any given node a traversal function
may
Continue down to left sub-tree or
Continue down to right sub-tree or
Or process the current node
Still leaves open the question of when we should process
the data item.
8. Binary Tree Traversal
To process the data item we have the following options.
Visit the node before moving down the left sub-tree.
(Preorder)
Visit the node after traversing the left sub-tree but before
traversing the right sub-tree. (Inorder)
Visit the after traversing both sub-trees. (PostOrder)
9. Binary Tree Traversal
InorderTraversal
1. Move down the tree as far left as possible
2. Visit the current node
3. Backup one node in the tree and visit it.
4. Move down the right sub-tree of the node visited in step 3.
5. Repeat the step 1 to 5 until all nodes have been processed.
65. Expression Trees
Is a BinaryTree.
Leaves of an expression tree are operand, such as
constants or variable names.
Other nodes contain the operators.
66. Expression Trees
We can evaluate and expression tree by applying the
operator at the root to the values obtained by recursively
evaluating the left and right sub-trees.
(a + b * c) +((d * e + f) *g)
*
b c
a
+
d e
*
+
f
*
g
+
74. Expression Trees
Postfix Expression to ExpressionTree
a b + c d e + * *
a
b
a, b are operands push pointers to
their nodes in the stack
75. Expression Trees
Postfix Expression to ExpressionTree
a b + c d e + * *
a b
+ operator , pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.
+
76. Expression Trees
Postfix Expression to ExpressionTree
a b + c d e + * *
a b
push pointers to the nodes of the
operands c, d & e
+
c
d
e
77. Expression Trees
Postfix Expression to ExpressionTree
a b + c d e + * *
a b
+ operator , pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.
+
c
d e
+
78. Expression Trees
Postfix Expression to ExpressionTree
a b + c d e + * *
a b
* operator , pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.
+
c
d e
+
*
79. Expression Trees
Postfix Expression to ExpressionTree
a b + c d e + * *
a b
* operator again, pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.
+
c
d e
+
*
*
80. Binary Search Trees
A binary tree , used for searching.
Each node in the tree is assigned a key value.
Keys are assumed as distinct.
81. Binary Search Trees
For every node X in
the tree
Left Sub-tree key
values < X < Right
Sub-tree key values.
4
3 5
1
2
7 9
8
10
11
12
13
6
84. Binary Search Tree Implementation
FindValue
Requires returning a pointer to the node in treeT that has the
key value.
IfT is Null return NULL, if the key stored at T is X, we can
returnT. Otherwise depending on the less than and greater
than relationship we can traverse the tree recursively either on
the left sub tree or the right subtree.
86. Binary Search Tree Implementation
Find MinimumValue
Get us the minimum or smallest key value in the tree.
For Find Minimum, start at the root and go left as long as there
is a left child.
87. Binary Tree Implementation
Position FindMin(SearchTree T)
{
if (T == NULL)
{
return NULL;
}else
if (TLeft == NULL)
{
return T;
}else
Return FindMin(TLeft);
}
88. Binary Search Tree Implementation
Find MaximumValue
Get us the maximum or largest key value in the tree.
For Find Maximum start at the root and go right as long as
there is a right child.
89. Binary Tree Implementation
Position FindMax(SearchTree T)
{
if (T == NULL)
{
return NULL;
}else
if (TRight == NULL)
{
return T;
}else
Return FindMin(TRight);
}
90. Binary Search Tree Implementation
Insert (Duplication Not Allowed)
Proceed down the tree in similar fashion as we did in Find
Function.
Insert X at the last spot on the path traversed.
92. Binary Search Tree Implementation
Delete
If the node is a leaf , it can be deleted immediately.
If the node has one child, the node can be deleted after its
parent adjust a pointer to bypass the node.
If the node has two children, replace the data of this node with
the smallest data of the right sub-tree and recursively delete
that node.
99. Binary Search Tree Implementation
SearchTree Delete(ElementType X, SearchTree T)
{
Position TempCell
if (T == NULL)
{
Error(“Element Not Found”);
}else
if (X < TElement)
{
TLeft = Delete(X,TLeft);
} else
if (X > TElement)
{
TRight = Delete(X,TRight);
} else
// contd. On next page
}
100. Binary Search Tree Implementation
SearchTree Delete(ElementType X, SearchTree T)
{….
If (TLeft && TRight) // left and right child //
{
TmpCell = FindMin(TRight);
TElement = TmpCellElement;
TRight = Delete(TElement, TRight);
}
Else
{
TmpCell = T;
If (TLeft == NULL) // leaves , no children //
{
T = TRight;
}else if (TRight == NULL)
{
T = TLeft;
}
Free(TmpCell);
}
return T;
}
101. AVL Trees
Binary search tree with a balance condition.
The balance condition must be easy to maintain and it
ensures that the dept of the tree is O(log n).
Idea is to acquire same height on the left and right sub-
trees.
Prevent a worst case running time of O(n). i.e. a left-iest
or right-iest tree.A tree structure looking like a linear list.
102. AVL Trees
Balanced Condition
left height(n) = 0 if n has no left child.
= 1 + height(left child(n)) for all other nodes.
right height(n) = 0 if n has no right child.
= 1 + height(right child(n)) for all other nodes.
Balance(n) = right height(n) – left height(n)
103. AVL Trees
Balance condition indicates the relative height of its right
sub-tree compares to its left.
If the balance is positive the right sub-tree has greater
depth than the left sub-tree.
If the balance is negative the left sub-tree has greater
depth than the right sub-tree.
A binary tree is an AVL tree if and only if every node in
the tree has a balance of -1 , 0 or +1.
104. AVL Trees
x
2
5
0 0
1
0 0 0 0
1 1
2
51
0 0 1 0
1 2
4
0 0
balance = right height – left height
= 0 - 0 (AVL Tree)
balance = right height – left height
= 2 – 1 = 1 (AVL Tree)
balance = right height – left height
= 1 – 1 = 0 (AVL Tree)
106. AVL Trees
Height of the two child sub-trees of any node differ by at
most one; therefore it is also said to be height-balanced.
Searching, Insert and Delete all take O(log n) in both
average and worst cases.Where n is the number of nodes
in the tree prior to the operation.
Insertions and deletions may require the tree to be
rebalanced by one or more tree rotation.
Node with balance factor 1,0 or -1 is considered
balanced.
Node with any other balance factor is called unbalance
and need to be rotated or rebalanced.
124. AVL Tree Insertion
Trace the path from the root node to a leaf node.
Insert the new node.
Retrace the path back up to the root, adjusting balances
along the way.
If a nodes balance is out of the bound condition i.e.
-1 > balance >1. rotate and rebalance.
125. AVL Tree Insertion
Rotation
Singly Rotation
It preserves the Ordered property of the tree.
It restores all nodes to appropriate AVL balance.
Preserves the Inorder traversal of the tree i.e. Inorder traversal will
access nodes in the same order after transformation.
Only modify three pointers to accomplish the rebalancing.
126. AVL Trees Implementation
Singly Rotation ( Left and Right )
Right Rotation of Root Q results in Q stepping down a
hierarchy. P becomes Root with Q as right child and right child
of P becomes left child of Q.