binary trees -2 chapter 6 1. 2 threaded trees (depth first) binary trees have a lot of wasted space:...
TRANSCRIPT
1
Binary trees -2
Chapter 6
2
3
Threaded trees (depth first)
• Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers
• We can use these pointers to help us in inorder traversals
• We have the pointers reference the next node in an inorder traversal; called threads
• We need to know if a pointer is an actual link or a thread, so we keep a Boolean for each pointer
4
Threaded Tree Example
8
75
3
11
13
1
6
9
5
Threaded Tree Traversal
• Start at the leftmost node in the tree, print it, and follow its right thread– following a thread to the right, will output
the node and continue to its right– following a link to the right, will go to the
leftmost node, print it, and continue
6
Threaded Tree Traversal -1
8
75
3
11
13
1
6
9
Start at leftmost node, print it
Output1
7
Threaded Tree Traversal -2
8
75
3
11
13
1
6
9
Follow thread to right, print node
Output13
8
Threaded Tree Traversal -3
8
75
3
11
13
1
6
9
Follow link to right, go to leftmost node and print
Output135
9
Threaded Tree Traversal -4
8
75
3
11
13
1
6
9
Follow thread to right, print node
Output1356
10
Threaded Tree Traversal -5
8
75
3
11
13
1
6
9
Follow link to right, go to leftmost node and print
Output13567
11
Threaded Tree Traversal - 6
8
75
3
11
13
1
6
9
Follow thread to right, print node
Output135678
8/8/02 12Amir Kamil
Threaded Tree Traversal - 7
8
75
3
11
13
1
6
9
Follow link to right, go to leftmost node and print
Output1356789
13
Threaded Tree Traversal -8
8
75
3
11
13
1
6
9
Follow thread to right, print node
Output135678911
14
Threaded Tree Traversal -9
8
75
3
11
13
1
6
9
Follow link to right, go to leftmost node and print
Output13567891113
15
Threaded Tree Traversal CodeNode leftMost(Node n) { Node ans = n; if (ans == null) { return null; } while (ans.left != null) { ans = ans.left; } return ans;}
void inOrder(Node n) { Node cur = leftmost(n); while (cur != null) { print(cur); if (cur.rightThread) { cur = cur.right; } else { cur = leftmost(cur.right); } }}
16
Traversal Through Tree Transformation (read only)
• Systematic visiting of all nodes in the tree.
• Each node visited once, does not specify order.
• Breadth-first traversal: visit each node, starting from the lowest level and moving down by level, visiting nodes from left to right.
• Depth-first traversals: go in subtree as deep as you can, backtrack. Differ on the order of visiting root, left, right subtrees.
• preorder: VLR.
• inorder: LVR.
• postorder: LRV.
17
Insertion
• searching does not modify the tree.• To insert a new node with key el(new), a tree node with a
dead end has to be reached, new node attached to it.• found using same procedure as searching: compare key
of currently scanned node el(cur) to el(new).
• If el(new) less than the key el(cur) try left child; otherwise try right child.
• If the child is empty, discontinue search and make the child point to a new node of key el.
18
19
Inserting in threaded tree
•for inorder, only takes care of successors.•Node with a right child: has its successor somewhere in the right subtree, does not need a thread.•Why ? Threads are for "climbing up the tree", not for going down.•A node with no right child has its successor somewhere. Inherits successor from parent.•If a node becomes a left node, its parent is successor.
20
21
Deleting (try)
22
Balancing a tree
• The main idea of DSW algorithm is a rotation• There are two types of rotation, left and right• Rotate Right( Gr, Par, Ch )
– If Par is not the root of the tree• Grandparent Gr of child Ch, becomes Ch’s parent by replacing Par;
– Right subtree of Ch becomes left subtree of Ch’s parent Par;
– Node Ch acquires Par as its right child
23
A picture will help
24
25
26
AVL tree
• The height of left and right subtree of every node differ by at most one.
AVL Trees 27
Balanced and unbalanced BST
4
2 5
1 3
1
5
2
4
3
7
6
4
2 6
5 71 3
Is this “balanced”?
AVL Trees 28
Perfect Balance
• Want a complete tree after every operation– tree is full except possibly in the lower right
• This is expensive– For example, insert 2 in the tree on the left and
then rebuild as a complete tree
Insert 2 &complete tree
6
4 9
81 5
5
2 8
6 91 4
AVL Trees 29
AVL - Good but not Perfect Balance
• AVL trees are height-balanced binary search trees
• Balance factor of a node– height(left subtree) - height(right subtree)
• An AVL tree has balance factor calculated at every node– For every node, heights of left and right subtree
can differ by no more than 1– Store current heights in each node
AVL Trees 30
Node Heights
1
00
2
0
6
4 9
81 5
1
height of node = hbalance factor = hleft-hright
empty height = -1
0
0
height=2 BF=1-0=1
0
6
4 9
1 5
1
Tree A (AVL) Tree B (AVL)
AVL Trees 31
Node Heights after Insert 7
2
10
3
0
6
4 9
81 5
1
height of node = hbalance factor = hleft-hright
empty height = -1
1
0
2
0
6
4 9
1 5
1
0
7
0
7
balance factor 1-(-1) = 2
-1
Tree A (AVL) Tree B (not AVL)
AVL Trees 32
Insert and Rotation in AVL Trees
• Insert operation may cause balance factor to become 2 or –2 for some node – only nodes on the path from insertion point to
root node have possibly changed in height– So after the Insert, go back up to the root node by
node, updating heights– If a new balance factor (the difference hleft-hright) is
2 or –2, adjust tree by rotation around the node
AVL Trees 33
Single Rotation in an AVL Tree
2
10
2
0
6
4 9
81 5
1
0
7
0
1
0
2
0
6
4
9
8
1 5
1
0
7
AVL Trees 34
Let the node that needs rebalancing be .
There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of .
The rebalancing is performed through four separate rotation algorithms.
Insertions in AVL Trees
AVL Trees 35
j
k
X Y
Z
Consider a validAVL subtree
AVL Insertion: Outside Case
h
hh
AVL Trees 36
j
k
XY
Z
Inserting into Xdestroys the AVL property at node j
AVL Insertion: Outside Case
h
h+1 h
AVL Trees 37
j
k
XY
Z
Do a “right rotation”
AVL Insertion: Outside Case
h
h+1 h
AVL Trees 38
j
k
XY
Z
Do a “right rotation”
Single right rotation
h
h+1 h
AVL Trees 39
jk
X Y Z
“Right rotation” done!(“Left rotation” is mirror symmetric)
Outside Case Completed
AVL property has been restored!
h
h+1
h
AVL Trees 40
j
k
X Y
Z
AVL Insertion: Inside Case Consider a validAVL subtree
h
hh
AVL Trees 41
Inserting into Y destroys theAVL propertyat node j
j
k
XY
Z
AVL Insertion: Inside Case
Does “right rotation”restore balance?
h
h+1h
AVL Trees 42
jk
X
YZ
“Right rotation”does not restorebalance… now k isout of balance
AVL Insertion: Inside Case
hh+1
h
AVL Trees 43
Consider the structureof subtree Y… j
k
XY
Z
AVL Insertion: Inside Case
h
h+1h
AVL Trees 44
j
k
XV
Z
W
i
Y = node i andsubtrees V and W
AVL Insertion: Inside Case
h
h+1h
h or h-1
AVL Trees 45
j
k
XV
Z
W
i
AVL Insertion: Inside CaseWe will do a left-right “double rotation” . . .
AVL Trees 46
j
k
X V
ZW
i
Double rotation : first rotationleft rotation complete
AVL Trees 47
j
k
X V
ZW
i
Double rotation : second rotation
Now do a right rotation
AVL Trees 48
jk
X V ZW
i
Double rotation : second rotation
right rotation complete
Balance has been restored
hh h or h-1
AVL Trees 49
Implementation
balance (1,0,-1)
key
rightleft
No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations
Once you have performed a rotation (single or double) you won’t need to go back up the tree
AVL Trees 50
Example of Insertions in an AVL Tree
1
0
2
20
10 30
25
0
35
0
Insert 5, 40
AVL Trees 51
Example of Insertions in an AVL Tree
1
0
2
20
10 30
25
1
35
0
50
20
10 30
25
1
355
40
0
0
01
2
3
Now Insert 45
AVL Trees 52
Single rotation (outside case)
2
0
3
20
10 30
25
1
35
2
50
20
10 30
25
1
405
40
0
0
0
1
2
3
45
Imbalance35 45
0 0
1
Now Insert 34
AVL Trees 53
Double rotation (inside case)
3
0
3
20
10 30
25
1
40
2
50
20
10 35
30
1
405
45
0 1
2
3
Imbalance
45
0
1
Insertion of 34
35
34
0
0
1 25 340
54
Heap
• A heap is a binary tree T that stores a key-element pairs at its internal nodes.
• Properties :
• Min Heap: key(parent) < key(child)• Max Heap: key(parent) > key(child)]
55
Example
56
Example of a complete binary max-heap
• if B is a child node of A, then key(A) ≥ key(B).
• This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap.
• Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.
57
58
59
60
Max heap
• Terminate heap when– reach root– key parent is greater than key child
61
62
63
64
65
6.8 and 6.10 no need