0% found this document useful (0 votes)
48 views

CSC2103 L5

The document discusses three data structures: expression trees, AVL trees, and red-black trees. It provides methods for constructing an expression tree from a postfix notation and describes how to balance an AVL tree after insertions by performing single and double rotations. It also lists the key properties that red-black trees must satisfy to remain balanced.

Uploaded by

Felicia Fam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

CSC2103 L5

The document discusses three data structures: expression trees, AVL trees, and red-black trees. It provides methods for constructing an expression tree from a postfix notation and describes how to balance an AVL tree after insertions by performing single and double rotations. It also lists the key properties that red-black trees must satisfy to remain balanced.

Uploaded by

Felicia Fam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

 Construct expression tree

 AVL Tree
 Red-Black Tree

CSC2103 - Data Structures & Algorithms


3
For example, given the postfix expression,
we want to construct the expression tree.

ab+cd+e**

 We will use a stack of references to keep


the references to the nodes of the
expression tree.
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.
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

CSC2103 - Data Structures & Algorithms


7 7

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

CSC2103 - Data Structures & Algorithms


 Insertion
15
 After insert 3

11 18

32

CSC2103 - Data Structures & Algorithms


 To ensure balance condition for AVL-tree,
after insertion of a new node, we back up the
path from the inserted node to root and check
the balance condition for each node.
 When the tree structure changes during
insertion or deletion, we need to transform
the tree to restore the AVL tree property.
 This is done using:
 single rotations
 double rotations
33

CSC2103 - Data Structures & Algorithms


 Single rotation right

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

CSC2103 - Data Structures & Algorithms


 Single rotation left

6
7

7
6 9
9

Balance the tree by rotating the


root, 6, to the left so that it becomes
Unbalanced the left sub tree of 7.

35

CSC2103 - Data Structures & Algorithms


 Double rotation left- right

9 9

5 7 7

7 5 9
5

36

CSC2103 - Data Structures & Algorithms


 Double rotation right - left

5 5 7

9 7 9
5

7 9

37

CSC2103 - Data Structures & Algorithms


Red Tree
Red Black Tree
Red-Black Tree
 A red-black tree is a binary search tree
with the following coloring properties:
1. Every node is red or black.
2. The root is black.
3. If a node is red, its children are black.
4. The path from the root to any leaf passes
through the same number of black nodes
 Purpose –keep the tree balanced
39

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Red-Black Tree Example

11
Note:

Properties of Red-
2 14
Black trees?

1 7

5 8

40

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Red-Black Tree Example

11

2 14

15
1 7

5 8

41

CSC2103 - Data Structures & Algorithms


Red Black Tree

Insert 30 30

 Problem: Root node is red, violate property 2


 Rectification: Recolor

30 Done

42

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Red-Black Trees: searching
 Exactly same mechanism as a normal binary
search tree.

 Red-Black Tree Insertion


 Case 1
 If the parent of the new node is black, there is
no violation of red-black tree property. The
insertion process is completed.

43

CSC2103 - Data Structures & Algorithms


Red Black Tree
Insert 15 30

15

No violation, insertion is completed.

44

CSC2103 - Data Structures & Algorithms


Red Black Tree
Insert 60 30

15 60

No violation, insertion is completed.

45

CSC2103 - Data Structures & Algorithms


Red Black Tree

 Insert 20

30

15 60

10 20

No violation, Insertion is completed

46

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Case 2
 If the parent of the new node is ‘Red’, the
property 3, which states that the children of a
red node must be black, is violated.
 What rectification steps to take?
 Case 2.1 – Uncle of the new node is ‘Red’
 Recolor parent, grandparent, and uncle
 After the recolor, the grandparent will be ‘red’.

 If the grandparent is the root, recolor the root to black, and


the property of the Red-Black tree will not be violated.
 If the great-grandparent is not ‘Black’, the processes of

analysis and rectification continue up-ward. 47

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Insert 10
30 30
30

15 60 15 60
15 60

10 10
10

 Problem: Violation of property 3, if a node is red, its children are black


 Analysis: Sibling of parent (uncle) [60] is red. New node [10] is the left
child of parent [15], which is the left child of grandparent [30].
 Rectification: Recolor the parent [15], grandparent [30], and uncle [60],
but this violate property 2, which states that the root is black.
 Since the grandparent [30] is the root, recolor the root and the property of
the ‘Red-Black” tree is not violated.
 No violation, insertion is completed
48

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Case 2.2 – Uncle of the new node is ‘Black’
 Case 2.2.1
 The new node is the left child of parent, and the parent is
the left child of grandparent.
 Rotate Right at the grandparent level

 Recolor the (original) grandparent and (original) parent.

 If the grandparent is now ‘Red’, and the great-grandparent

is also ‘Red’, the processes of analysis and rectification


continue up-ward.

49

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Case 2.2.2
 The new node is the right child of parent, and the parent is
the right child of grandparent.
 Rotate Left at the grandparent level

 Recolor the (original) grandparent and (original) parent.

 If the grandparent is now ‘Red’, and the great-grandparent


is also ‘Red’, the processes of analysis and rectification
continue up-ward.

50

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Case 2.2.3
 The new node is the right child of parent, and the parent is
the left child of grandparent.
 Rotate Right at the parent level, followed by a rotate Left

at the grandparent level.


 Recolor the (original) grandparent and (original) parent.

 If the grandparent is now ‘Red’, and the great-grandparent


is also ‘Red’, the processes of analysis and rectification
continue up-ward.

51

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Case 2.2.4
 The new node is the left child of parent, and the parent is
the right child of grandparent.
 Rotate Left at the parent level, followed by a rotate Right

at the grandparent level.


 Recolor the (original) grandparent and (original) parent.

 If the grandparent is now ‘Red’, and the great-grandparent


is also ‘Red’, the processes of analysis and rectification
continue up-ward.

52

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Insert 5 30
 Problem: Violation of
 property 3, if a node is red, 15 60
its children are black
 Analysis: Uncle [20] is 10 20
red. New node [5] is the
left child of parent [10],
which is the left child of 5 30
grandparent [15].
 Rectification: Recolor 15 60
the parent [10],
grandparent [15], and uncle 10
[20] No violation, insertion 20

 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

NIL node is considered black)


 New node [90] is the right 10 20 85

child of parent [85], which is


the right child of 5
90
grandparent [60].
 Rectification: Left rotate at
grandparent [60] Recolor the
node [60] and [85]

54

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Insert90
 Rotate, recolor is
30

complete. 15 85
 No violation, insert
complete. 10 20 60 90

55

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Insert 50
 Problem: Violation of
30
property 3, if a node is
red, its children are
black 15 85

 Analysis: New node


[50] is the left child of 10 20 60 90
parent [60], which is the
left child of grandparent 5
[85]. 50

 Rectification: Recolor
parent [60], grandparent
[85], and uncle [90]
56

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Insert 50
 No violation,
30

insertion is 15 85
completed
10 20 60 90

5 50

57

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Insert40
 Problem: Violation of 30
property 3, if a node is
red, its children are
black 15 85
 Analysis: New node
[40] is the left child of
parent [50], which is 10 20 60 90
the left child of
grandparent [60].
Uncle is black. (A NIL 5 50
node is considered
black)
 Rectification: Right 40
rotate at grandparent
[60] Recolor the node
[60] and [50] 58

CSC2103 - Data Structures & Algorithms


Red Black Tree
 Insert 40
30
 No violation,
insertion is 15 85

complete.
10 20 50 90

5 40 60

59

CSC2103 - Data Structures & Algorithms

You might also like