O(1) space complexity deletion for AVL trees

O(1) space complexity deletion for AVL trees

Information Processing Letters 22 (1986) 147-149 North-Holland 3 March 1986 O(1) SPACE C O M P L E X I T Y D E L E T I O N FOR AVL TREES Lin C H E N...

142KB Sizes 15 Downloads 105 Views

Information Processing Letters 22 (1986) 147-149 North-Holland

3 March 1986

O(1) SPACE C O M P L E X I T Y D E L E T I O N FOR AVL TREES Lin C H E N Computer Science Department, Fudan University, Shanghai, People's Republic of China Communicated by David Gries Received 8 January 1985 Revised 25 April 1985

Keywords: AVL tree, node deletion, algorithmic complexity

I. Introduction

Deleting a node from an AVL tree of n nodes requires time O(log n) (see standard texts such as those by Aho, Hopcroft and Ullman [2], Horowitz and Sahni [4], and Knuth [5]; the notion of AVL tree is due to Adelson-Velskii and Landis [1]). Typically, the operation may require O(log n)

space, because a path of nodes from the root of the tree to the node being deleted must be maintained. Sometimes, this stack is maintained separately; sometimes it is maintained using an extra pointer field in each node on the path (which points to its parent) together with a one-bit field that indicates whether the node is the left or right son of its parent [3].

P

Fig. 1. 0020-0190/86/$3.50 © 1986, Elsevier Science Publishers B.V. (North-Holland)

147

Volume 22, Number 3

INFORMATION PROCESSING LETTERS

This short article outlines a technique that eliminates completely the need for extra space for the stack. The main idea, the same one used in the S c h o r r - W a i t e - D e u t s c h algorithm for marking the nodes of a graph, eliminates the need for the extra pointers. Secondly, the fact that an AVL tree is a binary search tree obviates the need for the extra bit in each node. We only present the idea itself, for the algorithm will be clear to those familiar with the deletion algorithm for AVL trees. The technique should prove useful on other algorithms dealing with binary search trees.

2.

The

idea

Consider the AVL tree of Fig. 1. The AVL tree has the following properties. Each node p (say) has - a value p. v - - in this case, a character,

Fig. 2. (a =false.) 148

3 March 1986

a pointer p . l to its left subtree, a pointer p . r to its right subtree, and - a balance factor (indicated by the integer above the value of the node) with value (height of the left subtree) - (height of the right subtree). Also, the tree is a binary search tree: the value of each node is greater than the values in its left subtree and less than the values in its right subtree. Finally, the balance factor of each node is - 1 , 0, or 1. Suppose the node labeled with value i is to be deleted (see Fig. 1). Typically, a path is maintained from p to the root (in heavy lines in Fig. 1) by keeping a stack of the nodes. The stack is built as the tree is traversed to find i, and it is maintained as the node is deleted and the tree is rotated in some way to reestablish the AVL tree properties. The problem is to maintain this path using only constant extra space. Now look at Fig. 2. It illustrates the idea of maintaining the path from node p to the root by -

-

Volume 22, Number 3

INFORMATION PROCESSING LETTERS

changing a pointer in each node of the path. A simple variable q indicates p's predecessor on the path, q's predecessor on the path is given by its right pointer, and so on. (Each of the pointers that points to a predecessor on the path is called a backpointer.) It is straightforward to change the tree in this fashion and to maintain it as the subtree with root value i is changed to maintain the AVL-tree properties. Before termination of the deletion operation, the fields containing the backpointers must be restored to their original values. This requires retracing the path beginning with node p and ending at the root, and it requires knowing, for each node on thepath, whether the backpointer is in the left or right field. We show how to do this using a single Boolean variable. Let Boolean variable a have meaning " q ' s left pointer field contains the backpointer". (In Fig. 2, this is false, so a =false.)

Suppose this backpointer is in the right subfield, q.right, as in Fig. 2. Then, because of the binary-search-tree property, we can determine which field of node q.r contains the backpointer. If q.v < q . r . v , the backpointer is in the left field q.r.l; otherwise it is in the right field q.r.r. In general, for any node m (say) on the path, if we know which field its backpointer is in, say it is in re.r, then we can determine which field of node m . r contains the backpointer by just comparing the values of nodes m and m . r .

3 March 1986

This simple technique allows one to implement the deletion algorithm for AVL trees in O(log n) time and O(1) space.

Acknowledgment An actual implementation of the deletion algorithm was completed under the direction of Prof. Jiahua Qian. The author also wishes to thank David Gries, Baile Shi and Hong Zhu for their helpful suggestions.

References [1] G.M. Adelson-Velskii and Y.M. Landis, An algorithm for organization of information, Dokl. Akad. Nauk. SSSR 146 (1962) 263-266 (in Russian); English translation in: Soviet Math. Dokl. 3 (1962) 1259-1262. [2] A.V. Aho, J.E. Hopcroft and J.D. Uliman, The Design and Analysis of Computer Algorithms (Addison-Wesley, Reading, MA, 1974). [3] C.A. Crane, Linear lists and priority queus as balanced trees, Ph.D. Thesis STAN-CS-72-259, Computer Science Dept., Stanford Univ., 1972. [4] S. Horowitz and S. Sahni, Fundamentals of Computer Algorithms (Computer Science Press, Potomac, MD, 1978). [5] D.E. Knuth, The Art of Computer Programming, Vol. I: Fundamental Algorithms (Addison-Wesley, Reading, MA, 2nd ed., 1973).

149