Binary Search Trees Part-D1 < >

advertisement
Part-D1
Binary Search Trees
6
<
2
1
9
>
4 =
Binary Search Trees
8
1
Binary Search (§ 8.3.3)
• Binary search can perform operation find(k) on a dictionary
implemented by means of an array-based sequence, sorted by key
– at each step, the number of candidate items is halved
– terminates after O(log n) steps
• Example: find(7)
0
1
3
4
5
7
1
0
3
4
5
m
l
0
9
11
14
16
18
m
l
0
8
1
1
3
3
7
19
h
8
9
11
14
16
18
19
8
9
11
14
16
18
19
8
9
11
14
16
18
19
h
4
5
7
l
m
h
4
5
7
l=m =h
Binary Search Trees
2
Binary Search Trees
(§ 10.1)
• A binary search tree is a
binary tree storing keys
(or key-value entries) at
its internal nodes and
satisfying the following
property:
– Let u, v, and w be three
nodes such that u is in the
left subtree of v and w is in
the right subtree of v. We
have
key(u)  key(v)  key(w)
• An inorder traversal of a
binary search trees visits
the keys in increasing
order
6
2
1
9
4
8
• External nodes do not
store items
Binary Search Trees
3
Search (§ 10.1.1)
• To search for a key k, we
trace a downward path
starting at the root
• The next node visited
depends on the outcome
of the comparison of k
with the key of the
current node
• If we reach a leaf, the key
is not found and we
return null
• Example: find(4):
Algorithm TreeSearch(k, v)
if T.isExternal (v)
return v
if k < key(v)
return TreeSearch(k, T.left(v))
else if k = key(v)
return v
else { k > key(v) }
return TreeSearch(k, T.right(v))
<
2
– Call TreeSearch(4,root)
1
Binary Search Trees
6
9
>
4 =
8
4
Insertion
6
<
• To perform operation insert(k,
o), we search for key k (using
TreeSearch)
• Assume k is not already in the
tree, and let w be the leaf
reached by the search
• We insert k at node w and
expand w into an internal
node
• Example: insert 5
2
9
>
1
4
8
>
w
6
2
1
9
4
8
w
5
Binary Search Trees
5
Insertion
6
<
•
To perform operation insert(k, o), we
search for key k (using TreeSearch)
Algorithm TreeINsert(k, x, v):
Input: A search key, an associate value x
and a node v of T to start with
Output: a new node w in the subtree T(v)
that stores the entry (k, x)
W TreeSearch(k,v)
If k=key(w) then
return TreeInsert(k, x, T.left(w))
T.insertAtExternal(w, (k, x))
Return
• Example: insert 5
• Example: insert another 5?
2
9
>
1
4
8
>
w
6
2
1
9
4
8
w
5
Binary Search Trees
6
Deletion
• To perform operation
remove(k), we search for key k
• Assume key k is in the tree,
and let v be the node storing k
• If node v has a leaf child w, we
remove v and w from the tree
with operation
removeExternal(w), which
removes w and its parent
• Example: remove 4
6
<
2
9
>
4 v
1
8
w
5
6
2
1
Binary Search Trees
9
5
8
7
Deletion (cont.)
• We consider the case where the
key k to be removed is stored at
a node v whose children are
both internal
– we find the internal node w
that follows v in an inorder
traversal
– we copy key(w) into node v
– we remove node w and its left
child z (which must be a leaf)
by means of operation
removeExternal(z)
• Example: remove 3
1
v
3
2
8
6
w
5
z
1
v
5
2
8
6
Binary Search Trees
9
9
8
Deletion (Another Example)
1
v
3
2
8
6
w
9
4
z
5
1
v
4
2
8
6
9
5
Binary Search Trees
9
Performance
• Consider a dictionary
with n items
implemented by means
of a binary search tree of
height h
– the space used is O(n)
– methods find, insert and
remove take O(h) time
• The height h is O(n) in
the worst case and O(log
n) in the best case
Later, we will try to keep h =O(log n).
Binary Search Trees
10
AVL Trees
6
v
8
3
z
4
AVL Trees
11
AVL Tree Definition
• AVL trees are balanced.
• An AVL Tree is a binary
search tree such that
for every internal node
v of T, the heights of the
children of v can differ
by at most 1.
44
4
2
17
78
1
3
2
32
88
50
1
48
62
1
An example of an AVL tree where the
heights are shown next to the nodes:
AVL Trees
12
1
6
4
8
5
1
7
3
11
AVL tree
6
4
1
8
5
7
11
3
2
not an AVL tree
TCSS 342 AVL Trees v1.0
13
AVL Tree
-1
0
0
0
0
-1
0
0
AVL Tree
-2
1
0
0
AVL Tree
0
-1
0
1
Not an AVL Tree
n(2)
Height of an AVL Tree
3
4
n(1)
• Fact: The height of an AVL tree storing n keys is O(log n).
• Proof: Let us bound n(h): the minimum number of internal nodes
of an AVL tree of height h.
• We easily see that n(1) = 1 and n(2) = 2
• For n > 2, an AVL tree of height h contains the root node, one AVL
subtree of height n-1 and another of height n-2 (or n-1).
– That is, n(h)  1 + n(h-1) + n(h-2)
• Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So
n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(h-6), … (by induction),
n(h) > 2in(h-2i)
• Solving the base case we get: n(h) > 2 (h/2)-1
• Taking logarithms: h < 2log n(h) +2
• Thus the height of an AVL tree is O(log n)
AVL Trees
15
Insertion in an AVL Tree
• Insertion is as in a binary search tree
• Always done by expanding an external node.
• Example:
44
44
17
78
17
78
c=z
a=y
32
50
48
88
62
32
50
88
48
62
b=x
54
w
before insertion
after insertion
AVL Trees
16
Names of important nodes
• w: the newly inserted node. (insertion process follow the binary search
tree method)
• The heights of some nodes in T might be increased after inserting a node.
– Those nodes must be on the path from w to the root.
– Other nodes are not effected.
• z: the first node we encounter in going up from w toward the root such
that z is unbalanced.
• y: the child of z with higher height.
– y must be an ancestor of w. (why? Because z in unbalanced after inserting w)
• x: the child of y with higher height.
– x must be an ancestor of w.
– The height of the sibling of x is smaller than that of x. (Otherwise, the height
of y cannot be increased.)
– See the figure in the last slide.
Binary Search Trees
17
Algorithm restructure(x):
Input: A node x of a binary search tree T that has both
parent y and grand-parent z.
Output: Tree T after a trinode restructuring.
1. Let (a, b, c) be the list (increasing order) of nodes x, y,
and z. Let T0, T1, T2 T3 be a left-to-right (inorder) listing
of the four subtrees of x, y, and z not rooted at x, y, or z.
2. Replace the subtree rooted at z with a new subtree
rooted at b..
3. Let a be the left child of b and let T0 and T1 be the left
and right subtrees of a, respectively.
4. Let c be the right child of b and let T2 and T3 be the left
and right subtrees of c, respectively.
Binary Search Trees
18
Restructuring
(as Single Rotations)
• Single Rotations:
a=z
single rotation
b=y
c=x
T0
T1
T3
T2
c=z
a=z
T0
single rotation
b=y
b=y
c=x
T1
T3
T2
b=y
a=x
c=z
a=x
T0
T1
T2
T3
T0
Binary Search Trees
T1
T2
T3
19
Restructuring
(as Double Rotations)
• double rotations:
double rotation
a=z
c= y
b=x
a=z
c= y
b=x
T0
T2
T1
T3
T0
T1
double rotation
c=z
a=y
T2
T3
b=x
a=y
c=z
b=x
T0
T1
T3
T0
T1
T2
T3
T2
Binary Search Trees
20
Insertion Example, continued
44
2
5
z
17
32
3
1
1
1
1
50
2
1
7
78
2y
48
64
3
4
88
x
62
5
T3
54
unbalanced...
T0
T2
T1
44
2
4
3
17
32
...balanced
1
1
1
48
2 y
2
50
4
x
z6
62
3
1
5
78
2
54
7
88
T2
AVL Trees
T0
T1
21
T3
1
Theorem:
–One restructure operation is enough to
ensure that the whole tree is balanced.
–Proof: Look at the four cases on slides
20 and 21. After restructure operation,
the height of the sub-tree is reduced
by one.
Binary Search Trees
22
Removal in an AVL Tree
• Removal begins as in a binary search tree by calling
removal(k) for binary tree.
• may cause an imbalance.
• Example:
44
44
w
17
62
32
50
48
17
62
78
54
50
88
before deletion of 32
Binary Search Trees
48
78
54
88
after deletion
23
Rebalancing after a Removal
• Let z be the first unbalanced node encountered while
travelling up the tree from w. w-parent of the removed
node (in terms of structure, not the name. See Slide 1214)
• let y be the child of z with the larger height,
• let x be the child of y defined as follows;
– If one of the children of y is taller than the other, choose x as the
taller child of y.
– If both children of y have the same height, select x be the child of
y on the same side as y (i.e., if y is the left child of z, then x is the
left child of y; and if y is the right child of z then x is the right child
of y.)
• The way to obtain x, y and z are different from insertion.
Binary Search Trees
24
Rebalancing after a Removal
• We perform restructure(x) to restore balance at z.
• As this restructuring may upset the balance of another
node higher in the tree, we must continue checking for
balance until the root of T is reached
a=z
w
62
44
17
50
48
c=x
78
54
44
b=y
62
17
50
48
88
Binary Search Trees
78
88
54
25
Unbalanced after restructuring
Unbalanced
balanced
a=z
w
h=4
44
17
50
h=3
h=5
c=x
78
62
44
b=y
62
32
1
1
17
h=5
78
50
88
88
Binary Search Trees
26
Rebalancing after a Removal
• We perform restructure(x) to restore balance at z.
• As this restructuring may upset the balance of another
node higher in the tree, we must continue checking for
balance until the root of T is reached
a=z
w
62
44
17
50
48
c=x
78
54
44
b=y
62
17
50
48
88
Binary Search Trees
78
88
54
27
Example a:
• Which node is w? Let us remove node 17.
44
44
17
w
62
32
50
48
32
62
78
54
50
88
before deletion of 32
Binary Search Trees
48
78
54
88
after deletion
28
Rebalancing:
• We perform restructure(x) to restore balance at z.
• As this restructuring may upset the balance of another
node higher in the tree, we must continue checking for
balance until the root of T is reached
a=z
w
62
44
32
50
48
c=x
78
54
44
b=y
62
32
50
48
88
Binary Search Trees
78
88
54
29
Running Times for AVL
Trees
• a single restructure is O(1)
– using a linked-structure binary tree
• find is O(log n)
– height of tree is O(log n), no restructures needed
• insert is O(log n)
– initial find is O(log n)
– Restructuring up the tree, maintaining heights is O(log n)
• remove is O(log n)
– initial find is O(log n)
– Restructuring up the tree, maintaining heights is O(log n)
AVL Trees
30
Download