Professional Documents
Culture Documents
SECTION 6
OBJECTIVES: At the end of the session, the student is expected to be able to
1. Define a binary tree and related concepts.
2. State important properties of binary trees.
3. Give examples of different types of binary trees.
4. List the nodes of any given binary tree in preorder, inorder and postorder
5. Design algorithms which center on the idea of traversing a binary tree, e.g. ,
constructing a binary tree given its nodes in preorder and inorder, traversing a
binary tree in level order, etc.
6. Write EASY procedures to implement the algorithm in 5.
DISCUSSION:
Thus far we have considered only linear data structures, viz., the stack and the
queue, which are represented in the memory of a computer either by a sequentially
allocated vector or a singly-linked linear list. We will now consider a very important
nonlinear data structure – the binary tree.
Section 6 Page 1 of 12
Jennifer Laraya-Llovido
Data Structures & Algorithms
A
Λ A A A
A
(null
Binary tree) B
B
B B C
D E F G
Example (a) depicts the especially important null or empty binary. In examples (b) to (f),
node A is the root of the binary tree. In (b), both left and right subtrees of the root are
null; while in (c) and (d), the right and left subtrees, respectively, are null. For the binary
tree in (e), all right subtrees are null; while for that in (f), each node either has two
subtrees or none at all. Later, we will give names to these and other types of binary
trees.
A
………….. Level 0
B C …………… Level 1
D E F G ……. Level 2
H I J ……………………. Level 3
Node A is the root of the binary tree. The left and right subtrees of node A are the
binary trees rooted at nodes B and C, respectively. In turn, the left and right subtrees of
node B are the binary trees rooted at D and E, respectively. And so on.
Section 6 Page 2 of 12
Jennifer Laraya-Llovido
Data Structures & Algorithms
The number of non-null subtrees of a node is called its degree. Thus, nodes A, B,
C and D have degree 2; node E has degree 1; and nodes F, G, H, I and J have degree
zero. Nodes with degree zero are called leaves or terminal nodes.
The level of a node is defined by specifying that the root of the binary tree is at
level 0, the roots of its subtrees are at level 1, and so on. The height or depth of a binary
tree is the level of the bottommost node(s), which is also the length of the longest path
from the root to any leaf. The binary tree in Fig. 1has height (or depth) 3.
In addition to botanical terms (tree, root, leaf, branch) and spatial terms (left,
right, top, bottom), we will also use the familial terms (father, son, brother, descendant,
ancestor) in describing binary trees and operations on them. We will freely mix
metaphors involving these three classes of terms, as in the phrase left son of the root.
In the binary tree of Fig. 1, node A is the father of nodes B and C, and nodes B
and C are the sons of node A. Nodes B and C are, of course, brothers. Nodes D, E, H, I
and J are the descendants of node B; nodes A, B and D are the ancestors of node H.
Then
(a) N = n0 + n1 + n2 (obviously)
= nB + 1 (since each node, except the root, has a branch leading
to it)
Section 6 Page 3 of 12
Jennifer Laraya-Llovido
Data Structures & Algorithms
n0 = n2 + 1 Q.E.D.
2n
where ( ) = 2n (2n-1)(2n-2)…(n+1)
n n(n-1)(n-2)…1
The derivation of the formula for bn is quite involved. (See, for example, Standish[1980],
pp. 54-57.)
Certain types of binary trees are used in the analysis of the time complexity of a
number of searching algorithms and also in the implementation of a particular sorting
technique (heapsort). Unfortunately, there is no agreement among various authors as to
the names given to these binary trees. To add to the confusion, the same name is
sometimes used to refer to two different types of binary tree. For our purposes, we will
adopt, and consistently use, the following nomenclature for binary trees:
1. A right (left)-skewed binary tree is a binary tree in which every node has no left
(right) subtree. For a given number of nodes, a left or right-skewed binary tree
has the greatest depth.
A A
B B
C
C
Section 6 Page 4 of 12
Jennifer Laraya-Llovido
Data Structures & Algorithms
2. A strictly binary tree is a binary tree in which every node has either two subtrees
or none at all.
A
B C
D E
F G
3. A full binary tree is a strictly binary tree in which all terminal nodes lie at the
bottommost level. For a given depth, the full binary tree has the maximum
number of nodes.
B C
D E F G
H I J K L M N O
4. A complete binary tree is the binary tree which results when zero or more nodes
are deleted from a full binary tree in reverse level order, i.e., from right to left,
bottom to top.
B C
D E F G
H I J L
K
Section 6 Page 5 of 12
Jennifer Laraya-Llovido
Data Structures & Algorithms
a node means to perform the computations (not necessarily numerical) local to the node.
Such a process is called a traversal.
In a binary tree, there are three distinct and disjoint entities: a node (or root), its
left subtree (which may be null) and its right subtree (which may be null). Let us denote
these entities as N, L and R, respectively. The order in which we process these entities,
recursively, defines a particular traversal method. There are six different sequences:
NLR, NRL, LNR, LRN, RNL and RLN. If we insist that L should always precede R, then
we are left with three traversal orders: NLR-recursive, or preorder, LNR-recursive or
inorder, and LRN-recursive or postorder. The prefixes pre, in and post specifies the
position of the root in the order of processing. The three traversal methods are defined
as follows:
Definition: If the binary tree is empty, do nothing (or consider the traversal as
‘done’); else
1. Preorder traversal
Visit the root.
Traverse the left subtree in preorder.
Traverse the right subtree in preorder.
2. Inorder traversal
Traverse the left subtree in inorder.
Visit the root.
Traverse the right subtree in inorder.
3. Postorder traversal
Traverse the left subtree in postorder.
Traverse the right subtree in postorder.
Visit the root.
Note that each of the above definitions is recursive. This is to be expected since the
binary tree is itself a recursively defined structure.
If, in the above definitions, we interchange ‘left’ and ‘right’, then we obtain three
new traversal orders which we may call converse preorder, converse inorder and
converse postorder. Still another way to traverse a binary tree is in level order, that is,
visit the nodes from top to bottom, left to right.
Section 6 Page 6 of 12
Jennifer Laraya-Llovido
Data Structures & Algorithms
Example 1.
Preorder:A B C D E F G H I J
A Inorder:C B D E F A G F I H J
F Postorder:C E D B G I J H F A
B
H
C D G
E I J
Example 2.
-
Preorder: - + A * / B ^ C D E F
+ F Inorder: A + B / C ^ D * E - F
A * Postorder: A B C D ^ / E * + F -
E
/
B ^
C D
Thus far we have been looking at a binary tree in the abstract, independent of a specific
representation. However, to realize the traversal and other algorithms on binary trees on a
computer we need to represent a binary internally in computer memory. The most natural way to
do this is to use the link addressing method where each node has structure
where the LSON and RSON fields contain pointers to the left and right subtrees of the
node, respectively; and the DATA field contains the data local to the node. This
representation mimics the way we draw a binary tree on paper. Using this
representation, the binary tree of Example 1 is depicted, thus:
Section 6 Page 7 of 12
Jennifer Laraya-Llovido
Data Structures & Algorithms
B F
Λ C Λ Λ D Λ G Λ H
Λ E Λ Λ I Λ Λ J Λ
The pointer variable T points to the root node and, effectively, to the entire binary tree.
Note that from the root node all nodes of the binary tree are accessible via the LSON
and RSON links. In general, we will refer to a binary tree by the pointer to its root node;
e.g., the binary tree of Fig. 2 will be called binary tree T. The condition S= A means the
binary tree S is empty.
For any given node a, this linked representation of a binary tree answers the
question “Who are your sons?” but not the question “Who is your father?”. We ask the
first question as we descend a binary tree (from the root to the leaves), and we ask the
second question when we climb up a binary tree. Traversal and other algorithms on
binary trees invariably climbing down and up a binary tree. In the absence of a Father
link, we need to devise ways to locate the fathers. One easy way to do this is to use a
stack; other techniques, such as the use of threads, link inversion, etc., will be discussed
in subsequent sessions.
Section 6 Page 8 of 12
Jennifer Laraya-Llovido
Data Structures & Algorithms
traversal orders; the non-recursive versions, though less concise, give a better insight
into the traversal process, which is of considerable importance in understanding and
developing algorithms on binary trees.
The non-recursive version for inorder traversal is given below. The procedure
uses a sequential stack to store the addresses of nodes whose left subtrees are
travesed (in inorder) so that, subsequently, each such node can be visited and its right
subtree traversed (in inorder). Study the procedure carefully.
procedure INORDER(T)
//Non-recursive procedure to print the node labels of binary tree T. Uses a sequential
stack of maximum size 100.//
array S(1:100)
node(LSON, DATA, RSON)
αÅT
top Å 0
loop
while α <> A do
call SPUSH (S, 100, top, α)
α Å LSON(α)
endwhile
if top=0 then return
else [call SPOP(S,100,top,α)
output DATA(α)
αÅ RSON(α)]
forever
end INORDER
The recursive procedures for preorder and postorder traversal are readily coded
from the corresponding recursive definitions. The non-recursive version for preorder
traversal differs from that for inorder traversal only in the position of the output
statement. Writing these procedures is left as an exercise.
Section 6 Page 10 of 12
Jennifer Laraya-Llovido
Data Structures & Algorithms
which point it is unstacked the second time and finally visited. In summary, for preorder
and inorder traversal, a node is stacked-unstacked once, whereas for post order
traversal a node is stacked-unstacked twice. Thus, for postorder traversal, we either use
two stacks, or a single stack plus a tag bit to differentiate between the first and second
instances that a node is placed in the stack.
The EASY procedure POSTORDER given below uses a single sequential stack
and the – and + signs as tags. A node, say node a, is placed in the stack for the first
time as –a, and for the second time as +a. Study the procedure carefully.
1. Making a duplicate copy of a binary tree: To make a copy of a binary tree, say T,
represented as in Fig. 2, we can use postorder traversal (review the recursive
definition given above) as follows: for every node, say node a, in T
Traverse the left subtree of node a in postorder and make a copy of it.
Traverse the right subtree of node a in postorder and make a copy of it.
Make a copy of node a and attach copies of its left and right subtrees.
procedure COPY(T)
//Given a binary tree T, COPY makes a duplicate copy of T and returns a pointer
to the root node of the constructed copy//
node(LSON, DATA, RSON)
αÅA
if T<>A then [βÅ COPY (LSON(T))
α Å COPY (RSON(T))
call GETNODE (α )
DATA (α ) Å DATA (T)
LSON (α ) Å β
RSON (α) Å α]
return (α)
end COPY
SÅ COPY (T)
2. Determining the equivalence of two binary trees: two binary trees are equivalent if one
is an exact duplicate of the other, such as binary trees S and T above. To determine
whether two binary trees, say S and T, are equivalent, we can apply preorder traversal
(review the recursive definition given above) as follows: for every corresponding nodes,
say node a and node b, in S and T
Check whether node a and node b contain the same data
Traverse the left subtrees of node a and node b in preorder and check whether
they are equivalent
Traverse the right subtrees of node a and node b in preorder and check whether
they are equivalent
If at any point in the process it is discovered that S and T are not equivalent, the test is
terminated. The following EASY procedure formalizes the idea:
Procedure EQUIV(S,T)
//Given two binary trees S and T, EQUIV returns true if S and T are equivalent; else, it
returns false.//
node(LSON,DATA,RSON)
ansÅ false
case
:S = A and T=A: ans Å true
:S<> A and T <> A: [ans Å (DATA(S)= DATA (T))
if ans then ans Å EQUIV(LSON(S), LSON(S))
if ans then ans Å EQUIV(RSON(S), RSON(S))
endcase
return (ans)
end EQUIV
Section 6 Page 12 of 12
Jennifer Laraya-Llovido