• No results found

Efficient Algorithms to Globally Balance a Binary Search Tree

N/A
N/A
Protected

Academic year: 2021

Share "Efficient Algorithms to Globally Balance a Binary Search Tree"

Copied!
8
0
0

Loading.... (view fulltext now)

Full text

(1)

Programming Techniques and Data Structures Ellis Horowitz Editor

Efficient Algorithms to

Globally Balance a Binary

Search Tree

HSl CHANG and S. SlTHARAMA IYENGAR

ABSTRACT: A binary search tree can be globally balanced by readjustment of pointers or with a sorting process in O(n) time, n being the total number of nodes. This paper presents three global balancing algorithms, one of which uses folding with the other two adopting parallel procedures. These algorithms show improvement in time efficiency over some sequential algorithms [1, 2, 7] when applied to large binary search trees. A comparison of various algorithms is presented.

1. INTRODUCTION

A binary search tree is organized such that for any node, all keys in the left subtree are smaller and those in the right subtree are greater than the key value of that node. It provides a method of data organization which is both flexible and efficient.

The formation of binary trees grown in a random manner may have some branches longer than others. We know a binary tree search will, on the average,

require (2(n + 1)/n)H, - 3 (~1.386 log2n) comparisons

for a tree with n nodes if the keys are inserted into the tree in random order. Thus, by making extra efforts to maintain a perfectly balanced tree instead of a random tree, we could--provided all keys are looked up with equal probability--expect an average improvement of approximately 27.85 percent in the search path length [3, 7].

The task of balancing a binary search tree is then to adjust the left and right pointers of all the nodes in the tree so that the search path is optimized; we can attain © 1984 ACM 0001-0782/84/0700-0895 75¢

the same effect as a binary search without requiring a sorted list. Algorithms that dynamically restore tree structure to balance during insertion or deletion of nodes have been described by Adelson-Velskii, Landis, and Knuth [3, 6]. However, in this paper, we deal with global algorithms that balance the entire tree at one time. A global tree balancing algorithm generally runs in linear time and consists of two parts: first, a traversal to determine the order of all nodes, then restructuring pointers based on that order.

Martin and Ness [7] developed an algorithm that re- organizes a tree with n nodes by repetitively subdivid- ing n by 2 and uses the results as guidance to step through the framework of a perfectly balanced tree, i.e., for each node, the n u m b e r of nodes in its left subtree and right subtree differ by I at most. An in-order trav- ersal is carried out concurrently to provide relative node positions for the pointer-restructuring procedure which fits them into proper places in the balanced tree structure. In this algorithm, a stack is required to save pointers during traversal.

Bentley [1] discusses an algorithm to produce per- fectly balanced trees. In this algorithm, nodes are passed as a set or a linked list to the balancing proce- dure which finds the median element of the set of nodes as root and splits the set into two subsets, each forming a balanced subtree at the next level. This proc- ess continues until there are no more elements to split.

Another algorithm that needs no extra storage was given by Day [2]. The input tree is required to be right- threaded with negative backtrack pointers to allow

(2)

A l g o r i t h m S: { g l o b a l l y b a l a n c e a b i n a r y s e a r c h t r e e t h r o u g h f o l d i n g l p r o c e d u r e B A L A N C E ( R O O T , L S O N , R S O N , n ) : i n t e g e r R O O T , n; i n t e g e r a r r a y L S O N , R S O N ; b e g i n i n t e g e r N, M, A N S L , A N S R ; i n t e g e r a r r a y L I N K ( I : n ) ; l t r a v e r s e the o r i g i n a l t r e e a n d set u p L I N K } p r o c e d u r e T R A V B I N D ( T ) : i n t e g e r T; { p o i n t e r to t h e n e x t n o d e to be v i s i t e d } b e g i n i f T = n u l l t h e n r e t u r n ; T R A V B I N D ( L S O N ( T ) ) ; N ~ N + 7; { c o u n t t h e s e q u e n c e of v i s i t } L I N K ( N ) ~ T; { s t o r e the p o i n t e r to the N t h n o d e in t h e N t h e l e m e n t of L I N K 1 T R A V B I N D ( R S O N ( T ) ) ; end; { r e o r g a n i z e a t r e e by p a r t i t i o n i n g a n d f o l d i n g } p r o c e d u r e G R O W ( L O W , H I G H ) : i n t e g e r L O W , H I G H ; b e g i n { M I D is t h e m e d i a n of a s u b s e t b o u n d by L O W a n d H I G H , T L is t h e s u b t r e e r o o t in t h e b a l a n c e d l e f t h a l f - t r e e , T R is t h e c o u n t e r p a r t of T L in t h e r i g h t h a l f - t r e e . TL, T R a r e r e t u r n e d v i a A N S L , A N S R , r e s p e c t i v e l y } i n t e g e r M I D , TL, TR; c a s e L O W > H I G H Inull b r a n c h I : b e g i n A N S L , A N S R ~ n u l l ; end; L O W = H I G H Ileafl : b e g i n A N S L ~ L I N K ( L O W ) ; A N S R ~ L I N K ( L O W + M ) ; L S O N ( A N S L ) , R S O N ( A N S L ) -- n u l l ; L S O N ( A N S R ) , R S O N ( A N S R ) ~ n u l l ; end; L O W < H I G H { d i v i s i b l e s u b s e t } : b e g i n M I D ~ L ( L O W + H I G H ) / 2~; T L ~ L I N K ( M I D ) ; T R ~ L I N K ( M I D + M ) ; G R O W ( L O W , M I D - I ) ; I f o r m l e f t s u b t r e e 1 L S O N ( T L ) -- A N S L ; L S O N ( T R ) -- A N S R ; G R O W ( M I D + 7 , H I G H ) ; { f o r m r i g h t s u b t r e e } R S O N ( T L ) ~ A N S L ; R S O N ( T R ) ~ A N S R ; A N S L ~ TL; A N S R ~ TR; end; end; end; if n s 2 t h e n r e t u r n ; N ~ 0; { i n i t i a l i z e c o u n t e r } T R A V B I N D ( R O O T ) ; M ~ L ( N + I ) / 2~; { f o l d i n g v a l u e } R O O T - - L I N K ( M ) ; { n e w root} if N = 2 * M t h e n {N is e v e n 1 b e g i n M -- M + I; { a d j u s t f o l d i n g v a l u e } G R O W ( I , M - - 2 ) ; {put t h e n o d e a s s o c i a t e d w i t h M as a t e r m i n a l n o d e l e f t to its i m m e d i a t e s u c c e s s o r l L S O N ( L I N K ( M ) ) , R S O N ( L I N K ( M ) ) -- n u l l ; L S O N ( L I N K ( M + 7) ) -- L I N K ( M ) ; end; e l s e IN is odd} G R O W ( 7 , M - - 7 ) ; L S O N ( R O O T ) ~ A N S L ; R S O N ( R O O T ) ~ A N S R ; end; FIGURE 1. Algorithm S.

(3)

stackless traversal. A right-skewed tree is formed after traversal and this list-like structure serves as a back- bone for the pointer-restructuring procedure which shifts nodes to the left side of the backbone until the subtree path lengths balance, i.e., no path (from root to leaf) differs in length from any other path by more than 1. The produced tree, on the other hand, is not

threaded although it can be made a threaded tree with some modification. Martin and Ness's algorithm and Day's algorithm are given as tutorial matter on pages 700-701 for interested readers. For a broader treatment on balancing binary search trees, see [4-6, 8]. These include the present investigation of the authors.

In the next section, we present a new sequential bal- ancing algorithm which also uses partitioning but re- duces the number of times of partitioning through fold- ing. Section 3 presents two parallel algorithms as varia- tions of the previous sequential algorithm; and in Sec- tion 4, we analyze and compare these algorithms.

2. SEQUENTIAL BALANCING THROUGH FOLDING

The node of a tree is assumed to contain the following fields: left subtree pointer LSON, right subtree pointer RSON, and KEY, while ROOT points to the root node of the tree. We first traverse the original tree to determine the order of all n nodes. Array LINK is used to store the ordering information so that the ith element of LINK contains a pointer to the ith node in ascending key order, i.e., KEY(LINK(i - 1)) < KEY(LINK(i)) < KEY(LINK(i + 1)).

To reorganize a tree by partitioning the set, following Bentley, we first find the left median, 1 t.(n + 1)/2.3, of the whole set; the node corresponding to that ele- ment is to be the root of the new tree. The remaining L(n -- 1 ) / / 2 3 elements with values less than the left median in the left subset and the other r(n - 1)/2"3 elements in the right subset form two partitions. At the next level, we find for each of these two subsets their medians and use the two associated nodes as roots of the two corresponding subtrees as well as LSON and RSON, respectively, of the previous root. This process continues to find the medians at each level, partitions around them, and restructures the tree from top to bot- tom and from left to right.

Further considering the use of LINK, we observe that for trees containing an odd n u m b e r of nodes, the me- dian of an ordered set also serves as a special value, denoted by M, to let us construct the right half-tree concurrently with the left half-tree because the coun- terpart elements in the balanced two halves differ by M. If we know the position of an element K in the left half-tree, we can also determine the position of its counterpart element K + M in the right half-tree. So, we only have to partition half of the entire set in order to reorganize the tree structure.

We call this procedure folding because it is like some- thing bending over upon itself to form a symmetrical

1 If we have n sorted elements X1 . . . X., the median is (X,~.+1)/2., + X,.~/2.~-1)/2, the left median is X,~n+ll/2.,. and the right median is X~/z.,+~.

structure. The value M used in folding is called the folding value. When the total n u m b e r of nodes is even,

we let the right median, n / 2 + 1, be the folding value M. The node associated with the left median remains as the root of the tree and the node corresponding to M is placed as the leftmost node in the right half-tree.

Algorithm S (Figure 1) is a global balancing algorithm based on this idea. Procedure TRAVBIND traverse the original tree and sets up the auxiliary array LINK. After traversal, procedure GROW recursively partitions and forms the balanced left half-tree with its right-half counterpart built in the same step through folding. Two parameters are used in GROW: LOW, the lower bound, and HIGH, the upper bound for an ordered subset to be relinked in balanced form. The root of a balanced sub- tree is the left median MID of the subset. Three condi- tions direct the course of GROW:

1. If LOW > HIGH, we have a null subtree; return a null pointer.

2. If LOW = HIGH, we have a leaf;

set up a terminal node and return the node pointer.

3. If LOW < HIGH, we have a divisible subset; do the following:

a) find the root T through MID.

b) GROW left subtree and return root as LSON of T.

c} GROW right subtree and return root as RSON of T.

A tree is balanced w h e n the ROOT of the tree, and the LSON and RSON of each node are readjusted.

3. PARALLEL BALANCING

Balancing a tree through folding reveals parallelism in the pointer-restructuring procedure. It is possible to di- vide the set of nodes into subsets and perform reorga- nizing operations on these parts simultaneously. One way to balance the left half- and right half-tree at the same time is given in Algorithm P1 (Figure 2). In this algorithm, folding is not used and the pointer-restruc- turing procedure GROW directly resembles Bentley's scheme [1].

Another degree of parallelism exists between traver- sol and pointer restructuring. If we physically rearrange all nodes in sorted order during traversal, the pointer- restructuring procedure becomes a pointer generator which needs only to know the size of the tree to com- pute all the LSONs and RSONs for corresponding nodes in the balanced tree structure. A copy of the original tree has to be made for traversal and sorting so that nodes could be regenerated without destroying useful information. Algorithm P2 (Figure 3) describes the way to exploit this parallelism.

4. A COMPARISON OF PERFORMANCE MEASURES

Three conditions, w h e n there are 0, 1, or more than 1 element in a subset, dictate the course of the pointer- restructuring procedure in our algorithms. Take Algo-

(4)

A l g o r i t h m P I : } b u i l d b a l a n c e d l e f t h a l f - t r e e a n d r i g h t h a l f - t r e e in p a r a l l e l } p r o c e d u r e B A L A N C E ( R O O T , L S O N , R S O N , n): i n t e g e r R O O T , n; i n t e g e r a r r a y L S O N , R S O N ; b e g i n i n t e g e r N; i n t e g e r a r r a y L I N K ( I : n ) ; { t r a v e r s e t h e o r i g i n a l t r e e a n d s e t u p L I N K } p r o c e d u r e T R A V B I N D ( T ) : i n t e g e r T; } p o i n t e r to t h e n e x t n o d e to be v i s i t e d } b e g i n if T = n u l l t h e n r e t u r n ; T R A V B I N D ( L S O N ( T ) ) ; N ~ N +I; } c o u n t t h e s e q u e n c e of v i s i t } L I N K ( N ) ~ T; } s t o r e t h e p o i n t e r to t h e N t h n o d e in the N t h e l e m e n t of L I N K } T R A V B I N D ( R S O N ( T ) ) ; e n d ; } r e o r g a n i z e a t r e e by p a r t i t i o n i n g } p r o c e d u r e G R O W ( L O W , H I G H ) : i n t e g e r LOW, H I G H ; b e g i n } M I D is t h e m e d i a n of a s u b s e t b o u n d by L O W a n d H I G H , T is t h e r o o t of t h e b a l a n c e d s u b t r e e r e f l e c t e d by t h e s u b s e t } i n t e g e r M I D , T; c a s e L O W > H I G H } n u l l b r a n c h l : r e t u r n ( n u l l ) ; L O W = H I G H }leaf} : b e g i n T ~ L I N K ( L O W ) ; L S O N ( T ) , R S O N ( T ) -- n u l l ; r e t u r n ( T ) ; end; L O W < H I G H } d i v i s i b l e s u b s e t } : b e g i n M I D -- L ( L O W + H I G H ) ~ 2 J ; T ~ L I N K ( M I D ) ; c o b e g i n } f o r m l e f t a n d r i g h t s u b t r e e s in p a r a l l e l } L S O N ( T ) ~ G R O W ( L O W , MID--l); R S O N ( T ) -- G R O W ( M I D + I , H I G H ) ; c o e n d ; r e t u r n ( T ) ; end; end; e n d ; if n ~ 2 t h e n r e t u r n ; N ~ 0; } i n i t i a l i z e c o u n t e r 1 T R A V B I N D ( R O O T ) ; M ~ ~ ( N + I)/ 2J; R O O T -- L I N K ( M ) ; { n e w r o o t } I b a l a n c e t h e l e f t a n d r i g h t h a l f - t r e e s in p a r a l l e l } c o b e g i n L S O N ( R O O T ) ~ G R O W ( I , M - I ) ; R S O N ( R O O T ) ~ G R O W ( M + I , N ) ; c o e n d ; end; FIGURE 2. Algorithm Pl. r i t h m P1 for e x a m p l e ; t h e n u m b e r o f t i m e s e a c h c a s e is e x e c u t e d c a n b e c a l c u l a t e d as f o l l o w s :

Let n b e the total n u m b e r of n o d e s in a set (n _> 3), a n d let a = ulog2n3, a n d b = L(2 ° + 2 ~-1 - 1)/n.3 Let Ki b e t h e n u m b e r of t i m e s c a s e i is e x e c u t e d in A l g o r i t h m P1, i ~ 1, 2, 3. T h e n , K1 = 12a(t-(n - 2 a - * } / 2 ~ - l J } - n - I I -< 2 ~-1 K2 = n - K3 Ka = 2 a-b - 1 + Klb

Let us s u p p o s e that Co is t h e a v e r a g e t i m e to visit a n o d e d u r i n g t r a v e r s a l a n d Ci is t h e u n i t t i m e t a k e n to e x e c u t e c a s e i o n c e . T h e total b a l a n c i n g t i m e T(n) c a n

(5)

be expressed as

T(n}

= Con + C1K1 + C2K2 + C3K3 =

O(n).

This expression also applies to Algorithms S and P2. Based upon the above expression, we will do the analyses of Algorithms S, P1, and P2. When balanced through folding as described in Algorithm S, only

,n/2J

elements have to be partitioned. This tends to decrease Ki's by half and slightly increases C/s. If parallel execu- tion is implemented efficiently, Algorith m P1 should have similar effects as decreasing K/s, in terms of turn- around time, yet without increasing Ci's.

The savings in time becomes obvious when n is large. Algorithm' P2 overlaps tra~,ersal and pointer-restructur- ing, so that the turnaround time should be the greater

of the two. Also, C/s tend to be smaller, but more space is required and physical record movement tends to in- crease Co.

The sequential Martin-Ness and Bentley algorithms that recursively partition the set of nodes to fo~m a balanced, binary search tree follow the same partition- ing scheme as that in Algorithm S.

A higher degree of parallelism and time efficiency is ~ichieved in our algorithms at the expense of increased working space size. By using an array instead of a stack to store the ordering information of the set of nodes, we are able to build halves of the balanced tree simultane- ously as in Algorithm S, or construct left subtrees and right subtrees in parallel as in Algorithm P1. By making a copy of the original tree, a s d o n e in Algorithm P2, we

A l g o r i t h m P2:

{ b u i l d a b a l a n c e d t r e e w i t h a p a r a l l e l s o r t i n g p r o c e s s }

procedure B A L A N C E ( R O O T , KEY, L S O N , R S O N , n) :

integer R O O T , n; array K E Y ; integer array L S O N , R S O N ;

b e g i n

integer N;

integer array K E Y C O P Y ( I : n ) , L S O N C O P Y ( I : n ) , R S O N C O P Y ( 7 : n ) ; { t r a v e r s e t h e c o p y of the o r i g i n a l t r e e a n d s o f t k e y s 1

procedure T R A V S O R T ( T ) :

integer T; [pointer to t h e next node to be v i s i t e d I

begin if T = n u l l then return; T R A V S O R T ( L S O N C O P Y ( T ) ) ; N ~ N + I; { c o u n t the sequence of v i s i t 1 K E Y ( N ) ~ K E Y C O P Y ( T ) ; {put N t h k e y in N t h p o s i t i o n } T R A V S O R T ( R S O N C O P Y ( T ) ) ; end; {generate p o i n t e r s for a b a l a n c e d t r e e s t r u c t u r e } procedure G R O W ( L O W , H I G H ) : integer LOW, H I G H ; b e g i n integer M I D ; {median of a s u b s e t b o u n d by L O W a n d H I G H } C a S e L O W > H I G H { n u l l b r a n c h } : r e t u r n ( n u l l ) ; L O W = H I G H {leaf}: b e g i n L S O N ( L O W ) , R S O N ( L O W ) ~ n u l l ; r e t u r n ( L O W ) ; end; L O W < H I G H { d i v i s i b l e s u b s e t } : b e g i n M I D ~ u ( L O W + H I G H ) / 2~; cobegin { f o r m l e f t a n d r i g h t s u b t r e e s in p a r a l l e l } L S O N ( M I D ) ~ G R O W ( L O W , M I D - 7 ) ; R S O N ( M I D ) ~ G R O W ( M I D + I , H I G H ) ; coend; r e t u r n ( M I D ) ; end; end; end; if n s 2 then return; { m a k e a c o p y of t h e o r i g i n a l tree} K E Y C O P Y -- K E Y ; L S O N C O P Y ~ L S O N ; R S O N C O P Y ~ R S O N ; N -- 0; I i n i t i a l i z e c o u n t e r }

{ s o r t keys and regenerate p o i n t e r s in parallel} cobegin T R A V S O R T ( R O O T ) ; R O O T -- G R O W ( 7 , n ) ; [ n e w root} coend; end; FIGURE 3. Algorithm P2.

(6)

M a r t i n a n d N e s s ' s A l g o r i t h m :

procedure B A L A N C E ( R O O T , L S O N , R S O N , n ) :

integer R O O T , n; integer array L S O N , R S O N ;

b e g i n

IT h o l d s p o l n t e r t o n o d e to be v i s i t e d d u r i n g t r a v e r s a l , S T A C K is u s e d to s t o r e T,

T O P p o i n t s t o t h e t o p e l e m e n t of S T A C K 1

integer T, T O P , A N S ; integer array S T A C K ( I : n ) ; { t r a v e r s e t h e i n p u t t r e e a n d r e t u r n a n o d e p o i n t e r in a s c e n d i n g key o r d e r v i a A N S I procedure T R A V N E X T : b e g a n if T ~ n u l l then b e g i n T O P -- T O P + I; S T A C K ( T O P ) ~ T; T ~ L S O N ( T ) ; T R A V N E X T ; end; else b e g a n A N S ~ S T A C K ( T O P ) ; T O P -- T O P - - 7 ; T ~ R S O N ( A N S ) ; e n d ; end; { r e s t r u c t u r e p o i n t e r s b y p a r t i t i o n i n g l procedure G R O W ( N ) : integer N; ~ n u m b e r of e l e m e n t s in a s u b s e t 1 b e g i n IT is r o o t of a b a l a n c e d s u b t r e e r e f l e c t e d b y a s u b s e t , L P T R is u s e d t o t e m p o r a r i l y s t o r e t h e L S O N o f T 1 integer T, L P T R ; c a s e N = 0 { n u l l b r a n c h l : b e g i n A N S ~ n u l l ; end; N = I { l e a f } : b e g i n T R A V N E X T ; L S O N ( A N S ) , R S O N ( A N S ) ~ n u l l ; end; N > I I d i v i s i b l e s u b s e t l : b e g i n G R O W ( L ( N - - 7 ) / 2 ~ ) ; { f o r m l e f t s u b t r e e } L P T R -- A N S ; T R A V N E X T ; T -- A N S ; G R O W ( F ( N - 7 ) / 2 n ) ; R S O N ( T ) -- A N S ; L S O N ( T ) -- L P T R ; A N S -- T; e n d ; e n d ; e n d ; i f n s 2 t h e n r e t u r n ; T ~ R O O T ; T O P -- 0; { i n i t i a l i z a t i o n } G R O W ( n ) ; R O O T -- A N S ; { n e w root} end; ~ f o r m r i g h t s u b t r e e }

Martin and Ness's Algorithm

(7)

D a y ' s A l g o r i t h m : p r o c e d u r e B A L A N C E ( R O O T L S O N , R S O N , n ) : i n t e g e r R O O T , n ; i n t e g e r a r r a y L S O N , R S O N ; b e g i n i n t e g e r T, L A S T _ V I S I T E D , L , R , M , B A C K B O N E _ L E N G T H ; ~ s t r l p e n o d e s o f f r i g h t - t h r e a d e d t r e e t o f o r m b a c k b o n e . T p c i n t s t o t h e n o d e t o b e v i s i t e d , L A S T _ V I S I T E D h o l d s p o i n t e r t o t h e l a s t v i s i t e d n o d e l if n ~ 2 t h e n r e t u r n ; T ~ R O O T ; w h i l e L S O N ( T ) ~ n u l l d o { f i n d t h e f i r s t n o d e 1 b e g i n ; T ~ L S O N ( T ) ; e n d ; R O O T ~ T; { r o o t o f b a c k b o n e } L S O N ( R O O T ) ~ n u l l ; L A S ~ V I S I T E D -- T; T -- R S O N ( T ) ; w h i l e T ~ n u l l d o I t r a v e r s e l b e g i n if T > 0 t h e n b e g i n w h i l e L S O N ( T ) ~ n u l l d o b e g i n ; T -- L S O N ( T ) ; e n d ; e n d ; e l s e T ~ - ( T ) ; { b a c k t r a c k } R S O N ( L A S T _ V I S I T E D ) -- T; I c h a i n t o g e t h e r } L A S T V I S I T E D -- T; L S O N ~ T ) ~ n u l l ; T -- R S O N ( T ) ; e n d ; I r e s t r u c t u r e b a c k b o n e t o a b a l a n c e d t r e e . M i s t h e n u m b e r o f t r a n s f o r m a t i o n s n e e d e d i n a p a s s , T n o w p o l n t s E o n o d e t o b e s h i f t e d o u t o f t h e b a c k b o n e , L i s t h e l e f t a n c e s t o r o f T, R i s t h e r i g h t s o n o f T, B A C K B O N E _ L E N G T H is t h e n u m b e r o f n o d e s i n t h e b a c k b o n e } B A C K B O N E _ L E N G T H -- n - I; M -- L B A C K B O N E _ L E N G T H / 2 m ; w h i l e M > 0 d o { t r a n s f o r m } b e g i n T -- R O O T ; ~ m o v e o n R O O T i n a n t i c i p a t i o n 1 R O O T -- R S O N ( R O O T ) ; R S O N ( T ) ~ L S O N ( R O O T ) ; L S O N ( R O O T ) -- T ; T ~ R S O N ( R O O T ) ; L ~ R O O T ; for I ~ 2 t o M b y 1 d o I s h i f t } b e g i n R -- R S O N ( T ) ; R S O N ( L ) -- R ; R S O N ~ T ) ~ L S O N ( R ) ; L S O N ( R ) ~ T; T -- R S O N ( R ) ; L -- R ; e n d ; B A C K B O N E _ L E N G T H ~ B A C K B O N E _ L E N G T H - M - I; M -- L B A C K B O N E _ L E N G T H / 2 J ; e n d ; e n d ;

Day's Algorithm

(8)

TABLE I. Performance of the Algorithms

Algorithm Martin/Ness Bentley Day

[1] [2] [3]

Chang/lyengar

S P1 P2

Right-Threaded No No Yes No No No

Input Tree

Additional Work Space Yes Yes No Yes Yes Yes

Run Time* O(n) O(n log2 n)** O(n) O(n) O(n) O(n)

Parallel Execution No No No No Yes Yes

Sorting No No No No No Yes

(P)-Perfect or P P R P P P

(R)-Route Balanced * n is the total number of nodes.

** Build and balance a tree instead of reorganizing an existing one.

a r e a b l e to s o r t t h e n o d e s a n d r e g e n e r a t e s u b t r e e p o i n t e r s i n t h e s a m e t i m e . D a y ' s a l g o r i t h m u s e s a s t a c k l e s s t r a v e r s a l to b u i l d a l i n k e d list a n d a l o o p to r e o r g a n i z e t h e l i n k e d list to a r o u t e - b a l a n c e d t r e e s t r u c t u r e w i t h o u t a u x i l i a r y s t o r a g e a n d r e c u r s i o n . T h e a v e r a g e t i m e r e q u i r e d c a n b e fig- u r e d as s h o w n b e l o w : T(n) ~ Con + C1 ~ (t_(n - i)/2i_j) i = 1 <- Con + Cl(n - 1)(1 - 2-") = O(n) w h e r e Co is t h e a v e r a g e t i m e to v i s i t a n o d e d u r i n g t r a v e r s a l ; C1 is t h e t i m e to go t h r o u g h t h e r e s t r u c t u r i n g loop o n c e ; n is t h e t o t a l n u m b e r of n o d e s i n a t r e e (n >__ 3); a n d a = L1og2n.3. D a y ' s a l g o r i t h m is a m o r e e f f i c i e n t s e q u e n t i a l b a l a n c - i n g a l g o r i t h m , if p e r f e c t b a l a n c e is n o t r e q u i r e d . H o w - e v e r , i n o r d e r to u s e t h i s a l g o r i t h m , a t h r e a d e d t r e e h a s to b e m a i n t a i n e d . P r o p e r t i e s of p r e v i o u s l y d i s c u s s e d al- g o r i t h m s a r e s u m m a r i z e d i n T a b l e I for c o m p a r i s o n . 5. S U M M A R Y W e h a v e p r e s e n t e d a n e f f i c i e n t A l g o r i t h m S to g l o b a l l y b a l a n c e b i n a r y s e a r c h t r e e s t h r o u g h f o l d i n g p r o v i d e d t h a t t h e o r d e r of all n o d e s is p r e d e t e r m i n e d a n d t h e i t h i t e m c a n b e f o u n d g i v e n i. B a l a n c i n g a t r e e t h r o u g h f o l d i n g r e v e a l s p a r a l l e l i s m i n t h e p o i n t e r - r e s t r u c t u r i n g p r o c e d u r e . T h i s is a u s e f u l a d v a n t a g e i n a p a r a l l e l p r o c - e s s i n g e n v i r o n m e n t . T w o p a r a l l e l a l g o r i t h m s to b a l a n c e b i n a r y s e a r c h t r e e s a r e p r o p o s e d . A l g o r i t h m P1 p a r t i t i o n s a set a n d b a l a n c e s left s u b t r e e s a n d r i g h t s u b t r e e s s i m u l t a n e - ously. A n o t h e r p a r a l l e l a l g o r i t h m , P2, a c t u a l l y s o r t s t h e r e c o r d s a n d m a k e s p o i n t e r r e g e n e r a t i o n a n i n d e p e n d - e n t a c t i v i t y . A c o m p a r i s o n of o u r a l g o r i t h m s w i t h o t h e r e x i s t i n g a l g o r i t h m s is g i v e n i n T a b l e I. A c k n o w l e d g m e n t s T h e a u t h o r s w i s h to t h a n k a n a n o n y m o u s r e f e r e e for a n u m b e r of v a l u a b l e sugges- t i o n s a n d c r i t i c i s m s . T h a n k s a r e d u e to P r o f e s s o r H o r - o w i t z for h i s c o m m e n t s o n o u r p a p e r . W e w o u l d also l i k e to t h a n k P r o f e s s o r M o i t r a for h e r c o m m e n t s o n t h i s p a p e r . REFERENCES

1. Bentley, J.L. Multidimensional binary search trees used for associa- tive searching. Commun. ACM 18, 9 (Sept. 1975), 509-517. 2. Day, A.C. Balancing a binary tree. Comput. J. 19, 4 (Nov. 1978), 360-

361.

3. Horowitz, E., and Sahni, S. Fundamentals of Data Structures. Com- puter Science Press, Inc., Potomac, MD, 1976, pp. 442-456. 4. Iyengar, S.S., and Chang, H. Algorithms to create and maintain bal-

anced and threaded binary search trees. Submitted for publication Software and Practice Journal November 1982.

5. Iyengar, S.S., and Chang, H. A fast global algorithm to balance bi- nary search trees. Submitted for publication, 1984.

6. Knuth, D.E. The Art of Computer Programming, VoL 3: Sorting and Searching. Addison-Wesley Publ. Co., Inc., Reading, MA, 1973, p. 722.

7. Martin, W.A., and Ness, D.N. Optimal binary trees grown with a sorting algorithm. Commun. ACM 15, 2 {Feb. 1972), 88-93. 8. Moitra, A., and Iyengar, S.S. Maximally parallel algorithms to bal-

ancing binary search trees. Submitted to IEEE Trans. Comput. for publication.

CR Categories and Subject Descriptors: E.1 [Data]: Data Structures-- trees; F.1.2 [Computation by Abstract Devices]: Modes of Computation; F.1.3 [Computation by Abstract Devices]: Complexity Classes; F.2 [The-

ory of Computation]: Analysis of Algorithms and Problem Complexity

General Terms: Algorithms, Theory

Additional Key Words and Phrases: binary search tree, computa- tional complexity, parallel algorithms' folding method

Received 4/82; revised 3/83; accepted 11/83

Author's Present Address: Hsi Chang and S. Sitharama lyengar, Depart- ment of Computer Science, Louisiana State University, Baton Rouge, LA 70803.

Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commer- cial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission.

References

Related documents