Title: Dr' Wenzhan Song
1Dr. Wenzhan Song Assistant Professor, Computer
Science
2Chapter 4 Trees
- See how trees are used to implement the file
system of several popular operating systems - See how trees can be used to evaluate arithmetic
expressions - See how to use trees to support searching
operations in O(log N) worst-case bounds - Discuss and use the set and map classes
3Preliminaries
Generic tree
4Preliminaries
A tree example
5Preliminaries
First child/next sibling representation of the
tree in previous example
6Preliminaries
UNIX directoty
7Preliminaries
Unix directory with file sizes obtained via
postorder traversal
8Binary Trees
Generic binary tree
9Binary Trees
Worst case binary tree
10Binary Trees
Expression tree for (abc)((def)g)
11Binary Search Trees
Two binary trees (on the left tree is a search
tree)
12Binary Search Trees
binary search trees before and after inserting 5
13Binary Search Trees
Deletion of a node (4) with one child, before and
after
14Binary Search Trees
Deletion of a node (2) with two children, before
and after
15Binary Search Trees
A randomly generated binary search tree
16Binary Search Trees
Binary search tree after insert/remove
pairs
17AVL Trees
A bad binary search tree. Requiring balance at
the root is not enough
18AVL Trees
Two binary search tree. Only the left tree is AVL.
19AVL Trees
Smallest AVL tree of height 9
20AVL Trees
Single rotation to fix case 1
21AVL Trees
AVL property destroyed by insertion of 6, then
fixed by a single rotation
22AVL Trees
Single rotation fixes case 4
23AVL Trees
24AVL Trees
25AVL Trees
26AVL Trees
27AVL Trees
Single rotation fails to fix case 2
28AVL Trees
Left-right double rotation to fix case 2
29AVL Trees
Right-left double rotation to fix case 3
30AVL Trees
31AVL Trees
32AVL Trees
33AVL Trees
34AVL Trees
35AVL Trees
- Summarized Tips
- Case 1 (left-left) or Case 4 (right-right)
- Step 1 Find the deepest unbalanced subtree root,
say node x - Step 2 Find the child of node x, say node y,
whos subtree is taller - Step 3 Rotate subtree with node x promote node
y to replace the position of node x, then rotate
naturally only one choice! - Case 2 (left-right) or Case 3 (right-left)
- Step 1 Find the deepest unbalanced subtree root,
say node x - Step 2 Find the child of node x, say node y,
whos subtree is taller - Step 3 Find the child of node y, say node z,
whos subtree is taller - Step 4 Rotate subtree with node y promote node
z to replace the position of node y, then rotate
naturally only one choice! - Step 5 Rotate subtree with node x promote node
z to replace the position of node x, then rotate
naturally only one choice! - (Alternative of Step 4 and 5 promote node z to
replace node x, than attach x and y to left and
right of node z, and other subtrees A,B,C,D are
attached naturally only one choice)
36Splay Trees
Figure 4.47 Zig-zag
37Splay Trees
Figure 4.48 Zig-zig
38Splay Trees
The first splay step is at k1, and is clearly a
zig-zag
39Splay Trees
The next splay step at k1 is a zig-zig
40Splay Trees
41Splay Trees
Result of splaying at node1
42Splay Trees
Result of splaying at node 1 a tree of all left
children
43Splay Trees
Result of splaying the previous tree at node 2
44Splay Trees
Result of splaying the previous tree at node 3
45Splay Trees
Result of splaying the previous tree at node 4
46Splay Trees
Result of splaying the previous tree at node 5
47Splay Trees
Result of splaying the previous tree at node 6
48Splay Trees
Result of splaying the previous tree at node 7
49Splay Trees
Result of splaying the previous tree at node 8
50Splay Trees
Result of splaying the previous tree at node 9
51Splay Trees
- Add operation does not involve splaying
- Delete operation does
- (1) splay the to-be-delete node to the root,
then delete the root and result TL and TR. - (2) Rotate the largest element of TL to the
root, then TL will have a root with no right
child. - (3) Let TR be the right child of new root
52B-Trees
5-ary tree of 31 nodes has only three levels
53B-Trees
B-tree of order 5
54B-Trees
B-tree after insertion of 57 into the tree in the
previous fig
55B-Trees
Insertion of 55 into the B-tree in previous Fig
causes a split into two leaves
56B-Trees
Insertion of 40 into the B-tree in previous Fig
causes a split into two leaves and then a split
of the parent node
57B-Trees
B-tree after the deletion of 99 from the B-tree
in previous Fig
58(No Transcript)
59Red-Black Trees
- It is a binary search tree with the following
coloring properties - Every node is colored either red or black
- The root is black
- If a node is red, its children must be black
- Every path from a node to a NULL pointer must
contain the same number of black nodes - Hence the height is at most 2log(N1)
60Red-Black Trees
Red node
Fig. 12.9 Example of a red black tree (insertion
sequence 10, 85, 15, 70, 20, 60, 30, 50, 65, 80,
90, 40, 5, 55)
61Bottom-up Insertion
Fig. 12.10 Zig rotation and zig-zag rotation work
if S is black
62Top-down Insertion
Fig. 12.10 Color flip only if Xs parent is red
do we continue with a rotation Then apply the
rotation in previous slides
63Top-down Insertion
64Top-down Deletion
65(No Transcript)
66Trees
Tree for Exercises 4.1 to 4.3
67Trees
Tree for Exercise 4.8
68Trees
Tree for Exercise 4.27
69Trees
Tree for Exercise 4.43
70Trees
Two isomorphic trees