CSC2103 L5
CSC2103 L5
AVL Tree
Red-Black Tree
ab+cd+e**
the stack.
The first reference is set to be the right subtree of the
current node.
The second reference is set to the left subtree of the current
node.
We push the reference to the current node to the stack.
Postfix expression: a b + c d + e * *
Top
Stack:
a
Postfix expression: a b + c d + e * *
Top
Stack:
a b
Postfix expression: a b + c d + e * *
Top
Stack:
a b +
Postfix expression: a b + c d + e * *
Top
Stack:
a b +
b
Postfix expression: a b + c d + e * *
TopTop
Stack:
a +
a b
Postfix expression: a b + c d + e * *
Top
Stack:
+
a b
Postfix expression: a b + c d + e * *
TopTop
Stack:
+ c
a b
Postfix expression: a b + c d + e * *
TopTop
Stack:
+ c d
a b
Postfix expression: a b + c d + e * *
Top
Stack:
+ c d +
a b
Postfix expression: a b + c d + e * *
TopTop
Stack:
+ c d +
a b d
Postfix expression: a b + c d + e * *
TopTop
Stack:
+ c +
a b c d
Postfix expression: a b + c d + e * *
Top
Top
Stack:
+ +
a b c d
Postfix expression: a b + c d + e * *
TopTop
Stack:
+ + e
a b c d
Postfix expression: a b + c d + e * *
Top
Stack:
+ + e *
a b c d
Postfix expression: a b + c d + e * *
TopTop
Stack:
+ + e *
a b c d e
Postfix expression: a b + c d + e * *
Top
Top
Stack:
+ + *
a b c d + e
c d
Postfix expression: a b + c d + e * *
Top
Top
Stack:
+ *
a b + e
c d
Postfix expression: a b + c d + e * *
Top
Stack:
+ * *
a b + e
c d
Postfix expression: a b + c d + e * *
Top
Top
Stack:
+ * *
+ e *
a b
c d + e
c d
Postfix expression: a b + c d + e * *
Top
Top
Stack:
+ *
+ *
a b
a b + e
c d
Postfix expression: a b + c d + e * *
Top
Top
Stack:
*
+ *
a b + e
c d
Method:
Evaluate the expression from left to right:
When we find an operand, we dynamically create a node and
set the operand as the content of the node. The reference to the
node is pushed to the stack.
When we find an operator, we dynamically create a node and
set the operator as the content of the node. We refer to this
node as the current node.
We then pop the two references to the operands at the top of
the stack.
The first reference is set to be the right subtree of the
current node.
The second reference is set to the left subtree of the current
node.
We push the reference to the current node to the stack.
CSC2103 - Data Structures & Algorithms
28
AnAVL tree is a self-balancing binary
search tree in which
The heights of the left and right sub-trees of
any node differ by at most one; therefore, it is
also said to be height balanced.
29
5 9 5 9
2 6 8 13 2 6 8 13
4 AVL tree
4
not an AVL tree
30
3
CSC2103 - Data Structures & Algorithms
Searching an AVL tree is exactly same as
searching a regular binary search tree
all descendants to the right of a node are
greater than the node
all descendants to the left of a node are less
than the node
Whenever we insert a node into or delete a node
from a tree, the resulting tree may become
unbalanced.
AVL insert: same as BST insert, except that we
might have to “fix” the AVL tree after an insert.
31
11 18
32
9
7
7
6 9
6
Balance the tree by rotating the
root, 9, to the right so that it
Unbalanced becomes the right sub tree of 7.
34
6
7
7
6 9
9
35
9 9
5 7 7
7 5 9
5
36
5 5 7
9 7 9
5
7 9
37
11
Note:
Properties of Red-
2 14
Black trees?
1 7
5 8
40
11
2 14
15
1 7
5 8
41
Insert 30 30
30 Done
42
43
15
44
15 60
45
Insert 20
30
15 60
10 20
46
15 60 15 60
15 60
10 10
10
49
50
51
52
completed 53
5
CSC2103 - Data Structures & Algorithms
Red Black Tree
Insert 90
Problem: Violation of 30
property 3, if a node is red, its
children are black
15
Analysis: Uncle is black (A 60
54
complete. 15 85
No violation, insert
complete. 10 20 60 90
55
Rectification: Recolor
parent [60], grandparent
[85], and uncle [90]
56
insertion is 15 85
completed
10 20 60 90
5 50
57
complete.
10 20 50 90
5 40 60
59