You are on page 1of 35

Unit III

Tree Basic Concepts and Representation

8/28/16

Department of Computer Science


and Engineering,vignan university

1.Introduction
It is non-linear, two dimensional data structure which represents
hierarchical relationships between its individual data items.
Typically there is one node in the top row of a tree, with lines
connecting to more nodes on the second row, even more on the third,
and so on.
Thus trees are small on the top and large on the bottom.
A tree T is a set of nodes storing elements such that the nodes have a
parent-child relationship that satisfies the following condition .

It has a special data item referred as root node that has no


parent.
All remaining data item are partitioned into number of subsets,
each of which it is a tree, referred as sub-trees or leaf nodes.
8/28/16

Department of Computer Science


and Engineering,vignan university

2. Basic Tree Concepts


Node: A tree consists of finite set of elements.
Branches: A finite set of directed lines, called branches, which connect
the nodes.
Degree of the node: The number of branches associated with a node is the
degree of the node.
Indegree: When the branch is directed toward the node, it is an indegree
branch. The indegree of the root is, by definition zero. With the exception
of the root, all of the nodes in a tree must have an indegree of exactly one.
Outdegree: When the branch is directed away from the node, it is an
outdegree branch. All nodes in the tree can have zero, one, or more
branches leaving them; that is, they may have an outdegree of zero, one or
more.
Note: The sum of indegree and outdegree branches is the degree of the node.
8/28/16

Department of Computer Science


and Engineering,vignan university

Basic Tree Concepts (Contd.)


Root: if the tree is not empty, then the first node is called the root
node. Fig.3-1 is a representation of a tree.

8/28/16

Department of Computer Science


and Engineering,vignan university

Basic Tree Structure

8/28/16

Department of Computer Science


and Engineering,vignan university

Degree

Indegree
Indegree of node B =1
Indegree of node C = 1
Outdegree
Outdegree of node B = 3
Outdegree of node C = 2

8/28/16

Department of Computer Science


and Engineering,vignan university

Root 3. Basic Terminologies examples


In a tree data
structure, the first node
is called as Root Node.
Every tree must have
root node. We can say
that root node is the
origin of tree data
structure.
In any tree, there
must be only one root
node. We never have
multiple root nodes in a
tree.
8/28/16

Department of Computer Science


and Engineering,vignan university

(Continued..)
Edge
In a tree data
structure, the connecting
link between any two
nodes
is
called
as EDGE. In a tree with
'N' number of nodes
there will be a maximum
of 'N-1' number of
edges.

8/28/16

Department of Computer Science


and Engineering,vignan university

(Continued..)
Parent
In a tree data structure, the
node which is predecessor of
any node is called as Parent
node.
A node is a parent if it has
successor nodes- that is, if it has
an outdegree greater than zero.
In simple words, the node
which has branch from it to any
other node is called as parent
node.
Parent node can also be
defined as "The node which has
child / children".

8/28/16

Department of Computer Science


and Engineering,vignan university

(Continued..)
Child
In a tree data structure, the
node which is descendant of any
node is called as CHILD Node.
In simple words, the node
which has a link from its parent
node is called as child node.
A node is a parent if it has
predecessor nodes- that is, if it
has an indegree of one.
In a tree, any parent node
can have any number of child
nodes.
In a tree, all the nodes
except root are child nodes.

8/28/16

Department of Computer Science


and Engineering,vignan university

10

(Continued..)
Siblings
In a tree data
structure, nodes which
belong to same Parent
are called as Siblings.
Two or more nodes
with the same parent are
siblings.
In simple words, the
nodes with same parent
are called as Sibling
nodes.

8/28/16

Department of Computer Science


and Engineering,vignan university

11

(Continued..)

Leaf or External node


A leaf is any node
with an outdegree of
zero, i.e. it has no
children.

8/28/16

Department of Computer Science


and Engineering,vignan university

12

(Continued..)

Internal node
A node that is not a
root or a leaf is known
as an internal node
because it is found in the
middle portion of a tree.

8/28/16

Department of Computer Science


and Engineering,vignan university

13

(Continued..)
Level of a node
The level of a node
is its distance from the
root.
Because the root has
a zero distance from
itself, the root is at level
0. The children of the
root are at level 1, their
children are level 2, and
so forth.

8/28/16

Department of Computer Science


and Engineering,vignan university

14

(Continued..)
Height
In a tree data
structure,
the
total
number of edges from
leaf node to a particular
node in the longest path
is called as HEIGHT of
that Node.
In a tree, height of
the root node is said to
be height of the tree.
In a tree, height of
all leaf nodes is '0'.
8/28/16

Department of Computer Science


and Engineering,vignan university

15

(Continued..)
Depth
In a tree data structure,
the total number of edges from
root node to a particular node
is called as DEPTH of that
Node.
In a tree, the total number
of edges from root node to a
leaf node in the longest path is
said to be Depth of the tree.
In simple words, the
highest depth of any leaf node
in a tree is said to be depth of
that tree.
In a tree, depth of the root
node is '0'.
8/28/16

Department of Computer Science


and Engineering,vignan university

16

(Continued..)
Path
In a tree data structure,
the sequence of Nodes and
Edges from one node to
another node is called
as PATH between that two
Nodes.
Length of a Path is
total number of nodes in
that path.
In example the path A
- B - E - J has length 4.

8/28/16

Department of Computer Science


and Engineering,vignan university

17

(Continued..)
Subtree
A tree may be divided into
subtrees. A subtree is any connected
structure below the root.
The first node in a subtree is
known as the root of the subtree and
is used to name the subtree.
Furthermore, subtrees can be
subdivided into subtrees.
The concept of subtrees leads
us to a recursive definition of a tree:
a tree is a set of nodes that either (1)
is empty, or (2) has a designated
node, called the root, from which
hierarchically descend zero or more
subtrees, which are also trees.
In Example, {B, D, E, F, I, J},
{E, I, J}, {C, G, H, K} are the
subtrees.

8/28/16

Department of Computer Science


and Engineering,vignan university

18

Length of a path =
number of edges
Depth of a node N =
length of path from root to N
Height of node N =
length of longest path from N
to a leaf
Depth and height of tree
= height of root
In this example Depth =0
and height =2 for root node A.
Depth =2 and height = 0
for leaf nodes C,D, G,H and I.

8/28/16

Figure 3-1 A Tree

Department of Computer Science


and Engineering,vignan university

19

Some other terminologies

In addition to root, many different terms are used to describe the


attributes of a tree.
Ancestor: It is any node in the path from the root to the node.
Descendent of the node: It is any node in the path below the parent
node; that is, all nodes in the paths from a given node to a leaf are
descendents of the node. Figure 3-2 shows the usage of these terms.
Forest: A set of trees is called forest; if we remove the root of a tree
we get a forest. In the below fig, 3-3(a) if we remove A, we get a
forest with three trees that shown in fig. 3-3 (b).

8/28/16

Department of Computer Science


and Engineering,vignan university

20

Parents: A, B, F
Leaves: C, D, G, H, I
Children: B, E, F, C, D, G, H,
I
Internal Nodes: B, F
Siblings: {B, E, F}, {C, D},
{G, H, I} Root: A
Degree of the node A is 3.
Ancestor of node H is
A, F.
Indegree of the node F is 1
and outdegree of the node F is
3.

8/28/16

Figure 3-2 Tree nomenclature

Department of Computer Science


and Engineering,vignan university

21

Forest

Figure 3-3(a): Tree

8/28/16

Figure 3-3(b): Forest (Sub-trees)

Department of Computer Science


and Engineering,vignan university

22

4. Applications of Trees
1. Directory tree
Organizes directories and files hierarchically.
Directories are internal nodes; files are leaf nodes (usually).
2. Class hierarchy
Object is root, other classes are descendents.
3. Decision tree
Binary tree.
Path taken determined by Boolean expression.
4. Expression tree
Operators are internal nodes, variables and constants are leaf
nodes.
8/28/16

Department of Computer Science


and Engineering,vignan university

23

5. Tree methods in Java

Whatever the implementation of a tree is, its interface is the following:


root( ): returns root
parent(v): returns parent of v
children(v): returns iterator of children of v
size( ): returns number of nodes
elements( ): returns iterator of all elements
positions( ): returns iterator of all positions/nodes
swapElements(v,w): swaps elements at two nodes.
replaceElement(v,e): replaces element of a node
isInternal(v): test if node v is internal or not.
isExternal(v): test if node v is external or not.
isRoot(v): test if node v is root or not.
8/28/16

Department of Computer Science


and Engineering,vignan university

24

6. Tree Representation
A tree data structure can be represented in two methods. Those
methods are as follows,
List representation
Left Child-Right sibling representation
Consider the following tree shown in figure 3-4
In any tree with N nodes there will be maximum of N-1 edges. In
a tree every individual element is called as node.

8/28/16

Department of Computer Science


and Engineering,vignan university

25

Figure 3-4: Tree with 11 nodes and 10 edges

8/28/16

Department of Computer Science


and Engineering,vignan university

26

6.1 List Representation

In this representation, we use two types of nodes one for


representing the node with data and another for representing only
references.
We start with a node with data from root node in the tree.
Then it is linked to an internal node through a reference node and is
linked to any other node directly.
This process repeats for all the nodes in the tree.
The above tree shown in fig.3-5 is example can be represented using
list representation as follows..

8/28/16

Department of Computer Science


and Engineering,vignan university

27

Figure 3-5: List Representation

8/28/16

Department of Computer Science


and Engineering,vignan university

28

6.2 Left Child - Right Sibling Representation

In this representation, we use list with one type of node


which consists of three fields namely Data field, Left child reference
field and Right sibling reference field shown in the figure 3-6.
Data field stores the actual value of a node, left reference
field stores the address of the left child and right reference field
stores the address of the right sibling node. Graphical representation
of that node is as follows...
Figure 3-6: Format of Left Child - Right Sibling Representation

8/28/16

Department of Computer Science


and Engineering,vignan university

29

(Continued..)
In this representation, every node's data field stores the actual value
of that node.
If that node has left child, then left reference field stores the address
of that left child node otherwise that field stores NULL.
If that node has right sibling then right reference field stores the
address of right sibling node otherwise that field stores NULL.
The above tree example 3-4 can be represented using Left Child Right Sibling representation as follows shown in the figure 3-7.

8/28/16

Department of Computer Science


and Engineering,vignan university

30

Figure 3-7: Left Child - Right Sibling Representation

8/28/16

Department of Computer Science


and Engineering,vignan university

31

Properties of a tree
Any node can be the root of the tree and each node in a tree has the
property that there is exactly one path connecting that node with
every other node in the tree.
The tree in which the root is identified is called a rooted tree; and a
tree in which the root is not identified is called a free tree.
Each node, except the root, has a unique parent.

8/28/16

Department of Computer Science


and Engineering,vignan university

32

Questions
1.
2.
3.

Explain tree definition in terms of data structure.


Define Degree of the node.
Find the In-degree and out-degree of all nodes in the tree given below

8/28/16

Department of Computer Science


and Engineering,vignan university

33

(Continued..)
Explain briefly about the basic terminologies of the tree with
example.
Explain briefly about the representation of the tree.

8/28/16

Department of Computer Science


and Engineering,vignan university

34

Basic Tree concepts and BST

8/28/16

Department of Computer Science


and Engineering,vignan university

35

You might also like