CS221A – Data Structures and
Algorithms
Binary Trees
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.
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
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
Binary Tree Implementation
 typedef struct TreeNode *PtrToNode;
 typedef struct *PtrToNode Tree;
 Typedef struct *PtrToNode Root;
 struct TreeNode
 {
 ElementType Element;
 Tree Left;
 Tree Right;
 }
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.
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.
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)
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.
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8,
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4,
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4, 9,
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4, 9, 2,
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4, 9, 2, 10,
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4, 9, 2, 10, 5,
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4, 9, 2, 10, 5, 1,
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4, 9, 2, 10, 5, 1, 6,
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4, 9, 2, 10, 5, 1, 6, 3,
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4, 9, 2, 10, 5, 1, 6, 3, 7
Binary Tree Traversal
 InorderTraversal
1
2 3
4 5 6
8 9
7
10
Result : 8, 4, 9, 2, 10, 5, 1, 6, 3, 7
Binary Tree Traversal
 void InOrder(TreeNode Root)
 {
 if (Root !=null)
 {
 InOrder(RootLeft);
 PrintNode(RootElement);
 InOrder(RootRight);
 }
 }
Binary Tree Traversal
 PreorderTraversal
 Visit the data item.
 Visit the left sub tree.
 Visit the right sub tree.
Binary Tree Traversal
 PreorderTraversal
1
2 3
4 5 6
8 9
7
10
Binary Tree Traversal
Result : 1,
 PreorderTraversal
2 3
4 5 6
8 9
7
10
Binary Tree Traversal
Result : 1, 2,
 PreorderTraversal
3
4 5 6
8 9
7
10
Binary Tree Traversal
Result : 1, 2, 4,
 PreorderTraversal
3
5 6
8 9
7
10
Binary Tree Traversal
Result : 1, 2, 4, 8,
 PreorderTraversal
3
5 6
9
7
10
Binary Tree Traversal
Result : 1, 2, 4, 8, 9,
 PreorderTraversal
3
5 6 7
10
Binary Tree Traversal
Result : 1, 2, 4, 8, 9, 5,
 PreorderTraversal
3
6 7
10
Binary Tree Traversal
Result : 1, 2, 4, 8, 9, 5, 10,
 PreorderTraversal
3
6 7
Binary Tree Traversal
Result : 1, 2, 4, 8, 9, 5, 10, 3,
 PreorderTraversal
6 7
Binary Tree Traversal
Result : 1, 2, 4, 8, 9, 5, 10, 3, 6
 PreorderTraversal
7
Binary Tree Traversal
Result : 1, 2, 4, 8, 9, 5, 10, 3, 6, 7
 PreorderTraversal
Binary Tree Traversal
Result : 1, 2, 4, 8, 9, 5, 10, 3, 6, 7
 PreorderTraversal
Binary Tree Traversal
 void PreOrder(TreeNode Root)
 {
 if (Root !=null)
 {
 PrintNode(RootElement);
 PreOrder(RootLeft);
 PreOrder(RootRight);
 }
 }
Binary Tree Traversal
 PostorderTraversal
 Visit the left sub tree.
 Visit the right sub tree.
 Visit the data item.
Binary Tree Traversal
 PostorderTraversal
1
2 3
4 5 6
8 9
7
10
Binary Tree Traversal
Result : 8,
 PostorderTraversal
2 3
4 5 6
9
7
10
1
Binary Tree Traversal
Result : 8, 9
 PostorderTraversal
2 3
4 5 6 7
10
1
Binary Tree Traversal
Result : 8, 9, 4,
 PostorderTraversal
2 3
5 6 7
10
1
Binary Tree Traversal
Result : 8, 9, 4, 10,
 PostorderTraversal
2 3
5 6 7
1
Binary Tree Traversal
Result : 8, 9, 4, 10, 5,
 PostorderTraversal
2 3
6 7
1
Binary Tree Traversal
Result : 8, 9, 4, 10, 5, 2,
 PostorderTraversal
3
6 7
1
Binary Tree Traversal
Result : 8, 9, 4, 10, 5, 2, 6,
 PostorderTraversal
3
7
1
Binary Tree Traversal
Result : 8, 9, 4, 10, 5, 2, 6, 7
 PostorderTraversal
3
1
Binary Tree Traversal
Result : 8, 9, 4, 10, 5, 2, 6, 7, 3
 PostorderTraversal
1
Binary Tree Traversal
Result : 8, 9, 4, 10, 5, 2, 6, 7, 3, 1
 PostorderTraversal
Binary Tree Traversal
Result : 8, 9, 4, 10, 5, 2, 6, 7, 3, 1
 PostorderTraversal
Binary Tree Traversal
 void PostOrder(TreeNode Root)
 {
 if (Root !=null)
 {
 PostOrder(RootLeft);
 PostOrder(RootRight);
 PrintNode(RootElement);
 }
 }
Binary Tree Traversal
 Breadth FirstTraversal
 Process nodes by level
 Left to right within the level
Binary Tree Traversal
 Breadth First Traversal
1
2 3
4 5 6
8 9
7
10
Binary Tree Traversal
Result : 1,
 Breadth First Traversal
2 3
4 5 6
9
7
108
Binary Tree Traversal
Result : 1, 2,
 Breadth First Traversal
3
4 5 6
9
7
108
Binary Tree Traversal
Result : 1, 2, 3,
 Breadth First Traversal
4 5 6
9
7
108
Binary Tree Traversal
Result : 1, 2, 3, 4,
 Breadth First Traversal
5 6
9
7
108
Binary Tree Traversal
Result : 1, 2, 3, 4, 5
 Breadth First Traversal
6
9
7
108
Binary Tree Traversal
Result : 1, 2, 3, 4, 5, 6,
 Breadth First Traversal
9
7
108
Binary Tree Traversal
Result : 1, 2, 3, 4, 5, 6, 7,
 Breadth First Traversal
9 108
Binary Tree Traversal
Result : 1, 2, 3, 4, 5, 6, 7, 8,
 Breadth First Traversal
9 10
Binary Tree Traversal
Result : 1, 2, 3, 4, 5, 6, 7, 8, 9,
 Breadth First Traversal
10
Binary Tree Traversal
Result : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
 Breadth First Traversal
Binary Tree Traversal
Result : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
 Breadth FirstTraversal
Binary Tree Traversal
 void BreadthFirst(TreeNode Root)
 {
 // Solve It
 }
Expression Trees
 Is a BinaryTree.
 Leaves of an expression tree are operand, such as
constants or variable names.
 Other nodes contain the operators.
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
+
Expression Trees
 InorderTraversal (left, Root, Right)
*
b c
a
+
d e
*
+
f
*
g
+
Expression Trees
 InorderTraversal (left, Root, Right)
 a+ b * c + d * e + f * g (infix notation)
*
b c
a
+
d e
*
+
f
*
g
+
Expression Trees
 PreorderTraversal (Root, Left, Right)
*
b c
a
+
d e
*
+
f
*
g
+
Expression Trees
 PreorderTraversal (Root, Left, Right)
 + + a * b c * + * d e f g (Prefix Notation)
*
b c
a
+
d e
*
+
f
*
g
+
Expression Trees
 PostorderTraversal (Left, Right, Root)
*
b c
a
+
d e
*
+
f
*
g
+
Expression Trees
 PostorderTraversal (Left, Right, Root)
 a b c * + d e * f + g * + (Postfix Notation)
*
b c
a
+
d e
*
+
f
*
g
+
Expression Trees
 Postfix Expression to ExpressionTree
 a b + c d e + * *
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
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.
+
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
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
+
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
+
*
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
+
*
*
Binary Search Trees
 A binary tree , used for searching.
 Each node in the tree is assigned a key value.
 Keys are assumed as distinct.
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
Binary Search Trees Implementation
 typedef struct TreeNode *PtrToNode;
 typedef struct *PtrToNode SearchTree;
 typedef struct *PtrToNode Position;
 struct TreeNode
 {
 ElementType Element;
 TreeNode Left;
 TreeNode Right;
 }
Binary Search Tree Implementation
 SearchTree MakeEmpty(SearchTree T)
 {
 if (T != NULL)
 {
 MakeEmpty(TLeft);
 MakeEmpty(TRight);
 free(T);
 }
 return NULL;
 }
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.
Binary Tree Implementation
 Position Find(ElementType X, SearchTree T)
 {
 if (T == NULL)
 {
 return NULL;
 }
 if (X < TElement)
 {
 return Find(X, TLeft);
 }else
 if (X > TElement)
 {
 return Find(X, TRight);
 }
 else
 return T;
 }
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.
Binary Tree Implementation
 Position FindMin(SearchTree T)
 {
 if (T == NULL)
 {
 return NULL;
 }else
 if (TLeft == NULL)
 {
 return T;
 }else
 Return FindMin(TLeft);
 }
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.
Binary Tree Implementation
 Position FindMax(SearchTree T)
 {
 if (T == NULL)
 {
 return NULL;
 }else
 if (TRight == NULL)
 {
 return T;
 }else
 Return FindMin(TRight);
 }
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.
Binary Search Tree Implementation
 SearchTree Insert(ElementType X, SearchTree T)
 {
 if (T == NULL)
 {
 T = malloc(sizeof(struct TreeNode));
 TElement = X;
 TLeft = TRight = NULL;
 }else
 if (X < TElement)
 {
 TLeft = Insert(X,TLeft);
 } else
 if (X > TElement)
 {
 TRight = Insert(X,TRight);
 } else
 return T;
 }
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.
Binary Search Tree Implementation
 Delete
R
S
T U
Node to be deleted
Binary Search Tree Implementation
 Delete
R
S
T U
Node to be deleted
R
T
U
Before Deletion
After Deletion
Binary Search Tree Implementation
 Delete
6
2
1 4
Node to be deleted
3
8
Binary Search Tree Implementation
 Delete
6
2
1 4
Node to be deleted
3
8
6
2
1 4
3
8
6
2
1 4
3
8
Binary Search Tree Implementation
 Delete
6
2
1 5
Node to be deleted
3
8
4
Binary Search Tree Implementation
 Delete
6
2
1 5
Node to be deleted
3
8
4
6
2
1 5
3
8
4
6
2
1 5
3
8
4
Binary Search Tree Implementation
 SearchTree Delete(ElementType X, SearchTree T)
 {
 Position TempCell
 if (T == NULL)
 {
 Error(“Element Not Found”);
 }else
 if (X < TElement)
 {
 TLeft = Delete(X,TLeft);
 } else
 if (X > TElement)
 {
 TRight = Delete(X,TRight);
 } else
// contd. On next page
 }
Binary Search Tree Implementation
 SearchTree Delete(ElementType X, SearchTree T)
 {….
 If (TLeft && TRight) // left and right child //
 {
 TmpCell = FindMin(TRight);
 TElement = TmpCellElement;
 TRight = Delete(TElement, TRight);
 }
 Else
 {
 TmpCell = T;
 If (TLeft == NULL) // leaves , no children //
 {
 T = TRight;
 }else if (TRight == NULL)
 {
 T = TLeft;
}
 Free(TmpCell);
 }
 return T;
 }
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.
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)
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.
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)
AVL Trees
3
51
0 1 0 0
2 1 5
1
8
0 0
balance = right height – left height
= 1 – 3 = -2 (Non-AVL Tree)
balance = right height – left height
= 1 – 2 = -1 (AVL Tree)
2
41
0 0
1 2
3
0 0
2
0 0
1 0
3
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.
AVL Trees
7
6
5
8
balance = right height – left height
= ? – ? = ?
2
41
3
AVL Trees
7
6
5
8 balance = right height – left height
= 0 – 2 = -2 ( Non-AVL)
2
41
3
AVL Trees
7
6
5
8
balance = right height – left height
= 0 – 2 = -2 ( Non-AVL)
2
41
3
AVL Trees
7
6
5
8
balance = right height – left height
= 2 – 3 = -1 (AVL Tree)
2
41
3
AVL Trees
3
2
4
balance = right height – left height
= ? – ? = ?
1
5
AVL Trees
3
2
4
balance = right height – left height
= 2 – 0 = 2 (Non- AVL)1
5
AVL Trees
3
2
4
balance = right height – left height
= 2 – 0 = 2 (Non- AVL)1
5
AVL Trees
3
2
4
balance = right height – left height
= 2 – 1 = 1 (AVL Tree)
1
5
AVL Trees
3
2
4
balance = right height – left height
= ? – ? = ?
1
5
6
AVL Trees
3
2
4
balance = right height – left height
= 3 – 1 = 2 (Non AVL)
1
5
6
AVL Trees
3
2
4
balance = right height – left height
= 3 – 1 = 2 (Non AVL)
1
5
6
AVL Trees
3
2
4
balance = right height – left height
= 2 – 2 = 0 (AVL Tree)
1
5
6
AVL Trees
3
2
4
balance = right height – left height
= ? - ? = ?
1
5
6
7
AVL Trees
3
2
4
balance = right height – left height
= 2 - 0 = 2 (Non AVL)
1
5
6
7
AVL Trees
3
2
4
balance = right height – left height
= 2 - 0 = 2 (Non AVL)
1
5
6
7
AVL Trees
3
2
4
balance = right height – left height
= 2 - 2 = 0 (AVL Tree)
1
5
6
7
AVL Tree Implementation
 typedef struct AvlNode *Position;
 typedef struct AvlNode *AvlTree;
 struct AvlNode
 {
 ElementType Element;
 Avltree Left;
 Avltree Right;
 int Height;
 }
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.
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.
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.
AVL Tree Rotation
k2
X
Y
Z
k1
AVL Tree Rotation
k2
X
Y
Z
k1
k2
X
Y Z
k1
AVL Tree Rotation
k2
X Y
Z
k1
AVL Tree Rotation
k2
X Y
Z
k1
k2
X Y
Z
k1
AVL Tree Implementation
 Position LeftRotation(Position K2)
 {
 Position K1 = K2 Right;
 K2Right = K1Left;
 K1Left = K2;
 K2Height = Max(Height(K2Right),
Height(K2Left))+1;
 K1Height = Max(Height(K1Right), K2Height) + 1;
 return K1;
 }
AVL Tree Implementation
 Position RightRotation(Position K2)
 {
 Position K1 = K2 Left;
 K2Left = K1Right;
 K1Right = K2;
 K2Height = Max(Height(K2Left),
Height(K2Right))+1;
 K1Height = Max(Height(K1Left), K2Height) + 1;
 return K1;
 }
AVL Tree Insertion
 AvlTree Insert(ElementType X, AvlTree T)
 {
 Condition 1: T == Null
 Condition 2: X < TElement
 Condition 3: X > TElement
 }
AVL Tree Insertion
 AvlTree Insert(ElementType X, AvlTree T)
 { // T== Null
 If (T == Null)
 {
 T = malloc(sizeof(struct AvlNode));
 TElement = X;
 THeight = 0;
 TLeft = Null;
 TRight == Null;
 }
 .. cont
 }
AVL Tree Insertion
 AvlTree Insert(ElementType X, AvlTree T)
 { // X < TElement
 If (X < TElement)
 {
 TLeft = Insert(X,TLeft);
 if (Height(TLeft) – Height(TRight) == 2)
 {
 T = RightRotation(T);
 }
 }
 .. cont
 }
AVL Tree Insertion
 AvlTree Insert(ElementType X, AvlTree T)
 { // X > TElement
 If (X > TElement)
 {
 TRight = Insert(X,TRight);
 if (Height(TRight) – Height(TLeft) == 2)
 {
 T = LeftRotation(T);
 }
 }
 THeight = Max(Height(TLeft),Height(TRight))+1;
 return T;
 }
AVL Tree Insertion
 AvlTree Insert(ElementType X, AvlTree T)
 { // X > TElement
 If (X > TElement)
 {
 TRight = Insert(X,TRight);
 if (Height(TRight) – Height(TLeft) == 2)
 {
 T = LeftRotation(T);
 }
 }
 THeight = Max(Height(TLeft),Height(TRight))+1;
 return T;
 }
AVL Tree Implementation
 Position LeftRightRotation(Position K3)
 {
 K3Left = LeftRotation(K3Left)
 return RightRotation(K3);
 }
 Position RightLeftRotation(Position K3)
 {
 K3Right = RightRotation(K3Right)
 return LeftRotation(K3);
 }
AVL Trees
3
2
4
balance = right height – left height
= 2 - 2 = 0 (AVL Tree)
1
5
6
7
AVL Trees
3
2
4
1
5
6
7
16
AVL Trees
3
2
4
1
5
6
7
16
15
AVL Trees
3
2
4
1
5
6
7
16
15
AVL Trees
3
2
4
1
5
6
7
16
15
AVL Trees
3
2
4
1
5
6
7 16
15
AVL Trees
3
2
4
1
5
6
7 16
15
14
AVL Trees
3
2
4
1
5
6
7 16
15
14
AVL Trees
3
2
4
1
5
6
7
16
15
14
AVL Trees
3
2
4
1
5
6
7
16
15
14
Insert 13
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
Insert 12
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
12
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
12
Insert 11
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
12
11
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
12
11
Insert 10
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
12
11
10
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
12
11
10
Insert 8
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
12
11
10
8Insert 9
AVL Trees
3
2
4
1
5
6
7
16
15
14
13
12
11
10
8
9
AVL Tree Insertion
 AvlTree Insert(ElementType X, AvlTree T)
 { // X < TElement
 If (X < TElement)
 {
 TLeft = Insert(X,TLeft);
 if (Height(TLeft) – Height(TRight) == 2)
 {
 If (x<TLeftElement)
 T = RightRotation(T);
Else
T = RightLeftRotation(T);
 }
 }
 .. cont
 }
AVL Tree Insertion
 AvlTree Insert(ElementType X, AvlTree T)
 { // X > TElement
 If (X > TElement)
 {
 TRight = Insert(X,TRight);
 if (Height(TRight) – Height(TLeft) == 2)
 {
 If (x>TRightElement)
 T = LeftRotation(T);
Else
T = LeftRightRotation(T);
 }
 }
 THeight = Max(Height(TLeft),Height(TRight))+1;
 return T;
 }

Binary Trees

  • 1.
    CS221A – DataStructures and Algorithms Binary Trees
  • 2.
    Binary Trees  Treewith 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  Nodesin 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  Themaximum 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
  • 5.
    Binary Tree Implementation typedef struct TreeNode *PtrToNode;  typedef struct *PtrToNode Tree;  Typedef struct *PtrToNode Root;  struct TreeNode  {  ElementType Element;  Tree Left;  Tree Right;  }
  • 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.
  • 10.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10
  • 11.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8,
  • 12.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4,
  • 13.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4, 9,
  • 14.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4, 9, 2,
  • 15.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4, 9, 2, 10,
  • 16.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4, 9, 2, 10, 5,
  • 17.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4, 9, 2, 10, 5, 1,
  • 18.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4, 9, 2, 10, 5, 1, 6,
  • 19.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4, 9, 2, 10, 5, 1, 6, 3,
  • 20.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4, 9, 2, 10, 5, 1, 6, 3, 7
  • 21.
    Binary Tree Traversal InorderTraversal 1 2 3 4 5 6 8 9 7 10 Result : 8, 4, 9, 2, 10, 5, 1, 6, 3, 7
  • 22.
    Binary Tree Traversal void InOrder(TreeNode Root)  {  if (Root !=null)  {  InOrder(RootLeft);  PrintNode(RootElement);  InOrder(RootRight);  }  }
  • 23.
    Binary Tree Traversal PreorderTraversal  Visit the data item.  Visit the left sub tree.  Visit the right sub tree.
  • 24.
    Binary Tree Traversal PreorderTraversal 1 2 3 4 5 6 8 9 7 10
  • 25.
    Binary Tree Traversal Result: 1,  PreorderTraversal 2 3 4 5 6 8 9 7 10
  • 26.
    Binary Tree Traversal Result: 1, 2,  PreorderTraversal 3 4 5 6 8 9 7 10
  • 27.
    Binary Tree Traversal Result: 1, 2, 4,  PreorderTraversal 3 5 6 8 9 7 10
  • 28.
    Binary Tree Traversal Result: 1, 2, 4, 8,  PreorderTraversal 3 5 6 9 7 10
  • 29.
    Binary Tree Traversal Result: 1, 2, 4, 8, 9,  PreorderTraversal 3 5 6 7 10
  • 30.
    Binary Tree Traversal Result: 1, 2, 4, 8, 9, 5,  PreorderTraversal 3 6 7 10
  • 31.
    Binary Tree Traversal Result: 1, 2, 4, 8, 9, 5, 10,  PreorderTraversal 3 6 7
  • 32.
    Binary Tree Traversal Result: 1, 2, 4, 8, 9, 5, 10, 3,  PreorderTraversal 6 7
  • 33.
    Binary Tree Traversal Result: 1, 2, 4, 8, 9, 5, 10, 3, 6  PreorderTraversal 7
  • 34.
    Binary Tree Traversal Result: 1, 2, 4, 8, 9, 5, 10, 3, 6, 7  PreorderTraversal
  • 35.
    Binary Tree Traversal Result: 1, 2, 4, 8, 9, 5, 10, 3, 6, 7  PreorderTraversal
  • 36.
    Binary Tree Traversal void PreOrder(TreeNode Root)  {  if (Root !=null)  {  PrintNode(RootElement);  PreOrder(RootLeft);  PreOrder(RootRight);  }  }
  • 37.
    Binary Tree Traversal PostorderTraversal  Visit the left sub tree.  Visit the right sub tree.  Visit the data item.
  • 38.
    Binary Tree Traversal PostorderTraversal 1 2 3 4 5 6 8 9 7 10
  • 39.
    Binary Tree Traversal Result: 8,  PostorderTraversal 2 3 4 5 6 9 7 10 1
  • 40.
    Binary Tree Traversal Result: 8, 9  PostorderTraversal 2 3 4 5 6 7 10 1
  • 41.
    Binary Tree Traversal Result: 8, 9, 4,  PostorderTraversal 2 3 5 6 7 10 1
  • 42.
    Binary Tree Traversal Result: 8, 9, 4, 10,  PostorderTraversal 2 3 5 6 7 1
  • 43.
    Binary Tree Traversal Result: 8, 9, 4, 10, 5,  PostorderTraversal 2 3 6 7 1
  • 44.
    Binary Tree Traversal Result: 8, 9, 4, 10, 5, 2,  PostorderTraversal 3 6 7 1
  • 45.
    Binary Tree Traversal Result: 8, 9, 4, 10, 5, 2, 6,  PostorderTraversal 3 7 1
  • 46.
    Binary Tree Traversal Result: 8, 9, 4, 10, 5, 2, 6, 7  PostorderTraversal 3 1
  • 47.
    Binary Tree Traversal Result: 8, 9, 4, 10, 5, 2, 6, 7, 3  PostorderTraversal 1
  • 48.
    Binary Tree Traversal Result: 8, 9, 4, 10, 5, 2, 6, 7, 3, 1  PostorderTraversal
  • 49.
    Binary Tree Traversal Result: 8, 9, 4, 10, 5, 2, 6, 7, 3, 1  PostorderTraversal
  • 50.
    Binary Tree Traversal void PostOrder(TreeNode Root)  {  if (Root !=null)  {  PostOrder(RootLeft);  PostOrder(RootRight);  PrintNode(RootElement);  }  }
  • 51.
    Binary Tree Traversal Breadth FirstTraversal  Process nodes by level  Left to right within the level
  • 52.
    Binary Tree Traversal Breadth First Traversal 1 2 3 4 5 6 8 9 7 10
  • 53.
    Binary Tree Traversal Result: 1,  Breadth First Traversal 2 3 4 5 6 9 7 108
  • 54.
    Binary Tree Traversal Result: 1, 2,  Breadth First Traversal 3 4 5 6 9 7 108
  • 55.
    Binary Tree Traversal Result: 1, 2, 3,  Breadth First Traversal 4 5 6 9 7 108
  • 56.
    Binary Tree Traversal Result: 1, 2, 3, 4,  Breadth First Traversal 5 6 9 7 108
  • 57.
    Binary Tree Traversal Result: 1, 2, 3, 4, 5  Breadth First Traversal 6 9 7 108
  • 58.
    Binary Tree Traversal Result: 1, 2, 3, 4, 5, 6,  Breadth First Traversal 9 7 108
  • 59.
    Binary Tree Traversal Result: 1, 2, 3, 4, 5, 6, 7,  Breadth First Traversal 9 108
  • 60.
    Binary Tree Traversal Result: 1, 2, 3, 4, 5, 6, 7, 8,  Breadth First Traversal 9 10
  • 61.
    Binary Tree Traversal Result: 1, 2, 3, 4, 5, 6, 7, 8, 9,  Breadth First Traversal 10
  • 62.
    Binary Tree Traversal Result: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Breadth First Traversal
  • 63.
    Binary Tree Traversal Result: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Breadth FirstTraversal
  • 64.
    Binary Tree Traversal void BreadthFirst(TreeNode Root)  {  // Solve It  }
  • 65.
    Expression Trees  Isa BinaryTree.  Leaves of an expression tree are operand, such as constants or variable names.  Other nodes contain the operators.
  • 66.
    Expression Trees  Wecan 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 +
  • 67.
    Expression Trees  InorderTraversal(left, Root, Right) * b c a + d e * + f * g +
  • 68.
    Expression Trees  InorderTraversal(left, Root, Right)  a+ b * c + d * e + f * g (infix notation) * b c a + d e * + f * g +
  • 69.
    Expression Trees  PreorderTraversal(Root, Left, Right) * b c a + d e * + f * g +
  • 70.
    Expression Trees  PreorderTraversal(Root, Left, Right)  + + a * b c * + * d e f g (Prefix Notation) * b c a + d e * + f * g +
  • 71.
    Expression Trees  PostorderTraversal(Left, Right, Root) * b c a + d e * + f * g +
  • 72.
    Expression Trees  PostorderTraversal(Left, Right, Root)  a b c * + d e * f + g * + (Postfix Notation) * b c a + d e * + f * g +
  • 73.
    Expression Trees  PostfixExpression to ExpressionTree  a b + c d e + * *
  • 74.
    Expression Trees  PostfixExpression to ExpressionTree  a b + c d e + * * a b a, b are operands push pointers to their nodes in the stack
  • 75.
    Expression Trees  PostfixExpression 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  PostfixExpression 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  PostfixExpression 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  PostfixExpression 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  PostfixExpression 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
  • 82.
    Binary Search TreesImplementation  typedef struct TreeNode *PtrToNode;  typedef struct *PtrToNode SearchTree;  typedef struct *PtrToNode Position;  struct TreeNode  {  ElementType Element;  TreeNode Left;  TreeNode Right;  }
  • 83.
    Binary Search TreeImplementation  SearchTree MakeEmpty(SearchTree T)  {  if (T != NULL)  {  MakeEmpty(TLeft);  MakeEmpty(TRight);  free(T);  }  return NULL;  }
  • 84.
    Binary Search TreeImplementation  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.
  • 85.
    Binary Tree Implementation Position Find(ElementType X, SearchTree T)  {  if (T == NULL)  {  return NULL;  }  if (X < TElement)  {  return Find(X, TLeft);  }else  if (X > TElement)  {  return Find(X, TRight);  }  else  return T;  }
  • 86.
    Binary Search TreeImplementation  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 (TLeft == NULL)  {  return T;  }else  Return FindMin(TLeft);  }
  • 88.
    Binary Search TreeImplementation  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 (TRight == NULL)  {  return T;  }else  Return FindMin(TRight);  }
  • 90.
    Binary Search TreeImplementation  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.
  • 91.
    Binary Search TreeImplementation  SearchTree Insert(ElementType X, SearchTree T)  {  if (T == NULL)  {  T = malloc(sizeof(struct TreeNode));  TElement = X;  TLeft = TRight = NULL;  }else  if (X < TElement)  {  TLeft = Insert(X,TLeft);  } else  if (X > TElement)  {  TRight = Insert(X,TRight);  } else  return T;  }
  • 92.
    Binary Search TreeImplementation  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.
  • 93.
    Binary Search TreeImplementation  Delete R S T U Node to be deleted
  • 94.
    Binary Search TreeImplementation  Delete R S T U Node to be deleted R T U Before Deletion After Deletion
  • 95.
    Binary Search TreeImplementation  Delete 6 2 1 4 Node to be deleted 3 8
  • 96.
    Binary Search TreeImplementation  Delete 6 2 1 4 Node to be deleted 3 8 6 2 1 4 3 8 6 2 1 4 3 8
  • 97.
    Binary Search TreeImplementation  Delete 6 2 1 5 Node to be deleted 3 8 4
  • 98.
    Binary Search TreeImplementation  Delete 6 2 1 5 Node to be deleted 3 8 4 6 2 1 5 3 8 4 6 2 1 5 3 8 4
  • 99.
    Binary Search TreeImplementation  SearchTree Delete(ElementType X, SearchTree T)  {  Position TempCell  if (T == NULL)  {  Error(“Element Not Found”);  }else  if (X < TElement)  {  TLeft = Delete(X,TLeft);  } else  if (X > TElement)  {  TRight = Delete(X,TRight);  } else // contd. On next page  }
  • 100.
    Binary Search TreeImplementation  SearchTree Delete(ElementType X, SearchTree T)  {….  If (TLeft && TRight) // left and right child //  {  TmpCell = FindMin(TRight);  TElement = TmpCellElement;  TRight = Delete(TElement, TRight);  }  Else  {  TmpCell = T;  If (TLeft == NULL) // leaves , no children //  {  T = TRight;  }else if (TRight == NULL)  {  T = TLeft; }  Free(TmpCell);  }  return T;  }
  • 101.
    AVL Trees  Binarysearch 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  BalancedCondition  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  Balancecondition 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 00 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)
  • 105.
    AVL Trees 3 51 0 10 0 2 1 5 1 8 0 0 balance = right height – left height = 1 – 3 = -2 (Non-AVL Tree) balance = right height – left height = 1 – 2 = -1 (AVL Tree) 2 41 0 0 1 2 3 0 0 2 0 0 1 0 3
  • 106.
    AVL Trees  Heightof 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.
  • 107.
    AVL Trees 7 6 5 8 balance =right height – left height = ? – ? = ? 2 41 3
  • 108.
    AVL Trees 7 6 5 8 balance= right height – left height = 0 – 2 = -2 ( Non-AVL) 2 41 3
  • 109.
    AVL Trees 7 6 5 8 balance =right height – left height = 0 – 2 = -2 ( Non-AVL) 2 41 3
  • 110.
    AVL Trees 7 6 5 8 balance =right height – left height = 2 – 3 = -1 (AVL Tree) 2 41 3
  • 111.
    AVL Trees 3 2 4 balance =right height – left height = ? – ? = ? 1 5
  • 112.
    AVL Trees 3 2 4 balance =right height – left height = 2 – 0 = 2 (Non- AVL)1 5
  • 113.
    AVL Trees 3 2 4 balance =right height – left height = 2 – 0 = 2 (Non- AVL)1 5
  • 114.
    AVL Trees 3 2 4 balance =right height – left height = 2 – 1 = 1 (AVL Tree) 1 5
  • 115.
    AVL Trees 3 2 4 balance =right height – left height = ? – ? = ? 1 5 6
  • 116.
    AVL Trees 3 2 4 balance =right height – left height = 3 – 1 = 2 (Non AVL) 1 5 6
  • 117.
    AVL Trees 3 2 4 balance =right height – left height = 3 – 1 = 2 (Non AVL) 1 5 6
  • 118.
    AVL Trees 3 2 4 balance =right height – left height = 2 – 2 = 0 (AVL Tree) 1 5 6
  • 119.
    AVL Trees 3 2 4 balance =right height – left height = ? - ? = ? 1 5 6 7
  • 120.
    AVL Trees 3 2 4 balance =right height – left height = 2 - 0 = 2 (Non AVL) 1 5 6 7
  • 121.
    AVL Trees 3 2 4 balance =right height – left height = 2 - 0 = 2 (Non AVL) 1 5 6 7
  • 122.
    AVL Trees 3 2 4 balance =right height – left height = 2 - 2 = 0 (AVL Tree) 1 5 6 7
  • 123.
    AVL Tree Implementation typedef struct AvlNode *Position;  typedef struct AvlNode *AvlTree;  struct AvlNode  {  ElementType Element;  Avltree Left;  Avltree Right;  int Height;  }
  • 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.
  • 127.
  • 128.
  • 129.
  • 130.
    AVL Tree Rotation k2 XY Z k1 k2 X Y Z k1
  • 131.
    AVL Tree Implementation Position LeftRotation(Position K2)  {  Position K1 = K2 Right;  K2Right = K1Left;  K1Left = K2;  K2Height = Max(Height(K2Right), Height(K2Left))+1;  K1Height = Max(Height(K1Right), K2Height) + 1;  return K1;  }
  • 132.
    AVL Tree Implementation Position RightRotation(Position K2)  {  Position K1 = K2 Left;  K2Left = K1Right;  K1Right = K2;  K2Height = Max(Height(K2Left), Height(K2Right))+1;  K1Height = Max(Height(K1Left), K2Height) + 1;  return K1;  }
  • 133.
    AVL Tree Insertion AvlTree Insert(ElementType X, AvlTree T)  {  Condition 1: T == Null  Condition 2: X < TElement  Condition 3: X > TElement  }
  • 134.
    AVL Tree Insertion AvlTree Insert(ElementType X, AvlTree T)  { // T== Null  If (T == Null)  {  T = malloc(sizeof(struct AvlNode));  TElement = X;  THeight = 0;  TLeft = Null;  TRight == Null;  }  .. cont  }
  • 135.
    AVL Tree Insertion AvlTree Insert(ElementType X, AvlTree T)  { // X < TElement  If (X < TElement)  {  TLeft = Insert(X,TLeft);  if (Height(TLeft) – Height(TRight) == 2)  {  T = RightRotation(T);  }  }  .. cont  }
  • 136.
    AVL Tree Insertion AvlTree Insert(ElementType X, AvlTree T)  { // X > TElement  If (X > TElement)  {  TRight = Insert(X,TRight);  if (Height(TRight) – Height(TLeft) == 2)  {  T = LeftRotation(T);  }  }  THeight = Max(Height(TLeft),Height(TRight))+1;  return T;  }
  • 137.
    AVL Tree Insertion AvlTree Insert(ElementType X, AvlTree T)  { // X > TElement  If (X > TElement)  {  TRight = Insert(X,TRight);  if (Height(TRight) – Height(TLeft) == 2)  {  T = LeftRotation(T);  }  }  THeight = Max(Height(TLeft),Height(TRight))+1;  return T;  }
  • 138.
    AVL Tree Implementation Position LeftRightRotation(Position K3)  {  K3Left = LeftRotation(K3Left)  return RightRotation(K3);  }  Position RightLeftRotation(Position K3)  {  K3Right = RightRotation(K3Right)  return LeftRotation(K3);  }
  • 139.
    AVL Trees 3 2 4 balance =right height – left height = 2 - 2 = 0 (AVL Tree) 1 5 6 7
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
    AVL Tree Insertion AvlTree Insert(ElementType X, AvlTree T)  { // X < TElement  If (X < TElement)  {  TLeft = Insert(X,TLeft);  if (Height(TLeft) – Height(TRight) == 2)  {  If (x<TLeftElement)  T = RightRotation(T); Else T = RightLeftRotation(T);  }  }  .. cont  }
  • 160.
    AVL Tree Insertion AvlTree Insert(ElementType X, AvlTree T)  { // X > TElement  If (X > TElement)  {  TRight = Insert(X,TRight);  if (Height(TRight) – Height(TLeft) == 2)  {  If (x>TRightElement)  T = LeftRotation(T); Else T = LeftRightRotation(T);  }  }  THeight = Max(Height(TLeft),Height(TRight))+1;  return T;  }