0% found this document useful (0 votes)
14 views36 pages

Tree 10 Oct 2023-1

This is tree and stack and queue information
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views36 pages

Tree 10 Oct 2023-1

This is tree and stack and queue information
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

tree

A tree is recursively defined as a set of one or more nodes where one node is
designated as the root of the tree and all the remaining nodes can be partitioned
into non-empty sets each of which is a sub-tree of the root.
Figure shows a tree where node A is the root node; nodes B, C, and D are
children of the root node and form sub-trees of the tree rooted at node A

Root node : The root node R is the topmost node in the tree.
If R = NULL, then it means the tree is empty.
Sub-trees : If the root node R is not NULL, then the trees T1 , T2 ,
and T3 are called the sub-trees of R.
Leaf node : A node that has no children is called the leaf node or
the terminal node.
Path : A sequence of consecutive edges is called a path. For
example, in Fig. the path from the root node A to
node I is given as: A, D, and I.
Ancestor node : An ancestor of a node is any predecessor node on the
path from root to that node. The root node does not have
any ancestors. In the tree given in Fig. nodes A, C, and G
are the ancestors of node K
Descendant node : A descendant node is any successor node on any path
from the node to a leaf node. Leaf nodes do not have any
descendants. In the tree given in Fig. nodes C, G, J, and K
are the descendants of node A.

Level number : Every node in the tree is assigned a level number in


such a way that the root node is at level 0, children of
the root node are at level number 1. Thus, every node is
at one level higher than its parent. So, all child nodes
have a level number given by parent’s level number + 1.
Degree
Degree : Degree of a node is equal to the number of children
that a node has. The degree of a leaf node is zero.
In-degree : In-degree of a node is the number of edges arriving
at that node
Out-degree : Out-degree of a node is the number of edges leaving
that node
TYPES OF TREES
Trees are of following 6 types:
1. General trees
2. Forests
3. Binary trees
4. Binary search trees
5. Expression trees
6. Tournament trees

1.General trees are data structures that store elements hierarchically.


The top node of a tree is the root node and each node, except the
root, has a parent. A node in a general tree (except the leaf nodes)
may have zero or more sub-trees. General trees which have 3 sub-
trees per node are called ternary trees. However, the number of
sub-trees for any node may be variable. For example, a node can
have 1 sub-tree, whereas some other node can have 3 sub-trees.

2 . Forests A forest is a disjoint union of trees. A set of disjoint trees (or


forests) is obtained by deleting the root and the edges connecting
the root node to nodes at level 1.

We can convert a forest into a tree


by adding a single node as
the root node of the tree.
For example, Fig. (a) shows
a forest and Fig.(b) shows
the corresponding tree.
Similarly, we can convert a
general tree into a forest
by deleting the root node of the tree.
3. Binary Trees: A binary tree is a data structure that is defined as a
collection of elements called nodes. In a binary tree, the topmost
element is called the root node, and each node has 0, 1, or at the
most 2 children.
A node that has zero children is called a leaf node or a
terminal node. Every node contains a data element, a left
pointer which points to the left child, and a right pointer
which points to the right child. The root element is pointed by
a 'root' pointer. If root = NULL, then it means the tree is
empty.

Figure shows a binary tree. In the figure, R is the root node and
the two trees T1 and T2 are called the left and right sub-trees of
R. T1 is said to be the left successor of R. Likewise, T2 is called
the right successor of R. Note that the left sub-tree of the root
node consists of the nodes: 2, 4, 5, 8, and 9. Similarly, the right
sub-tree of the root node consists of nodes: 3, 6, 7, 10, 11, and
12. In the tree, root node 1 has two successors: 2 and 3. Node 2
has two successor nodes: 4 and 5. Node 4 has two successors: 8
and 9. Node 5 has no successor. Node 3 has two successor
nodes: 6 and 7. Node 6 has two successors: 10 and 11. Finally,
node 7 has only one successor: 12. A binary tree is recursive by
definition as every node in the tree contains a left sub-tree and a
right sub-tree. Even the terminal nodes contain an empty left
sub-tree and an empty right sub-tree. Look at Fig. nodes 5, 8, 9,
10, 11, and 12 have no successors and thus said to have empty
sub-trees
Parent If N is any node in T that has
left successor S1 and right
successor S2, then N is called
the parent of S1 and S2.
Correspondingly, S1 and S2
are called the left child and
the right child of N. Every
node other than the root
node has a parent.

Level number
Every node in
the binary tree is
assigned a level
number (refer Fig.
The root node is
defined to be at level
0. The left and the
right child of the root
node have a level
number 1. Similarly,
every node is at one
level higher than its parents. So all child nodes are defined to
have level number as parent's level number + 1.

Degree of a node It is equal to the number of children that a node has. The degree of a leaf
node is zero. For example, in the tree, degree of node 4 is 2, degree of node 5 is
zero and degree of node 7 is 1. Sibling All nodes that are at the same level and
share the same parent are called siblings (brothers). For example, nodes 2 and 3;
nodes 4 and 5; nodes 6 and 7; nodes 8 and 9; and nodes 10 and 11 are siblings

Leaf node A node that has no children is called a leaf node


or a terminal node. The leaf nodes in the tree
are: 8, 9, 5, 10, 11, and 12.

Similar binary trees Two binary trees T and T¢ are said to


be similar if both these trees have the same
structure. Figure 9.5 shows two similar binary
trees.

Copies Two binary trees T and T¢ are said to be copies if they have similar structure and if they
have same content at the corresponding nodes. Figure 9.6 shows that T¢ is a copy
of T.

Edge It is the line connecting a node N to any of its successors. A binary tree of n nodes has
exactly n – 1 edges because every node except the root node is connected to its
parent via an edge.
Path A sequence of consecutive edges. For example, in Fig. 9.4, the path from the root node to
the node 8 is given as: 1, 2, 4, and 8.

Depth The depth of a node N is given as the length of the path from the root R to the node N.
The depth of the root node is zero.

Height of a tree It is the total number of nodes on the path from the root node to the
deepest node in the tree. A tree with only a root node has a height of 1. A binary
tree of height h has at least h nodes and at most 2h – 1 nodes. This is because
every level will have at least one node and can have at most 2 nodes. So, if every
level has two nodes then a tree with height h will have at the most 2h – 1 nodes as
at level 0, there is only one element called the root. The height of a binary tree
with n nodes is at least log2 (n+1) and at most n.

In-degree/out-degree of a node It is the number of edges arriving at a node. The root node
is the only node that has an in-degree equal to zero. Similarly, out-degree of a
node is the number of edges leaving that node. Binary trees are commonly used to
implement binary search trees, expression trees, tournament trees, and binary
heaps

Complete Binary Trees A complete binary tree is a binary tree that satisfies two
properties. First, in a complete binary tree, every level, except possibly the last, is
completely filled. Second, all nodes appear as far left as possible. In a complete
binary tree Tn , there are exactly n nodes and level r of T can have at most 2r
nodes. Figure 9.7 shows a
complete binary tree.
Note that in Fig. 9.7, level
0 has 20 = 1 node, level 1
has 21 = 2 nodes, level 2
has 22 = 4 nodes, level 3
has 6 nodes which is less
than the maximum of 23 =
8 nodes. In Fig. 9.7, tree
T13 has exactly 13 nodes.
They have been purposely
labelled from 1 to 13, so
that it is easy for the
reader to find the parent
node, the right child node,
and the left child node of
the given node. The formula can be given as—if K is a parent node, then its left
child can be calculated as 2 × K and its right child can be calculated as 2 × K + 1. For
example, the children of the node 4 are 8 (2 × 4) and 9 (2 × 4 + 1). Similarly, the
parent of the node K can be calculated as | K/2 |. Given the node 4, its parent can
be calculated as | 4/2 | = 2. The height of a tree Tn having exactly n nodes is given
as: Hn = | log2 (n + 1)

Extended Binary Trees A binary tree T is said to be an extended binary tree (or a 2-
tree) if each
node in the tree
has either no
child or exactly
two children.
Figure 9.8
shows how an
ordinary binary
tree is
converted into
an extended
binary tree.

In an extended binary tree,


nodes having
two children are called internal nodes and nodes having no children are called
external nodes. In Fig. 9.8, the internal nodes are represented using circles and the
external nodes are represented using squares. To convert a binary tree into an
extended tree, every empty sub-tree is replaced by a new node. The original nodes
in the tree are the internal nodes, and the new nodes added are called the
external nodes.

Binary Search Trees


A binary search tree, also known as an ordered
binary tree, is a variant of binary
tree in which the nodes are
arranged in an order.
Expression Trees Binary trees are widely used to store algebraic expressions.
For example, consider the algebraic expression given as:

Exp = (a – b) + (c * d) This
expression can be represented
using a binary tree as shown in
Fig. 9.13
Expression for the above binary tree is [{(a/b) + (c*d)} ^ {(f % g)/(h – i)}

Example Given the expression, Exp = a + b / c * d – e, construct the


corresponding binary tree. Solution Use the operator
precedence chart to find the sequence in which operations
will be performed. The given expression can be written as
Exp = ((a + ((b/c) * d)) – e)

Example 4 Marks Summer 2019

Example 4 Marks Winter 2019


Define the following term w.r.t. tree: (i) In-degree (ii) out-
degree.

In -degree: Number of edges coming


towards node is in-degree of node.
For e.g. : In degree of node B is 1
Out -degree: Number of edges going
out from node is out -degree of node.
For e.g. Out Degree of is node D is 2

Questions asked in MSBTE


Exam
Sr General tree Binary Tree
No
1 Stack is a data structure in which insertion Queue is a data structure in which insertion
and deletion operations are performed at and deletion operations are performed at
same end. different ends
2 In general tree, root has in degree 0 and In binary tree, root has in degree 0 and
maximum out degree n. maximum out degree 2.
3 In general tree, each node have in-degree one In binary tree, each node have in-degree one and
and maximum out-degree n. maximum out-degree 2.
4 Subtree of general tree are not ordered Subtree of binary tree is ordered.
5
TRAVERSING A BINARY TREE Traversing a binary tree is the process of visiting
each node in the tree exactly once in a systematic way.

Pre-order Traversal To traverse a non-empty binary tree in pre-order, the


following operations are performed recursively at
each node. The algorithm works by:

1. Visiting the root node,


2. Traversing the left sub-tree, and finally
3. Traversing the right sub-tree

In-order Traversal To traverse a non-empty binary tree in in-order, the following

operations are performed recursively at each node.


The algorithm works by:
1. Traversing the left sub-tree,
2. Visiting the root node, and finally
3. Traversing the right sub-tree.

Post-order Traversal To traverse a non-empty binary tree in post-order, the


following operations are performed
recursively at each node. The algorithm
works by:
1. Traversing the left sub-tree,
2. Traversing the right sub-tree, and finally
3. Visiting the root node
Example 1
Pre-order Traversal :
: A, B, D, G, H, L, E, C, F, I, J, K
In-order Traversal :
G, D, H, L, B, E, A, C, I, F, K, J
Post-order Traversal
G, L, H, D, E, B, I, K, J, F, C, A
Example 2
Pre-order Traversal
: A, B, D, C, E, F, G, H, I
In-order Traversal :
B, D, A, E, H, G, I, F, C
Post-order Traversal
D, B, H, I, G, F, E, C, A
Example 3 Winter 2018 6 Marks
Pre-order Traversal:
A, B, D, E, Q, F, R, H, C, I, J, K, L, P

IN-order Traversal :
Q, E, F, R, D, H, B, A, I, J, K, C, L, P

Post-order Traversal
Q, R, F, E, H, D, B, K, J, I, P, L, C, A

Example 4 Summer 2019 6 Marks

Pre-order Traversal:

36,25,20,10,1,15,22,32,48,43,56,50,60,58,75

IN-order Traversal :1,10,15,20,22,25,32,36,43,48,50,56,58,60,75


Post-order Traversal
1,15,10,22,20,32,25,43,50,58,75,60,56,48,36

Example 5

Pre-order Traversal:
40, 30, 25, 15, 28, 35, 50, 45, 60, 55, 70

IN-order Traversal :
15, 25, 28, 30, 35, 40, 45, 50, 55, 60, 70

Post-order Traversal :
15, 28, 25, 35, 30, 45, 55, 70, 60, 50, 40

Example 6

Pre-order Traversal:
50, 17, 12, 9, 14, 23, 19, 72, 54,67,76,

IN-order Traversal :
9, 12, 14, 17, 23, 19, 50, 54 ,67, 72, 76

Post-order Traversal :
9, 14, 12, 19, 23, 17, 67, 54, 76, 72, 50

Example 7

Pre-order Traversal:
1, 2, 5, 6, 3, 8, 7

IN-order Traversal :
5, 2, 6, 1, 8, 3, 7

Post-order Traversal :
5, 6, 2, 8, 7, 3, 1

Example 8
Pre-order Traversal:
100, 20, 10, 30, 200, 150, 300

IN-order Traversal :
10, 20, 23, 100, 150, 200, 300

Post-order Traversal :
10, 30, 20, 150, 300, 200, 100
Example 9

Pre-order Traversal:

30, 20, 10, 15, 25, 23, 39, 35, 42

IN-order Traversal :
10,15, 20, 23, 25, 30, 35, 39, 42 Post-
order Traversal :
15 , 10 , 23 , 25, 20, 35, 42, 39, 30

Example 10
Pre-order

Traversal:
A, B, D E, C, F, G

IN-order Traversal :
D, B, E, A, F, C, G

Post-order Traversal :
15 , 10 , 23 , 25, 20, 35, 42, 39, 30

Example 11

Pre-order Traversal:
A, B, C, G, D, E, F

IN-order Traversal :
G, C, B, D, A F, E

Post-order Traversal :
G, C D, B, F, E, A
Example 12
Pre-order Traversal:
A, B, C, E, D, F, G, H, J, I, K

IN-order Traversal :
E, C, B, D, A, F, J, H, G , I, K

Post-order Traversal :
E, C, D, B, J, H, K, I, G, F, A

Example 13
Example 14
Example 15
Example 16

Example 17
Example 18
Example 19

Example 20
Example 21

Example 22
Example 23

Example 24
Example 25

Example 26 27
Stepwise construction of Binary search tree for following elements: 30,100,90,15,2,25,36,72,78,10 is
as follows:

You might also like