Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL) O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 1 10.2 AVL Tree 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 An example of an AVL tree where the heights are shown next to the nodes
54
Embed
10.2 AVL Treeelearning.kocw.net/KOCW/document/2013/youngnam/YoungTak...Advanced Networking Tech. Lab. Yeungnam University (YU -ANTL) O-O Programming & Data Structure DS 10 - 1 Prof.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 1
10.2 AVL Tree
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
An example of an AVL tree where the heights are shown next to the nodes
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 2
Height of an AVL Tree
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. 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
Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n)
3
4 n(1)
n(2)
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 3
Update Operations
Insertion An example insertion of an entry with key 54 search-and-repair to restore the balance
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 4
Trinode Restructuring
Algorithm resturcture(x)
Algorithm restructure(x): Input: A node x of a binary search tree T that has both a parent y
and a grandparent z Output: Tree T after a trinode restructuring (which corresponds to a
single or double rotation) involving nodes x, y, and z 1: Let (a,b,c) be a left-to-right (inorder) listing of the nodes x, y, and z,
and let (T0,T1,T2,T3) be a left-to-right (inorder) listing of the four subtrees of x, y, and z 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.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 5
Trinode Restructuring (1)
let (a,b,c) be an inorder listing of x , y, z perform the rotations needed to make b the topmost node
of the three
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 6
Trinode Restructuring (2)
double rotation: RL, LR
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 7
Update Operation - Removal
Removal begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, w, may cause an imbalance.
Example:
Fig. 10. 8 Before removal
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 8
Rebalancing after a Removal
Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height
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
44
17
78 50
88 48
62
54
w
c=x
b=y
a=z
44
17
78
50 88
48
62
54
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 9
Performance of AVL Trees
Performance of an n-entry map realized by an AVL tree
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 10
C++ Implementation of an AVL Tree
AVLEntry
template <typename E> class AVLEntry : public E { // an AVL entry private:
int ht; // node height protected: // local types
typedef typename E::Key K; // key type typedef typename E::Value V; // value type int height() const { return ht; } // get height void setHeight(int h) { ht = h; } // set height
public: // public functions AVLEntry(const K& k = K(), const V& v = V()) // constructor : E(k,v), ht(0) { } friend class AVLTree<E>; // allow AVLTree access
};
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 11
class AVLTree (1) template <typename E> // an AVL tree class AVLTree : public SearchTree< AVLEntry<E> > { public: // public types
typedef AVLEntry<E> AVLEntry; // an entry typedef typename SearchTree<AVLEntry>::Iterator Iterator; // an iterator
protected: // local types typedef typename AVLEntry::Key K; // a key typedef typename AVLEntry::Value V; // a value typedef SearchTree<AVLEntry> ST; // a search tree typedef typename ST::TPos TPos; // a tree position
public: // public functions AVLTree(); // constructor Iterator insert(const K& k, const V& x); // insert (k,x) void erase(const K& k) throw(NonexistentElement); // remove key k entry void erase(const Iterator& p); // remove entry at p
protected: // utility functions int height(const TPos& v) const; // node height utility void setHeight(TPos v); // set height utility bool isBalanced(const TPos& v) const; // is v balanced? TPos tallGrandchild(const TPos& v) const; // get tallest grandchild void rebalance(const TPos& v); // rebalance utility
};
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 12
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 22
Example of Execution in Rebalancing in AVL Tree
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 23
10.3 Splay Trees
Splay Tree A splay tree is a self-adjusting binary search tree with the additional
property that recently accessed elements are quick to access again. It performs basic operations such as insertion, look-up and removal in O(log n) amortized time. For many sequences of non-random operations, splay trees perform better than other search trees, even when the specific pattern of the sequence is unknown. The splay tree was invented by Daniel Dominic Sleator and Robert Endre Tarjan in 1985.[1]
All normal operations on a binary search tree are combined with one basic operation, called splaying. Splaying the tree for a certain element rearranges the tree so that the element is placed at the root of the tree. One way to do this is to first perform a standard binary tree search for the element in question, and then use tree rotations in a specific fashion to bring the element to the top. Alternatively, a top-down algorithm can combine the search and the tree reorganization into a single phase.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 24
Splaying splay x by moving x to the root of T through a sequence of restructuring
splay tree splay tree is a binary search tree where a node is splayed after it is
accessed (for a search or update) deepest internal node accessed is splayed splaying costs O(h), where h is height of the tree – which is still O(n)
worst-case – O(h) rotations, each of which is O(1)
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 25
Zig-Zig : The node x and its parent y are both left children or both right children. We replace z by x, making y a child of x and z a child of y, while
maintaining the in-order relationships of the nodes in T.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 26
zig-zag: One of x and y is a left child and the other is a right child. In this case, we replace z by x and make x have y and z as its children,
while maintaining the in-order relationships of the nodes in T.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 27
zig: x does not have a grandparent (or we are not considering x’s
grandparent for some reason). In this case, we rotate x over y, making x’s children be the node y and
one of x’s former children w, in order to maintain the relative inorder relationships of the nodes in T.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 28
Example of splaying a node (Fig. 10.16)
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 29
When to splay (1) When searching for key k, if k is found at a node x, we splay x, else we
splay the parent of the external node at which the search terminates unsuccessfully. For example, the splaying in Figures 10.16 and 10.17 would be performed after searching successfully for key 14 or unsuccessfully for key 14.5.
When inserting key k, we splay the newly created internal node where k gets inserted. For example, the splaying in Figures 10.16 and 10.17 would be performed if 14 were the newly inserted key. We show a sequence of insertions in a splay tree in Figure 10.18.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 30
When to splay (2) When deleting a key k, we splay the parent of the node w that gets
removed, that is, w is either the node storing k or one of its descendents. (Recall the removal algorithm for binary search trees.) An example of splaying following a deletion is shown in Figure 10.19.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 31
Performance of Splay Trees
Recall: rank of a node is logarithm of its size. Thus, amortized cost of any splay operation is
O(log n) In fact, the analysis goes through for any
reasonable definition of rank(x) This implies that splay trees can actually adapt to
perform searches on frequently-requested items much faster than O(log n) in some cases
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 32
10.4 (2, 4) Trees
Multi-way Search Trees multi-way trees are defined so that each internal node can have
many children Let v be a node of an ordered tree. We say that v is a d-node if v
has d children. We define a multi-way search tree to be an ordered tree T
that has the following properties, which are illustrated in Figure 10.1(a): Each internal node of T has at least two children. That is, each
internal node is a d-node such that d ≥ 2. Each internal d-node v of T with children v1, . . . ,vd stores an ordered
set of d−1 key-value entries (k1,x1), . . ., (kd−1,xd−1), where k1 ≤ ··· ≤ kd−1. Let us conventionally define k0 = −∞ and kd = +∞. For each entry (k,x)
stored at a node in the subtree of v rooted at vi, i = 1, . . . ,d, we have that ki−1 ≤ k ≤ ki.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 33
A multi-way search tree is an ordered tree such that Each internal node has at least 2 children and stores d −1 key-
element items (ki, oi), where d is the number of children For a node with children v1 v2 … vd storing keys k1 k2 … kd−1 keys in the subtree of v1 are less than k1 keys in the subtree of vi are between ki−1 and ki (i = 2, …, d − 1) keys in the subtree of vd are greater than kd−1
The leaves store no items and serve as placeholders
11 24
2 6 8 15
30
27 32
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 34
Multi-Way Inorder Traversal We can extend the notion of inorder traversal from binary trees to multi-
way search trees Namely, we visit item (ki, oi) of node v between the recursive traversals
of the subtrees of v rooted at children vi and vi + 1 An inorder traversal of a multi-way search tree visits the keys in
increasing order
11 24
2 6 8 15
30
27 32
1 3 5 7 9 11 13 19
15 17
2 4 6 14 18
8 12
10
16
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 35
Multi-Way Searching
Similar to search in a binary search tree A each internal node with children v1 v2 … vd and
keys k1 k2 … kd−1 k = ki (i = 1, …, d − 1): the search terminates successfully k < k1: we continue the search in child v1 ki−1 < k < ki (i = 2, …, d − 1): we continue the search in child vi k > kd−1: we continue the search in child vd
Reaching an external node terminates the search unsuccessfully
Example: search for 30
11 24
2 6 8 15
30
27 32
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 36
Searching in a multi-way tree
search path for key 12
search path for key 24
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 37
(2,4) Trees
A (2,4) tree (also called 2-4 tree or 2-3-4 tree) is a multi-way search with the following properties Node-Size Property: every internal node has at most 4 children Depth Property: all the external nodes have the same depth
Depending on the number of children, an internal node of a (2,4) tree is called a 2-node, 3-node or 4-node
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 38
Height of a (2,4) Tree
Theorem: A (2,4) tree storing n items has height O(log n) Proof: Let h be the height of a (2,4) tree with n items Since there are at least 2i items at depth i = 0, … , h − 1 and no items at
depth h, we have n ≥ 1 + 2 + 4 + … + 2h−1 = 2h − 1
Thus, h ≤ log (n + 1)
Searching in a (2,4) tree with n items takes O(log n) time
1
2
2h−1
0
items 0
1
h−1
h
depth
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 39
Update Operations for (2, 4) Trees - Insertion
We insert a new item (k, x) at the parent v of the leaf reached by searching for k We preserve the depth property but We may cause an overflow (i.e., node v may become a 5-node)
Example: inserting key 30 causes an overflow
27 32 35
10 15 24
2 8 12 18
10 15 24
2 8 12 27 30 32 35 18
v
v
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 40
Overflow and Split
We handle an overflow at a 5-node v with a split operation: let v1 … v5 be the children of v and k1 … k4 be the keys of v node v is replaced nodes v' and v" v' is a 3-node with keys k1 k2 and children v1 v2 v3 v" is a 2-node with key k4 and children v4 v5
key k3 is inserted into the parent u of v (a new root may be created)
The overflow may propagate to the parent node u
15 24
12 27 30 32 35 18 v
u
v1 v2 v3 v4 v5
15 24 32
12 27 30 18 v'
u
v1 v2 v3 v4 v5
35 v"
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 41
Fig. 10.22 – Node Split (a) overflow at a 5-node v; (b) the third key of v inserted into the parent u of v; (c) node v replaced with a 3-node v′ and a 2-node v′′.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 42
Fig. 10.23 – A sequence of insertion into (2, 4) tree (a) initial tree with one entry; (b) insertion of 6; (c) insertion of 12; (d) insertion of 15, which causes an overflow; (e) split, which causes the creation of a new root node; (f) after the split;
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 43
Fig. 10.23 – A sequence of insertion into (2, 4) tree (g) insertion of 3; (h) insertion of 5, which causes an overflow; (i) split; (j) after the split;
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 44
Fig. 10.23 – A sequence of insertion into (2, 4) tree (k) insertion of 10; (l) insertion of 8.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 45
Fig. 10.24 – An insertion in a (2, 4) tree that causes a cascading split
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 9- 46
Analysis of Insertion
Algorithm insert (k , e) 1. We search for key k to
locate the insertion node v
2. We add the new entry (k , e) at node v
3. while overflow (v) if isRoot(v) create a new empty root
above v v ← split(v)
Let T be a (2,4) tree with n items Tree T has O(log n) height Step 1 takes O(log n) time
because we visit O(log n) nodes
Step 2 takes O(1) time Step 3 takes O(log n) time
because each split takes O(1) time and we perform O(log n) splits
Thus, an insertion in a (2,4) tree takes O(log n) time
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 47
Removal
We reduce deletion of an entry to the case where the item is at the node with leaf children
Otherwise, we replace the entry with its inorder successor (or, equivalently, with its inorder predecessor) and delete the latter entry
Example: to delete key 24, we replace it with 27 (inorder successor)
27 32 35
10 15 24
2 8 12 18
32 35
10 15 27
2 8 12 18
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 48
Underflow and Fusion
Deleting an entry from a node v may cause an underflow, where node v becomes a 1-node with one child and no keys
To handle an underflow at node v with parent u, we consider two cases
Case 1: the adjacent siblings of v are 2-nodes Fusion operation: we merge v with an adjacent sibling w and move an
entry from u to the merged node v' After a fusion, the underflow may propagate to the parent u
9 14
2 5 7 10
u
v 9
10 14
u
v' w 2 5 7
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 49
Underflow and Transfer
To handle an underflow at node v with parent u, we consider two cases
Case 2: an adjacent sibling w of v is a 3-node or a 4-node Transfer operation: 1. we move a child of w to v 2. we move an item from u to v 3. we move an item from w to u After a transfer, no underflow occurs
4 9
6 8 2
u
v w 4 8
6 2 9
u
v w
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 50
A sequence of removal from a (2, 4) tree (a) removal of 4, causing an underflow; (b) a transfer operation; (c) after the transfer operation; (d) removal of 12, causing an underflow;
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 51
Fig. 10. 25 - A sequence of removal from a (2, 4) tree (e) a fusion operation; (f) after the fusion operation; (g) removal of 13; (h) after removing 13.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 52
Fig. 10.26 – A propagating sequence of fusions in a (2, 4) tree (a) removal of 14, which causes an underflow; (b) fusion, which causes another underflow; (c) second fusion operation, which causes the root to be removed; (d) final tree.
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 53
Analysis of Deletion
Let T be a (2,4) tree with n items Tree T has O(log n) height
In a deletion operation We visit O(log n) nodes to locate the node from which
to delete the entry We handle an underflow with a series of O(log n)
fusions, followed by at most one transfer Each fusion and transfer takes O(1) time
Thus, deleting an item from a (2,4) tree takes O(log n) time
Advanced Networking Tech. Lab. Yeungnam University (YU-ANTL)
O-O Programming & Data Structure Prof. Young-Tak Kim DS 10 - 54