0% found this document useful (0 votes)
8 views

Module 7 8 - Trees

Trees are one of the most important data structures in computer science. They provide a natural way to represent hierarchical data like family trees. A tree consists of nodes connected by directed lines or branches, with one node designated as the root. Key terminology includes root, parent, child, ancestor, descendant, leaf, internal node, subtree, and level. Trees can be represented using arrays or linked structures. Binary trees restrict nodes to having at most two children, and binary tree traversals like preorder, inorder, and postorder visit nodes in a predefined sequence. Expression trees represent mathematical expressions as binary trees, with operands as leaves and operators as internal nodes.

Uploaded by

razyfreel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Module 7 8 - Trees

Trees are one of the most important data structures in computer science. They provide a natural way to represent hierarchical data like family trees. A tree consists of nodes connected by directed lines or branches, with one node designated as the root. Key terminology includes root, parent, child, ancestor, descendant, leaf, internal node, subtree, and level. Trees can be represented using arrays or linked structures. Binary trees restrict nodes to having at most two children, and binary tree traversals like preorder, inorder, and postorder visit nodes in a predefined sequence. Expression trees represent mathematical expressions as binary trees, with operands as leaves and operators as internal nodes.

Uploaded by

razyfreel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

TREES

7.1 INTRODUCTION

Trees are one of the most important data structure in Computer Science. They come in
many forms. They provide natural representation for many kinds of data that occur in applications,
and they are useful for solving a wide variety of problem.
In many applications, objects exhibit a nonlinear order in which a member may have
multiple successors. For example, in describing a family tree, a parent may have many descendants
(children). See Figure 7.1
“A tree is a nonlinear structure which consists of a finite set of elements, called node,
and a finite set of directed lines, called branches that connect the nodes.” [GILB98]

grandfather

father uncle aunt

brother child sister

Figure 7.1 Example of Family Tree

7.2 BASIC TERMINOLOGY

Root - first node of a tree/ no parent/ ancestor

Parent – immediate root of a node/ a node which has successor nodes

Children – roots of the subtrees

Siblings – node or children of the same parent

Ancestor – all nodes along the path from the to that node

Descendants – all the nodes of a subtree

Leaf/terminal node – it has no children/ no descendants


Nonleaf / nonterminal node – has at least one children or descendant node.

Degree of a tree – number of subtrees of a node

Weight of a tree – number of leaf nodes

Level of a node – the node’s distance from the root

Subtree – any connected structure below the root. In Figure 7.2 we have 5 subtrees
(B,C,D,E,H)

Level 0
d Root
A

f Level 1
B C D

E F G H I J Level 2

K L M Level 3

Figure 7.2 Tree Nomenclature

7.3 TREE REPRESENTATIONS

Array Representation of Complete Binary Trees

The array representation can use a fixed-size or non-fixed array. For fixed-sized
array, the size of data structure is fixed during compilation and it does not change its size
during execution. For non fixed-sized array, it allows the array to grow or shrink as needed
during execution of program.
Figure 7.3 is an example of complete binary tree, it has ten nodes which contains a
single character. The ten characters that the tree contains can be stored in an array as shown
in the figure. We can consecutively place each node on the array based on their depth.
‘A’

‘B’ ‘C’

‘D’ ‘E’ ‘F’ ‘G’

‘H’ ‘I’ ‘J’

‘A’ ‘B’ ‘C’ ‘D’ ‘E’ ‘F’ ‘G’ ‘H’ ‘I’ ‘J’

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Figure 7.3 Representation of Binary Tree using Array

There are several reasons why the array representation is convenient:

1. The data from the root always appear in the [0] component of the array.

2. Suppose that the data for a non root node appears in component [i] of the array. Then
the data for its parent is always at location [(i - 1) / 2] (using integer division). For
example, using Figure 7.3, if i=6 then [(6-1)/2] = 2 where location [6] contains “G”. So
using the formula, the node parent of “G” is at location [2].

3. Suppose that the data for a node appears in component [i] of the array. Then its children
(if they exist) always have their data at the locations
Left child at component [2i + 1]
Right child at component [2i + 2]
For example, if i=2 , where location [2] contains “C”, then
Left child location [2(2) + 1] = 5, where location [5] contains “F”
Right child location [2(2) + 2] = 6 where location [6] contains “G”
These formulas make it easy to implement algorithm that traverse the tree, moving from
one node to another in various ways, processing data along the way.
7.4 Binary Trees
A binary tree is a tree in which no node can have more than two subtrees. In other words,
a node can have zero, one, or two subtrees [GILB98]. Figure 7.4 is an example of binary tree.
A binary tree with zero node is called a null tree.

B C

D E F G

Figure 7-4 BinaryLeft


TreeSubtree Right Subtree

Figure 7.4 Binary Tree

7.4.1 Example of Binary Trees

a. Full Binary Tree – every leaf has the same depth and every non leaf has two
children. See Figure 7.4, it is an example of full binary tree.

b. Complete Binary Tree – every depth except the deepest must contain as many
nodes as possible and at the deepest level all the nodes are as far as left as
possible. See Figure 7.5, it is an example of complete binary tree.

B C

D E F G

H I J
Figure 7.5 Complete Binary Tree

7.4.2 Binary Tree Structure


Each node in the structure must contain the data to be stored and two pointers, one
to the left subtree and one to the right subtree.

Node
Left subtree <pointer to node>
Data <data type>
Right subtree <pointer to node>
End Node

7.5 BINARY TREE TRAVERSALS

A binary tree traversal requires that each node of the tree be processed once and only once
in a predetermined sequence.

7.5.1 Preorder Traversal


In the preorder traversal, the root node is processed first, followed by left subtree,
and then the right subtree. Figure 7.6 illustrated the sequence of visited nodes indicated by
the number placed in each node.

1 A

2 C
B 5

3 D E F G
4 6 7

Figure 7.6 Preorder Traversal - ABDECFG

Preorder (root)
{
If (root is not null)
Process (root)
Preorder (root -> left subtree)
Preorder (root -> right subtree)
}
Algorithm 7.1 Preorder Traversal of a Binary Tree

7.5.2 Inorder Traversal

The inorder traversal processes the left subtree first, then the root, and finally the
right subtree. The meaning of prefix in is that the root is processed in between the subtrees.

4
B C

2 6
D E F G

1 3 5 7

Figure 7.7 Inorder Traversal - DBEAFCG

Inorder (root)
{
If (root is not null)
Inorder (root -> left subtree
Process (root)
Inorder (root -> right subtree)
}

Algorithm 7.2 Inorder Traversal of a Binary Tree


7.5.3 Postorder Traversal
The postorder traversal processes the root node after the left and right subtrees have
been processed. It starts by locating the leftmost leaf and processing it. It then processes its
right sibling, including its subtrees and finally, it processes the root node.

7
A

6
B 3 C

D E F G

1 2 4 5

Figure 7.8 Postorder Traversal - DEBFGCA

Postorder (root)
{
If (root is not null)
Postorder (root -> left subtree)
Postorder (root -> right subtree)
Process (root)
}

Algorithm 7.3 Postorder Traversal of a Binary Tree

7.6 EXPRESSION TREES


One interesting application of binary tree is expression tree. An expression is a sequence
of tokens that follow prescribed rules. A token maybe either an operand or an operator. The
standard arithmetic operators are +, -, *, /.

An expression tree is a binary tree with the following properties:


1. Each leaf is an operand.
2. The root and internal nodes are operators.
3. Subtrees are subexpressions with the root being an operator.

Figure 7.9 is an example of expression tree.


(a+b) * (c–d)

+ -

a b c d

Figure 7.9 An infix expression and its expression tree

Infix (tree)
* Print the infix expression for an expression tree
{
If (tree not empty)
If (tree-> token is an operand)
Print (tree-> token)
Else
Print (open parenthesis)
Infix (tree-> left)
Print (tree->token)
Infix(tree-> right)
Print ( close parenthesis)
}

Algorithm 7.4 Infix expression tree traversal

The above algorithm traverses the tree and prints the expression. To print an infix
expression, we must add an opening parenthesis at the beginning of each expression and a
closing parenthesis at the end of each expression. Because the root of the tree and each of its
subtrees represents a subexpression; we print the opening parenthesis whenever we start a tree
or subtree and the closing parenthesis when we have processed all of its children.
( * )

4
( ) ( )
+ -

2 6

a b c d
E
1 3 5 7

( (a+b) * (c–d) )

Figure 7.10 Infix Traversal of an Expression Tree

Quick Review

➢ Tree is a nonlinear structure which consists of nodes and branches.

➢ Binary tree is a tree in which no node can have more than two subtrees.

➢ Binary trees can be represented using array.

➢ There are three types of binary tree traversals namely, preorder, inorder, postorder.

➢ An expression tree is a binary tree which consist of mathematical operators and


operands.

You might also like