• No results found

Realization with an AVL-Tree

In document Computer Science (Page 88-93)

Data Structures

4.4.5 Realization with an AVL-Tree

An AVL-tree is a search tree T with the following additional restrictions:

Binary property. T is a binary tree, that is, every internal node has two children (left and right child), and stores one key.

Balance property. For every internal node, the heights of the subtrees rooted at the children of  differ at most by one.

13 14 15

10 12

11

(a)

13 14 15

10 12

= '

(b)

13 14 15

10

"

12

(c) º

º

º

FIGURE 4.9 Example of node merge in a 2–4 tree: (a) initial configuration, (b) the removal of an element from dictionary D() causes an underflow at node , and (c) merging node  = into its sibling.

13

12 15

(a)

10

(b)

14

12 13 15

14 10

''

FIGURE 4.10 Example of subsequent node merge in a 2–4 tree: (a) overflow at nodeand (b) final configuration after splitting node.

TABLE 4.10 Performance of a Dictionary Realized by an (a, b)-Tree

Operation Time

SIZE O(1)

FIND O(log N)

LOCATEPREV O(log N)

LOCATENEXT O(log N)

NEXT O(log N)

PREV O(log N)

MIN O(1)

MAX O(1)

INSERT O(log N)

REMOVE O(log N)

MODIFY O(log N)

35 10

21

51

46 77

80

92 +1

+1 −1

0

0

0 0

0

FIGURE 4.11 Example of AVL-tree storing nine elements. The keys are shown inside the nodes, and the balance factors (see subsequent section on rebalancing) are shown next to the nodes.

An example of AVL-tree is shown in Figure 4.11. The height of an AVL-tree storing N elements is O(log N). This can be shown as follows. Let Nh be the minimum number of elements stored in an AVL-tree of height h. We have N0= 0, N1= 1, and

Nh= 1 + Nh−1+ Nh−2, for h≥ 2

The preceding recurrence relation defines the well-known Fibonacci numbers. Hence, Nh= (N), where

 = (1 +√

5)/2 = 1.6180 · · · is the golden ratio.

The realization of a dictionary with an AVL-tree extends that with a search tree. Namely, the implemen-tation of operations INSERTand REMOVEmust be modified to preserve the binary and balance properties after an insertion or deletion.

4.4.5.1 Insertion

The implementation of INSERTfor search trees given earlier in this section adds the new element to an existing node. This violates the binary property, and hence cannot be done in an AVL-tree. Hence, we modify the three cases of the INSERTalgorithm for search trees as follows:

r Case x= x: an element with key x already exists, and we return a null locator c .

r Case x = xand is a leaf: we replace  with a new internal node  with two leaf children, store element (x, y) in, and return a locator c to (x, y).

r Case x= xand is an internal node: we set  =  and recursively execute the method.

35

10

21 51

46

80

92 +2

+1 −2

0

0 0

77 −1

0 +1

64

1

2

3

T3

T0

T1

T2

FIGURE 4.12 Insertion of an element with key 64 into the AVL-tree ofFigure 4.11. Note that two nodes (with balance factors+2 and −2) have become unbalanced. The dashed lines identify the subtrees that participate in the rebalancing, as illustrated inFigure 4.14.

We have preserved the binary property. However, we may have violated the balance property because the heights of some subtrees of T have increased by one. We say that a node is balanced if the difference between the heights of its subtrees is−1, 0, or 1, and is unbalanced otherwise. The unbalanced nodes form a (possibly empty) subpath of the path from the new internal node to the root of T. See the example of Figure 4.12.

4.4.5.2 Rebalancing

To restore the balance property, we rebalance the lowest node that is unbalanced, as follows:

r Letbe the child of whose subtree has maximum height, and be the child ofwhose subtree has maximum height.

r Let (1,2,3) be the left-to-right ordering of nodes{, ,}, and (T0, T1, T2, T3) be the left-to-right ordering of the four subtrees of{, ,} not rooted at a node in {, ,}.

r Replace the subtree rooted at with a new subtree rooted at 2, where1is the left child of2and has subtrees T0and T1, and3is the right child of2and has subtrees T2and T3.

Two examples of rebalancing are schematically shown in Figure 4.14. Other symmetric configurations are possible. InFigure 4.13we show the rebalancing for the tree of Figure 4.12.

Note that the rebalancing causes all the nodes in the subtree of2to become balanced. Also, the subtree rooted at2now has the same height as the subtree rooted at node before insertion. This causes all of the previously unbalanced nodes to become balanced. To keep track of the nodes that become unbalanced, we can store at each node a balance factor, which is the difference of the heights of the left and right subtrees.

A node becomes unbalanced when its balance factor becomes+2 or −2. It is easy to modify the algorithm for operation INSERTsuch that it maintains the balance factors of the nodes.

4.4.5.3 Deletion

The implementation of REMOVEfor search trees given earlier in this section preserves the binary property, but may cause the balance property to be violated. After deleting a node, there can be only one unbalanced node, on the path from the deleted node to the root of T .

35

FIGURE 4.13 AVL-tree obtained by rebalancing the lowest unbalanced node in the tree ofFigure 4.11. Note that all of the nodes are now balanced. The dashed lines identify the subtrees that participate in the rebalancing, as illustrated inFigure 4.14.

FIGURE 4.14 Schematic illustration of rebalancing a node in the INSERTalgorithm for AVL-trees. The shaded subtree is the one where the new element was inserted. (a) and (b) Rebalancing by means of a single rotation. (c) and (d) Rebalancing by means of a double rotation.

TABLE 4.11 Performance of a Dictionary Realized by an AVL-Tree

Operation Time

SIZE O(1)

FIND O(log N)

LOCATEPREV O(log N)

LOCATENEXT O(log N)

NEXT O(log N)

PREV O(log N)

MIN O(1)

MAX O(1)

INSERT O(log N)

REMOVE O(log N)

MODIFY O(log N)

To restore the balance property, we rebalance the unbalanced node using the previous algorithm, with minor modifications. If the subtrees ofhave the same height, the height of the subtree rooted at2is the same as the height of the subtree rooted at before rebalancing, and we are done. If, instead, the subtrees ofdo not have the same height, then the height of the subtree rooted at2is one less than the height of the subtree rooted at before rebalancing. This may cause an ancestor of 2to become unbalanced, and we repeat the above computation. Balance factors are used to keep track of the nodes that become unbalanced, and can be easily maintained by the REMOVEalgorithm.

4.4.5.4 Complexity

Let T be an AVL-tree storing N elements. The height of T is O(log N). Each dictionary operation affects only the nodes along a root-to-leaf path. Rebalancing a node takes O(1) time. We conclude that each operation takes O(log N) time.

Table 4.11 shows the performance of a dictionary realized with an AVL-tree. In this table we denote with N the number of elements in the dictionary at the time the operation is performed. The space complexity is O(N).

In document Computer Science (Page 88-93)