Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Copyright 2000-2016 Networking Laboratory
Lecture 7. Binary Search Trees / AVL Trees
T. H. Cormen, C. E. Leiserson and R. L. RivestIntroduction to Algorithms, 3rd Edition, MIT Press, 2009
Sungkyunkwan University
Hyunseung [email protected]
Algorithms
Tree Single parent, multiple children
Binary tree Tree with 0–2 children per node
Tree Binary Tree
Introduction
Networking Laboratory 2/53
Algorithms
Introduction
Search trees support SEARCH, MINIMUM, MAXIMUM, PREDECESSOR,
SUCCESSOR, INSERT, and DELETE Dictionary and priority queue Basic operations take time proportional to the height of the tree
Complete binary tree: Θ(lg n) Linear-chain tree: Θ(n) Expected height of a randomly built binary search tree: O(lg n)
Networking Laboratory 3/53
Algorithms
Trees
Terminology Root ⇒ no predecessor Leaf ⇒ no successor Interior ⇒ non-leaf Height ⇒ distance from root to leaf
Root node
Leaf nodes
Interior nodes Height
Networking Laboratory 4/53
Algorithms
Types of Binary Trees
Degenerate – only one child Balanced – mostly two children Complete – always two children
Degenerate Binary Tree
Balanced Binary Tree
Complete Binary Tree
Networking Laboratory 5/53
Algorithms
Binary Trees Properties
Degenerate Height = O(n) for n nodes Similar to linear list
Balanced Height = O( lg n ) for n nodes Useful for searches
Degenerate Binary Tree
Balanced Binary Tree
Networking Laboratory 6/53
Algorithms
Binary Search Trees
Key property Value at node
Smaller values in left subtree Larger values in right subtree
Example X > Y X < Z
Y
X
Z
Networking Laboratory 7/53
Algorithms
Binary Search Tree (BST) Binary tree Represented by a linked data structure each node is an object key + satellite data Pointers: left left child, right right child, p parent
Binary search tree property Let x be a node in a BST
If y is a node in the left subtree of x, then key[y] ≤ key[x] If y is a node in the right subtree of x, then key[y] ≥ key[x]
Binary Search Trees
Networking Laboratory 8/53
Algorithms
Binary Search Trees
Examples
Binary Search Trees
Non-Binary Search Tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
Networking Laboratory 9/53
Algorithms
Tree Traversal Tree Traversal
A technique for processing the nodes of a tree in some order
Preorder traversal Process all nodes of a tree by processing the root,
then recursively processing all subtrees Also known as prefix traversal
Networking Laboratory 10/53
Algorithms
Inorder traversal Process all nodes of a tree by recursively processing
the left subtree, then processing the root, and finally the right subtree
Tree Traversal
Networking Laboratory 11/53
Algorithms
Postorder traversal Process all nodes of a tree by recursively processing all subtree
s, then finally processing the root Also known as postfix traversal
Tree Traversal
Networking Laboratory 12/53
Algorithms
a
b c
d ef
g
h i Tree Traversal
Preorder The key of the root of a subtree is printed before the values
in its left subtree and those in its right subtree a, b, d, e, c, f, h, i, g
Inorder The key of the root of a subtree is printed between the values
in its left subtree and those in its right subtree d, b, e, a, h, f, i, c, g
Postorder The key of the root of a subtree is printed after the values
in its left subtree and those in its right subtree d, e, b, h, i, f, g, c, a
Tree Traversal
Networking Laboratory 13/53
Algorithms
Computation Time of INORDER
If x is the root of an n-node subtree, then the call INORDER-TREE-WALK(x)takes Θ(n) time Use the substitution method T(n) = T(k) + T(n-k-1) + d; T(0)=c Show that T(n) = Θ(n)
by proving that T(n) = (c+d)n+c For n=0 (c+d)*0 + c = c For n > 0
• T(n) = T(k) + T(n-k-1) + d = ((c+d) *k+c)+((c+d) *(n-k-1)+c) + d= (c+d) *n+c-(c+d)+c+d = (c+d) *n + c
Networking Laboratory 14/53
Algorithms
Searching
Networking Laboratory 15/53
Algorithms
Example Binary Searches
Find ( 2 )
5
10
30
2 25 45
5
10
30
2
25
4510 > 2, left
5 > 2, left
2 = 2, found
5 > 2, left
2 = 2, found
Networking Laboratory 16/53
Algorithms
Example Binary Searches
Find ( 25 )
5
10
30
2 25 45
5
10
30
2
25
4510 < 25, right
30 > 25, left
25 = 25, found
5 < 25, right
45 > 25, left
30 > 25, left
10 < 25, right
25 = 25, found
Networking Laboratory 17/53
Algorithms
Practice Problems Suppose that we have numbers between 1 and 1000 in a
binary search tree and want to search for the number 363.Which of the following sequences could NOT be thesequence of nodes examined?
a. 2, 252, 401, 398, 330, 344, 397, 363. b. 924, 220, 911, 244, 898, 258, 362, 363. c. 925, 202, 911, 240, 912, 245, 363.
Networking Laboratory 18/53
Algorithms
Minimum and Maximum
The element with the minimum/maximum key can be found by following left/right child pointersfrom the root until NIL is encountered
Networking Laboratory 19/53
Algorithms
Successor And Predecessor
Assume distinct keys The successor of a node x is the node with the
smallest key greater than key[x] Right subtree nonempty successor is the leftmost
node in the right subtree (Line 2) Otherwise, the successor is the lowest ancestor of x
whose left child is also an ancestor of x go up the treeuntil we encounter a node that is the left child of its parent (Lines 3-7)
Networking Laboratory 20/53
Algorithms
TREE-SUCCESSOR(X)
Networking Laboratory 21/53
Algorithms
Examples
Networking Laboratory 22/53
Algorithms
Insertion
Trace down the tree until a leaf
Should be inserted in the left subtreeShould be inserted in the right subtree
Networking Laboratory 23/53
Algorithms
Insert ( 20 )
5
10
30
2 25 45
10 < 20, right
30 > 20, left
25 > 20, left
Insert 20 on left
20
Insertion Example
Networking Laboratory 24/53
Algorithms
Insertion Example
12
18
1915
1713
5
92
Insert an item with key 13into a BST
12
18
15
Networking Laboratory 25/53
Algorithms
Deletion
Consider three cases for the node z to be deleted z has no children: modify p[z] to replace z with NIL as its child z has only one child: splice out z by making a new link between its
child and its parent z has two children: splice z’s successor y,
which has no left child and replace z’s key and satellite data with y’s key and satellite
Networking Laboratory 26/53
Algorithms
Deletion Example
Networking Laboratory 27/53
Algorithms
Deletion Example
Networking Laboratory 28/53
Algorithms
TREE-DELETE Algorithm
Networking Laboratory 29/53
Algorithms
TREE-DELETE Algorithm
TREE-DELETE Algorithm Lines 1-3: determines a node y to splice out
(z itself or z’s successor) Lines 4-6: x is set to the non-NIL child of y,
or to NIL if y has no children Lines 7-13: splice out y by modifying pointers
in p[y] and x Special care for when x=NIL or when y is the root
Lines 14-16: if the successor of z was the node spliced out, y’s key and satellite data are moved toz, overwriting the previous key and satellite data
Line 17: return node y for recycle it via the free list
Networking Laboratory 30/53
Algorithms
Example Deletion (Leaf)
Delete ( 25 )
5
10
30
2 25 45
10 < 25, right
30 > 25, left
25 = 25, delete
5
10
30
2 45
Networking Laboratory 31/53
Algorithms
Example Deletion (Internal Node)
Delete ( 10 )
5
10
30
2 25 45
5
25
30
2 25 45
5
25
30
2 45
Replacing 10 with smallestvalue in right
subtree
Deleting leaf Resulting tree
Networking Laboratory 32/53
Algorithms
Example Deletion (Internal Node)
Delete ( 10 )
5
10
30
2 25 45
5
5
30
2 25 45
2
5
30
2 25 45
Replacing 10 with largestvalue in left
subtree
Replacing 5 with largestvalue in left
subtree
Deleting leaf
Networking Laboratory 33/53
Algorithms
AVL Trees (Adelson-Velskii and Landis)
BST : all values in T1 ≤ x ≤ all values in T2
T1, T2 are AVL trees and height(T1) - height(T2) ≤ 1
2T1T
x
AVL Trees
Networking Laboratory 34/53
Algorithms
AVL Tree Property: It is a BST in which the heights of the left and right subtrees of the root differ by at most 1and in which the right and left subtrees are also AVL trees
Example: Select a AVL tree !
AVL Trees
OK NONetworking Laboratory 35/53
Algorithms
Very much like as in BST with operations to maintain height balance-rotation code associated with each node-2, -1, 0, 1, 2
Insertion
-2
h+2h
-1
h+1h
0
h h
+2
hh+2
+1
hh+1
Networking Laboratory 36/53
Algorithms
4 Types Single Left Rotation Double Left Rotation Single Right Rotation Double Right Rotation
SLR, DLR : left ( +2 ) SRR, DRR : right ( -2 )
Rotation
Networking Laboratory 37/53
Algorithms
Case 1
Case 2
Rotation-2
h+2h
+2
hh+2
Networking Laboratory 38/53
Algorithms
Case 1 (a)
Rotation
T3
C -2
h+1
h
A-1
T2
hT3
A
T1
0
h+1 h
C 0
T2
h
SRR
T1
Networking Laboratory 39/53
Algorithms
Case 1 (a)`
Rotation
T3
C -2
h-1
A0
T2
h
T3
A 1
hh-1
C -1
h
SRR
h
T1 T2
T1
Networking Laboratory 40/53
Algorithms
Case 1 (b)
Rotation
T4
C -2
h+1
A+1
T2
DRR
B -1
T3
hh+1
T1
h+1
T4
B 0
h+1
A0
T2
C+1
T3
hh+1
T1
h+1
h
hh
h hhhh
0
0
Networking Laboratory 41/53
Algorithms
Case 2 (a)
Rotation
T1
A +2
h or h+1
h
C 0 or 1
T2
hT2
C
h
A 0
T1
h
SLR
T3
h or h+1T3
-1 or 0
Networking Laboratory 42/53
Algorithms
Case 2 (b)
DLR = SRR at C + SLR at A DRR = SLR at A + SRR at C
Rotation
T1
A +2
h
C -1
T2
DLRB
T3
hh
T4
hT4
B 0
h
A0
T2
C0
T3
hh
T1
h
Networking Laboratory 43/53
Algorithms
5 6 9 8 10 7 4 1 3 2
Example
5
SLR
5
6
+25
6
9
+15
6
9
5
6
9
8
5
6
9
8 10
+1-15
6
9
8 10
7
+2DLR
Networking Laboratory 44/53
Algorithms
5 6 9 8 10 7 4 1 3 2
Example
-15
6
9
8 10
7
+2DLR 6
8
9
5 107
6
8
9
5 107
4
6
8
9
5 107
4
1
-2-1
SRR6
8
9
4 107
1 5
Networking Laboratory 45/53
Algorithms
5 6 9 8 10 7 4 1 3 2
Example
SRR
4
8
9
1 10
7
6
3 5
DLR
4
8
9
2 10
7
6
3 51
-2 6
8
9
4 107
1 5
3
-1
4
8
9
1 10
7
6
3 5
2
+2-1
Networking Laboratory 46/53
Algorithms
Example
Networking Laboratory 47/53
Algorithms
Insert at leaf Keep going upward and update code from leaf to
parent if
Stop when
Rotate when
Insertion Strategy
0
0+1-1
+1-1
0
+1-1
+2-2
Networking Laboratory 48/53
Algorithms
Insertion Strategy
+2
-1
+2
+1
-2
+1
-2
-1
Double Single
Left
Right
Networking Laboratory 49/53
Algorithms
Delete the node as in BST deletion Keep going upward and update code from leaf to
parent if
Stop when
Rotate when
Deletion Strategy
0+1-1
+1
-10
+1-1
+2-2
Networking Laboratory 50/53
Algorithms
Example
SLR
Delete 5
-1
6
10
12
5 118
7 9
+10
-1
6
10
12
118
7 9
-1+20
8
10
12
1196
7
Delete 10
Networking Laboratory 51/53
Algorithms
Example
SRR
8
10
12
1196
7
Delete 10
8
11
12
96
7
8
9
12
116
7
-2-1
-2
+1 DRR
Networking Laboratory 52/53
Algorithms
Example
SRR
8
11
12
96
7
8
9
12
116
7
-2-1
-2
+1 DRR
7
9
12
116 8
6
8
11
7 129
Networking Laboratory 53/53