SlideShare une entreprise Scribd logo
1  sur  160
Télécharger pour lire hors ligne
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;
 }

Contenu connexe

Tendances

Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap treezia eagle
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVLKatang Isip
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREESTABISH HAMID
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeManishPrajapati78
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11sumitbardhan
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackAmrinder Arora
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)Waheed Khalid
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)amna izzat
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 

Tendances (20)

Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Trees
TreesTrees
Trees
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
Avl trees
Avl treesAvl trees
Avl trees
 
Avl trees
Avl treesAvl trees
Avl trees
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 

En vedette

Glamping y aguas termales: vacaciones en contacto con la naturaleza para reca...
Glamping y aguas termales: vacaciones en contacto con la naturaleza para reca...Glamping y aguas termales: vacaciones en contacto con la naturaleza para reca...
Glamping y aguas termales: vacaciones en contacto con la naturaleza para reca...GaliWonders
 
Mentefacto De Poder
Mentefacto De PoderMentefacto De Poder
Mentefacto De Poderguest485c5c0
 
05. semana 5
05. semana 505. semana 5
05. semana 5moira_IQ
 
Cartoon 6 Linked In
Cartoon 6 Linked InCartoon 6 Linked In
Cartoon 6 Linked InLieke_Kemper
 
Cartoon Februari 2010 Linked In
Cartoon Februari 2010 Linked InCartoon Februari 2010 Linked In
Cartoon Februari 2010 Linked InLieke_Kemper
 
Dificultades en el aprendizaje tac
Dificultades en el aprendizaje tacDificultades en el aprendizaje tac
Dificultades en el aprendizaje tacbelgica bultron
 
Oh no, I´ve lost my ducks. Help me find them and win a fun goodie...
Oh no, I´ve lost my ducks. Help me find them and win a fun goodie...Oh no, I´ve lost my ducks. Help me find them and win a fun goodie...
Oh no, I´ve lost my ducks. Help me find them and win a fun goodie...Jan Alfrink
 
Assemblea constitutiva Palamós i Sant Joan per l'Autodeterminació
Assemblea constitutiva Palamós i Sant Joan per l'AutodeterminacióAssemblea constitutiva Palamós i Sant Joan per l'Autodeterminació
Assemblea constitutiva Palamós i Sant Joan per l'AutodeterminacióPere Minobas Suquet
 
Para sicologia
Para sicologiaPara sicologia
Para sicologiamoira_IQ
 
Park Inn Cardiffs 50 50 Campaign
Park Inn Cardiffs 50 50 CampaignPark Inn Cardiffs 50 50 Campaign
Park Inn Cardiffs 50 50 CampaignJan Alfrink
 
Puppet barcampexercises.jzt
Puppet barcampexercises.jztPuppet barcampexercises.jzt
Puppet barcampexercises.jztsom_nangia
 
Presentacion de video de stadistica
Presentacion de video de stadisticaPresentacion de video de stadistica
Presentacion de video de stadisticakeremcitha
 

En vedette (19)

Glamping y aguas termales: vacaciones en contacto con la naturaleza para reca...
Glamping y aguas termales: vacaciones en contacto con la naturaleza para reca...Glamping y aguas termales: vacaciones en contacto con la naturaleza para reca...
Glamping y aguas termales: vacaciones en contacto con la naturaleza para reca...
 
Mentefacto De Poder
Mentefacto De PoderMentefacto De Poder
Mentefacto De Poder
 
A logica dos_caes
A logica dos_caesA logica dos_caes
A logica dos_caes
 
Urai Slideshare
Urai SlideshareUrai Slideshare
Urai Slideshare
 
05. semana 5
05. semana 505. semana 5
05. semana 5
 
Cartoon 6 Linked In
Cartoon 6 Linked InCartoon 6 Linked In
Cartoon 6 Linked In
 
Royos X
Royos XRoyos X
Royos X
 
Cartoon Februari 2010 Linked In
Cartoon Februari 2010 Linked InCartoon Februari 2010 Linked In
Cartoon Februari 2010 Linked In
 
Dificultades en el aprendizaje tac
Dificultades en el aprendizaje tacDificultades en el aprendizaje tac
Dificultades en el aprendizaje tac
 
Oh no, I´ve lost my ducks. Help me find them and win a fun goodie...
Oh no, I´ve lost my ducks. Help me find them and win a fun goodie...Oh no, I´ve lost my ducks. Help me find them and win a fun goodie...
Oh no, I´ve lost my ducks. Help me find them and win a fun goodie...
 
Assemblea constitutiva Palamós i Sant Joan per l'Autodeterminació
Assemblea constitutiva Palamós i Sant Joan per l'AutodeterminacióAssemblea constitutiva Palamós i Sant Joan per l'Autodeterminació
Assemblea constitutiva Palamós i Sant Joan per l'Autodeterminació
 
Battle Code
Battle CodeBattle Code
Battle Code
 
Photo Album Ahmad 047
Photo Album Ahmad 047Photo Album Ahmad 047
Photo Album Ahmad 047
 
Para sicologia
Para sicologiaPara sicologia
Para sicologia
 
Park Inn Cardiffs 50 50 Campaign
Park Inn Cardiffs 50 50 CampaignPark Inn Cardiffs 50 50 Campaign
Park Inn Cardiffs 50 50 Campaign
 
Puppet barcampexercises.jzt
Puppet barcampexercises.jztPuppet barcampexercises.jzt
Puppet barcampexercises.jzt
 
Tercer sector
Tercer sectorTercer sector
Tercer sector
 
Presentacion de video de stadistica
Presentacion de video de stadisticaPresentacion de video de stadistica
Presentacion de video de stadistica
 
Tablas de contingencia
Tablas de contingenciaTablas de contingencia
Tablas de contingencia
 

Similaire à CS221A - Data Structures and Algorithms Binary Trees

Review session2
Review session2Review session2
Review session2NEEDY12345
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms CourseHunt
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdfssuser50179b
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphsmaznabili
 
Tree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy CollectionsTree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy CollectionsSunil Yadav
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologyKamranAli649587
 
DS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxDS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxVeerannaKotagi1
 
Web-IT Support and Consulting - dBase exports
Web-IT Support and Consulting - dBase exportsWeb-IT Support and Consulting - dBase exports
Web-IT Support and Consulting - dBase exportsDirk Cludts
 
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...Dirk Cludts
 

Similaire à CS221A - Data Structures and Algorithms Binary Trees (20)

Review session2
Review session2Review session2
Review session2
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphs
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Tree
TreeTree
Tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
Tree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy CollectionsTree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy Collections
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Trees unit 3
Trees unit 3Trees unit 3
Trees unit 3
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
DS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxDS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docx
 
Tree
TreeTree
Tree
 
Web-IT Support and Consulting - dBase exports
Web-IT Support and Consulting - dBase exportsWeb-IT Support and Consulting - dBase exports
Web-IT Support and Consulting - dBase exports
 
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
 
Binary tree
Binary treeBinary tree
Binary tree
 

Dernier

Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxAneriPatwari
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 

Dernier (20)

Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 

CS221A - Data Structures and Algorithms Binary Trees

  • 1. CS221A – Data Structures and Algorithms Binary Trees
  • 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
  • 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  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 +
  • 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  Postfix Expression to ExpressionTree  a b + c d e + * *
  • 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
  • 82. Binary Search Trees Implementation  typedef struct TreeNode *PtrToNode;  typedef struct *PtrToNode SearchTree;  typedef struct *PtrToNode Position;  struct TreeNode  {  ElementType Element;  TreeNode Left;  TreeNode Right;  }
  • 83. Binary Search Tree Implementation  SearchTree MakeEmpty(SearchTree T)  {  if (T != NULL)  {  MakeEmpty(TLeft);  MakeEmpty(TRight);  free(T);  }  return NULL;  }
  • 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.
  • 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 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 (TLeft == NULL)  {  return T;  }else  Return FindMin(TLeft);  }
  • 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 (TRight == NULL)  {  return T;  }else  Return FindMin(TRight);  }
  • 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.
  • 91. 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;  }
  • 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.
  • 93. Binary Search Tree Implementation  Delete R S T U Node to be deleted
  • 94. Binary Search Tree Implementation  Delete R S T U Node to be deleted R T U Before Deletion After Deletion
  • 95. Binary Search Tree Implementation  Delete 6 2 1 4 Node to be deleted 3 8
  • 96. 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
  • 97. Binary Search Tree Implementation  Delete 6 2 1 5 Node to be deleted 3 8 4
  • 98. 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
  • 99. 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  }
  • 100. 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;  }
  • 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)
  • 105. 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
  • 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.
  • 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.
  • 130. AVL Tree Rotation k2 X Y 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
  • 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;  }