Dr' Wenzhan Song - PowerPoint PPT Presentation

1 / 70
About This Presentation
Title:

Dr' Wenzhan Song

Description:

See how trees are used to implement the file system of several popular operating ... Step 2: Find the child of node x, say node y, who's subtree is taller; ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 71
Provided by: holge
Category:
Tags: song | taller | wenzhan

less

Transcript and Presenter's Notes

Title: Dr' Wenzhan Song


1
Dr. Wenzhan Song Assistant Professor, Computer
Science
2
Chapter 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

3
Preliminaries
Generic tree
4
Preliminaries
A tree example
5
Preliminaries
First child/next sibling representation of the
tree in previous example
6
Preliminaries
UNIX directoty
7
Preliminaries
Unix directory with file sizes obtained via
postorder traversal
8
Binary Trees
Generic binary tree
9
Binary Trees
Worst case binary tree
10
Binary Trees
Expression tree for (abc)((def)g)
11
Binary Search Trees
Two binary trees (on the left tree is a search
tree)
12
Binary Search Trees
binary search trees before and after inserting 5
13
Binary Search Trees
Deletion of a node (4) with one child, before and
after
14
Binary Search Trees
Deletion of a node (2) with two children, before
and after
15
Binary Search Trees
A randomly generated binary search tree
16
Binary Search Trees
Binary search tree after insert/remove
pairs
17
AVL Trees
A bad binary search tree. Requiring balance at
the root is not enough
18
AVL Trees
Two binary search tree. Only the left tree is AVL.
19
AVL Trees
Smallest AVL tree of height 9
20
AVL Trees
Single rotation to fix case 1
21
AVL Trees
AVL property destroyed by insertion of 6, then
fixed by a single rotation
22
AVL Trees
Single rotation fixes case 4
23
AVL Trees
24
AVL Trees
25
AVL Trees
26
AVL Trees
27
AVL Trees
Single rotation fails to fix case 2
28
AVL Trees
Left-right double rotation to fix case 2
29
AVL Trees
Right-left double rotation to fix case 3
30
AVL Trees
31
AVL Trees
32
AVL Trees
33
AVL Trees
34
AVL Trees
35
AVL 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)

36
Splay Trees
Figure 4.47 Zig-zag
37
Splay Trees
Figure 4.48 Zig-zig
38
Splay Trees
The first splay step is at k1, and is clearly a
zig-zag
39
Splay Trees
The next splay step at k1 is a zig-zig
40
Splay Trees
41
Splay Trees
Result of splaying at node1
42
Splay Trees
Result of splaying at node 1 a tree of all left
children
43
Splay Trees
Result of splaying the previous tree at node 2
44
Splay Trees
Result of splaying the previous tree at node 3
45
Splay Trees
Result of splaying the previous tree at node 4
46
Splay Trees
Result of splaying the previous tree at node 5
47
Splay Trees
Result of splaying the previous tree at node 6
48
Splay Trees
Result of splaying the previous tree at node 7
49
Splay Trees
Result of splaying the previous tree at node 8
50
Splay Trees
Result of splaying the previous tree at node 9
51
Splay 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

52
B-Trees
5-ary tree of 31 nodes has only three levels
53
B-Trees
B-tree of order 5
54
B-Trees
B-tree after insertion of 57 into the tree in the
previous fig
55
B-Trees
Insertion of 55 into the B-tree in previous Fig
causes a split into two leaves
56
B-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
57
B-Trees
B-tree after the deletion of 99 from the B-tree
in previous Fig
58
(No Transcript)
59
Red-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)

60
Red-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)
61
Bottom-up Insertion
Fig. 12.10 Zig rotation and zig-zag rotation work
if S is black
62
Top-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
63
Top-down Insertion
64
Top-down Deletion
65
(No Transcript)
66
Trees
Tree for Exercises 4.1 to 4.3
67
Trees
Tree for Exercise 4.8
68
Trees
Tree for Exercise 4.27
69
Trees
Tree for Exercise 4.43
70
Trees
Two isomorphic trees
Write a Comment
User Comments (0)
About PowerShow.com