Trees As Data Structures
Trees As Data Structures
Research Online
1979
Recommended Citation
Berztiss, Alfs T., Trees as data structures, Department of Computing Science, University of Wollongong,
Working Paper 79-2, 1979, 75p.
https://ro.uow.edu.au/compsciwp/3
Research Online is the open access institutional repository for the University of Wollongong. For further information
contact the UOW Library: [email protected]
PREFACE
A. T. Berztiss
Department of Computer Science
University of Pittsburgh
Pittsburgh, PA 15260, U.S.A.
TREES AS DATA STRUCTURES
FURTHER READING • 75
1. TREES AND THEIR REPRESENTATIOlll
c
when there is just one arc originating from a node, it is still
s
possible to impose a left or right orientation on this arc, and,
if this is done, the binary tree becomes oriented. It is common
practice to reserve the term binary tree for structures that
would be more precisely called oriented binary trees.
The level of a node in a tree is the length of the path
(i.e., the number of arcs on the path) from the root to this
node. The level of the root is thus zero. The height of a tree
is the greatest level assignment given to a node in the tree,
i.e., it is the length of the longest path from the root to-a
terminal node.
i
Level i of a binary tree is full if there are exactly 2
nodes at this level. A binary tree of height k is full if
level k in this binary tree is full -- level k being full implie
of course, that levels k-l, ..• ,l,O are all full. A binary tree
of height k is balanced if level k-l in this binary tree is fu1] 1
Consider node n in a binary' tree. The binary subtree-rooted at
the left successor of n is the left subtree of n, and that
rooted at the right successor is the right subtree. A binary t
tree is height balanced (or an AVL-tree, after Ade1son-Ve1skii a
and Landis) if, at every node in the tree, its left and right F
subtrees differ by no more than 1 in height (for the purposes
of this definition a tree that does not exist has height -1
for example, if a node has no left successor, and its right
successor is a leaf, the heights of its left and right subtrees
are -1 and 0, respectively).
Every tree can ~e transformed into a binary tree by means
of the following procedure, which has become known as the t
Knuth transformation: i
Denote the arcs originating at internal node x in a tree o
by <x'Yl' <x'Y2 , ... , <x,y? Assign left orientation
to arc <x'Yr ' and replace the remaining arcs by arcs 0:
Figure 1 Figure 2
.2 Traversals of trees
pre 1- 2 6 7 3 8 4 5 9 10 12 11
in 6 7 2 8 3 4 9 12 10 11 5 1 (1)
post 7 6 8 12 11 10 9 5 4 3 2 1
T-pre 1 2 6 7 3 8 4 5 9 10 12 11
(2)
T-post 6 7 2 8 3 4 9 12 10 11 5 1
Figure 1 Figure 2
pre 1 2 6 7 3 8 4 5 9 10 12 11
in 6 7 2 8 3 4 9 12 10 11 5 1 (1)
post 7 6 8 12 11 10 9 5 4 3 2 1
T-pre 1 2 673 8 4 5 9 10 12 11
(2)
T-post 6 7 2 8 3 4 9 12 10 11 5 1
LR-levcl 126 3 7 8 4 5 9 10 12 11
(3)
RL-level 123 6 4 8 7 5 9 10 11 12
T-LR-level 1 2 3 4 567 8 9 10 11 12
(4)
T-RL-level 154 3 2 11 10 9 8 7 6 12
LR-down 1 234 5 6 7 8 9 10 11 12
(5)
RL-up 1 543 2 11 10 9 8 7 6 12
R(pre) 11 12 10 9 5 4 8 3 7 6 2 1
R(in) 1 5 11 10 12 9 4 3 8 2 7 6 (6)
R (post) 1 2 3 4 5 9 10 11 12 8 6 7
C (pre) 1 2 3 4 5 9 10 11 12 8 6 7
C (in) 1 5 11 10 12 9 4 3 8 2 7 6 (7)
C(post) 11 12 10 9 5 4 8 3 7 6 2 1
Note that
R(pre) C (post) ,
R(in) C (in) (8)
R(past) C(pre)
pre III
R
.. C (post)
ct
C (pre) " ~
tc
post
R
R,C
in • ~ C(in)
Figure 3
6
R
LR-level ....t ---........ R (LR-level)
C t
RL-level .....t----'l~~ R (RL-level)
t
c
LR-down .... R
~ R (LR-down)
ct
RL-down 4 ~
tc
R(RL-down)
R
R R(RL~up)
RL-up ~ ~
LR-up
t ... ....
t
c
R(LR-up)
R
Figure 4
RL-1eve1 1 2 3 6 4 8 7 5 9 10 11 12
R (LR-leve1) 11 12 10 9 5 4 8 7 3 6 2 1
R(RL-1eve1) 12 11 10 9 5 7 8 4 6 3 2 1
RL-down 1 2 6 3 8 7 4 5 9 10 12 11
R(LR-down) 12 11 10 9 8 7 6 5 4 3 2 1 (9)
R(RL-down) 11 12 10 9 5 4' 7 8 3 6 2 1
LR-Up 6 2 1 7 8 3 4 9 5 12 10 11
R(RL-up) 12 6 7 8 9 10 11 2 3 4 5 1
R(LR-up) 11 10 12 5 9 4 3 8 7 1 2 6
Note that the order of the statements for R{t) is exactly the
reverse of that for traversal t, but that C(t) is obtained from
t by interchanging Y and Z alone.
Let us now remove recursion from schema (IO) by the intro-
duction of an explicit pushdown store.
where
until noleft
until PDI = ;r
end;
until noleft;
repeat node -<- PD2;
process node
until PD2 == ~
until PDI == ~
preorder
inorder
lU.-levelorder
RL-downorder
LR-uporder
1: 2,3,4,5
2: 6,7
3: 8 (12)
5: 9,10,11
10: 12
11
INOROER
RL-LEVELORDER
RL-OOWNORDER
LR-UPORDER
PREORDER
Figure 5
J.L
1: 2, -
2: 6, 3
3: 8, 4
4: 5
(13)
5: 9, -
6: 7
9: 10
10: 12,11
1: 2
2: 6 3
3: 8 4
4: 3* 5
5: 9 1*
6: 7
7: 6* 2*
8: 2* 3*
9: 4* 10
10: 12 11
11: 10* 5*
12: 9* 10*
Figure 6
node: = nj
while right exists do node: rig~tj
parent: = thread;
1 2 3 -4 5 6 7· 8 9 10 11 12
1 1 1 0 1 o o 0 o 1 o o (14 )
o 1 110 1 o 0 1 1 o o
1 2 3 4 567 8 9 10 11 12 13 14 15
(15)
9 8 6 4 7 3 2 1 5
Figure 7
16
i 1 2 3 4 5 6 7 8 9
(16)
heap [iJ 9 8 4 7 5 1 2 3 6
A~
3 6
Fi1lure 8
17
for i: = I to n do
begin read (datum);
heap[i]: = datum;
bubbleup (heap,i)
temp: = heap[j];
heap[j]: = heap[n];
if heap[j]) temp then bubbleup (heap,j)
else siftdo~n (heap,j,n-l);
for i: = ndownto 2 do
begin swap heap[l] and heap[i];
siftdown (heap,l,i-l)
At the end of the k-th iteration the k largest keys are in their
final positions, and, since the tree of the remaining n-k
keys possesses the heap property, the largest of these keys is
in heap [1], ready to be shifted into hea~[n-k]at the start of the
next iteration.
The search tree prooerty of binary trees is similar to the
tree property: for each node, the key at the left successor is
smaller and the key at the right successor larger than the key
at the node. Keys 1,2, ... ,9 have to be qssigned to the binary
19
Figure 9
The expression
(17)
Examples:
(19a)
o.
-cn + c - d 0,
o d n +2 _ d n+l _ d n
n 2
d (d - d - 1).
(21)
*
gives c : 1//5, c -1//5. Thus
l 2
a (h) n (23)
Aa .
n
Taking (17) with the right hand side set to zero, and making
substitution (23), we obtain
0,
which simplifies to
(24)
(25)
(26)
We expect a (p) to have the same form as the right hand side
n
of (17).
Examples:
n
1. The homogeneous solution of (lB) is A2 • For the
particular solution try the constant B. The substitution
n
B-2B = 1 gives B = -1. Hence the total solution is A2 -l, and
initial value a = 1 establishes A = 1. WARNING: Constants Ai
l
are not to be evaluated before the total solution has" been
n
derived. Here, if al=l had been used in an (h) = A2 , we would
have obtained A=~.
(2B)
(29)
n
an (h) = Al (_2)n + A • For the particular solution try B2 . Then
2
n
B2 n + B2 n - l - 2B2 - 2 = 2 n , or 2B + B - B = 2, giving B=l. Hence
n
an = (-2)n Al + A + 2 , where Al and A are determined from
2 2
boundary values.
(30)
F(x) = \~ (31)
t 1 =o
Solution: Since
d 1 -x
r=x
1
generates [1,1,1, •.. ], we have that
d d 1
x dx(i-x] generates [0,1,2, .•• ], and x dX(x dx generates (I-X]]
2 12 2 2 ] Th
[0 , , ,.... e ~ eren ~at~on resu ts ~n (I-x) 3, whO~c h ,
dOff t" l ' x(l+x)
. x(l+x)
on application of the summ~ng operator becomes (I-x)'. The
r:=2 a nx n ~oo
l. n=2 an_Ix
n
- r"n=2 a n-2 x n O. (33)
or F (x) x
l-x-x 2
and we next have to restore F(x) to the form (31). By the method
27
x
F(x) (I-cx) (l-dx)
1 [ cx dx ],
c-d l-cx l-dx .
1+.5 1- 15 cx
where c -2-' d -2-' But
1-cx
F (x) 1 \~ (ck_dk)xk,
Hence c-d l k=O
(34)
1 _ 1 + x + x 2 + x 3 + •••
I-x -
Hence, 1 1 + 2x + 3x 2 + 4x 3 + •••
-(l-X) 2
and
x 234
nx •
n
(1~x)2 = X + 2x + 3x + 4x + •••
x
Substitution into (34) gives F(x)-a
o = xF(x) + rr=xr2, and, since a o =l,
F(x) = x + 1
(I-x) 3 (l-x)
1
[0,1,3, ... ,~k(k+l) , ... ], and, since I-x generates [1,1,1, ••. ,1, ••• ],
we have
an ~n(n+l) + 1.
2.4 Recurrence relat10ns w1tn var1aDle coerr1C1cncs
o , (36)
l n
a n + l / II k-l g (k) ] - n - lg(k)
[ an;t! k=l ' ] 0, (37)
n-l
a n ;11 k=l g (k) • (38)
t;n-l k
A + ~k:l [f(k)/ITi=l g(i)]
Examples:
n
2 [A ~n-l
+ l.k=l ¥ I (k+l) 2J
= n2[A ~n-l
+ l.k=l k(k+l) ]
2
n [A
~n-l
+ l.k=l (~- k;l)]
2
n [A + n~l].
2
Hence = n A + n(n-l), and use of al=l gives A=l.
a
n.
The solution: a = n 2 + n(n-l) = n(2n-l).
n
i 1 2 3 4 5
c. 1 3/2 17/9 53/24 149/60
C,
~
, 1 3/2 5/3 2 11/5 (41)
~
ratio 1 1 1.13 1.10 1.13
Derivation of C
N is easy:
•
C
N = ~ l~=o (i+1)n i (M l1og 2 NJ ).
Hence
N = N~(M+1)
C' (N+1-2M)+~ \ ~-l (i+1) 2 i
N L ~=O
(i<O) ,
With the new node added there are now N-l nodes for which
the expected number of comparisons is C~l' and the added node
HI
for which it is ~(2ni_l - ni' summed over all levels. Hence
Then
,N-l
[.i=l (i+l) (2n i _ l - nil
,N-l . N-l (' ' ) ,N-I ,
Li=l ~n.
.~-
I + 2 }:N-l
~=~-
'-I ~n,1- l-~n.
. In, 1 + }: 1- 1 -[.·-ln~.
1-
(43)
i=O
i=l
,,
,,
I
,,
, , ;' ,
, ,, '\ i=2
, ,, ,,
I ,
, I \
...
Figure 11
32
1 2
C _ + N 1
C = (l-iF) - N
-2
N N l .
1 2. 1
CN+ 1 = (1 - TN+lT 2 ) CN + N+l - (N+T) 2. (44)
C = (N+l) [A + IN
N 2N
(2k-l) (2k
k=2 -p- m )J
= (~~l) [A + L~=2 ~i~~l~)]' N~ 2.
2 (2k-l)
k(k+l)
J
33
.. (N+l)
2N
[1 + L
N
k=2
6
k+l K
2
lJ
~~1)lL~=3~+ N~l ]
(~~1) [ 4H N - 4 - 2 + N~l ]
1
2 (l+&)H N - 3, (45)
111
loge N + 0.577216 + 2N - 12N 2 + l20N~ -
(46)
Thus,
1. 39.
K (p)
( 3+/S)
n 5+15 n.
giving
3+/5) (1+/5)
KN '" logN ( 5+/5 flog - 2 - (53)
(c. f. C'
N
Figure 12
1\
d:
Figure 13
39
C1
C1 B
y 6
B y
B y
Figure 14
40
Figure 15
I
I
CD
Figure 16
e R set flag ~
e L set flag 0
0 R set flag e ; stop
0 L L a: rotate; stop
0 L R c: d.rotate; stop
(:)) R L d: d.rotate; stop
(:)) R R b: rotate; stop
(:)) L set flag e; stop
Figure 17
e e e e
0 e ~ e
s 0 e e
Figure 18
the key associated with a node in this traversal sequence into its
predecessor node in terms of the inorder sequence. Ultimately the key
associated with a terminal node is so shifted, and this terminal node
becomes vacant. Now a procedure analogous to that for insertion can bE
used to restore balance in the tree. No more than O(log N) nodes are
examined, i.e., deletion is also an O(log N) process.
42
Figure 19
~ ~ JR ep ~ JR ~
~,~ ~ ,~ ~ ~ ~ ~ ~'~ ~'~ ~ ~ ~ ~
Figure 20
43
Figure 21
~ T not empty do
44
indegree, and one node form each nontrivial strong component that
has zero indegree as a whole. It is easy to see that the roots of
the K-trees in a minimal representation constitute a node base.
The output of the algorithm above consists of list K, followed
by list K',~ where either ~f the lists may be empty, and this
output has the depth-first property. Moreover, if K' contains no
more than one K-tree, there is minimality. If K' co~~ains more
than one K-tree, then a determination has to be made 'Jf whether
or not the roots of the K-trees belong, as D-nodes, to strong
components of zero indegree. If they do not, then the K-trees
rooted at these nodes can be absorbed by other members of K', and
a minimal representation results. More detail of this at the
start of Section 3.3.
Figure 22
eet[l] 0;
eet[yJ maxx<y(eet[x J + w[x,yJ), y 'I- 1;
&et[n] eet [n J;
let[yJ miny>x (let [x J - w[x,yJ) , y 'I- n.
l i newlength;> length[y] ~
begin lengthly]:=newlength;
make beforety] an empty list;
add x to list before [y ]
end
else if newlength=length(y] then add x to list before[yJ;
Figure 23
54
until terminal:
repeat advance to next K-node under postorder;
jL K-node is root of K-tree ~
begin alldone:= rootdone:= ~:
vet visited
55
Figure 24
53 57 44 50 47 47
Fig. 25
L:="';
y:=start-up node;
set cost of start-up node to zero;
K:=atomic K-tree of y;
done:-false;
repeat
fill: all successors of y do
~ x:=node being processed;
cost:=(cost of y) + (cost of yx);
i f cost < L tbgn
if x not on level n then
enter in pqueue a record consisting of cost and of a
pointer to node x in K, using the value of cost to
determine the position of the record in pqueue
else begin L:~cost;
else
begin extract lowest cost record from pqueue:
use it to gain access to a node in K:
y:=the node accessed in K:
attach atomic K-tree to y in K
~
until done:
Figure 26
Figure 27
61
Figure 28
**1**23***813***73***434*5**63758 (54)
***434*5**~37
iff, lettin'.! 111 and lei denote rCl;p<,ctivcly the number of node
symbols and of K-opprators in sl ••• si' the following holds:
where above each node symbol is a count of the stars that precede
it.
Reverse postorder is symmetrical to preorder in that
R(past) := C(pre). Hence a K-formula "symmetrical" to (54) can
be produced in a reverse postorder traversal of the K-tree of
Fig. 21:
**18**2***8***75***4*5**673433313 (57)
2 0 2 3 3 o 3 1 2 0 0 o 0 o .0 o o (58)
1 8 2 8 7 5 4 567 3 4 3 3 3 1 3
process node[l];
f.w;: k:=l to n-l do
begin if star[k]tO then begin nodestack ~ node[k];
starstack ~ starCk]
process node[k+l];
i ~ starstack;
i:=i-l;
if ito then starstack ~ i else pop nodestack
ltlhJ.l.e
moretodo /\ nodestacktO do
begin i ~ starstack;
i:=i-l;
i f ito ~ hegin starstack ~ i;
moretodo : =.fsU..ag.
65
Figure 29
66
Figure 30
queue
new: ~ queue,
push: queue x item ~ queue,
pop : queue - - ? queue,
peek: queue - - ? item,
null: queue~Boolean;
-
forall q E queue,
null (new)
i E item let
~,
.
New York,
. 1958.
H. Levy and F. Lessman, Finite Difference Equations,
Macmillan, New York, 1961.