DS Mod4
DS Mod4
Module IV : Trees
[Type here]
DSA Module 18CS32- IV- Tree Data Structure
Contents
1. Introduction
2. Concept of Tree
3. Tree Terminology
4. Types of Trees
5. Binary Tree
6. Properties of Binary Trees
7. Binary Tree Representations
8. Linked Representation
9. Binary Tree Traversals Types
10. Binary Tree Traversals Functions
10.1. Inorder Tree Traversal
10.2. Preorder Tree Traversal
10.3. Postorder Tree Traversal
10.4. Level Order Traversal (Top to Bottom, Left to Right)
11. Binary Search Tree .
12. Inserting Into A Binary Search Tree
13. Searching A Binary Search Tree
14. Expression Trees
Example
15. Algebraic expressions
Boolean expressions
16. Write a Program to Create a tree for Postfix Expression and Evaluate the
Expression using Binary Tree. Also Print the Expression in Infix, Postfix, Prefix form
using Inorder, Postorder and Preorder respectively.
17. Function for finding Height of a Binary Tree
18. Function for counting the number of leaf nodes and internal nodes
19. Questions on Tree
Credits:GAT
1
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
Module IV :Trees
1. Introduction
A tree structure consists of nodes and edges that organize data in a hierarchical
fashion. The relationships between data elements in a tree are similar to those of a
family tree: “child," “parent”, "ancestor," etc. The data elements are stored in nodes
and pairs of nodes are connected by edges. The edges represent the relationship
between the nodes that are linked with arrows or directed edges to form a
hierarchical structure resembling an upside-down tree complete with branches,
leaves, and even a root.
Formally, we can define a tree as a set of nodes that either is empty or has a node
called the root that is connected by edges to zero or more subtrees to form a
hierarchical structure. Each subtree is itself by definition a tree. A classic example of a
tree structure is the representation of directories and subdirectories in a UNIX file
system. The top tree in Figure 13.1 illustrates the hierarchical nature of a student's
home directory in the UNIX file system. Trees can be used to represent structured
data, which results in the subdivision of data into smaller and smaller parts.
A simple example of this use is the division of a book into its various parts of chapters,
sections, and subsections, as illustrated by the bottom tree in Figure 1. Trees are also
used for making decisions. One that you are most likely familiar with is the phone, or
menu, tree. When you call customer service for most businesses today, you are
greeted with an automated menu that you have to traverse. The various menus are
nodes in a tree and the menu options from which you can choose are branches to
other nodes. Some of the examples are shown below.
2
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
3
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
2. Concept of Tree
It implies that we organize the data so that items of information are related by
the branches.
Definition: A tree is a finite set of one or more nodes such that:
1. There is a specially designated node called the root.
2. The remaining nodes are partitioned into n ≥ 0 disjoint sets T1,…, Tn ,
where each of these sets is a tree. We call T1,…, Tn the subtrees of the
root.
Binary Tree Representation in memory Using Doubly Linked List
3. Tree Terminology
Root of the tree:
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.
Node: It is stands for the item of information and the branches to other nodes.
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.
4
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
Parent: node: a node that has subtrees is the parent of the roots of the
subtrees. In a tree data structure, the node which is predecessor of any node is
called as PARENT NODE. 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".
Child node: a node that is the roots of the subtrees are the children of the
node. 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. In a tree, any parent node can have any
number of child nodes. In a tree, all the nodes except root are child nodes.
5
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
Siblings: In a tree data structure, nodes which belong to same Parent are
called as SIBLINGS. In simple words, the nodes with same parent are called as
Sibling nodes.
Leaf: In a tree data structure, the node which does not have a child is called as
LEAF Node. In simple words, a leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes.
External node is also a node with no child. In a tree, leaf node is also called as
'Terminal' node.
Internal Nodes: In a tree data structure, the node which has atleast one child
is called as INTERNAL Node. In simple words, an internal node is a node with
atleast one child.
In a tree data structure, nodes other than leaf nodes are called as Internal
Nodes. The root node is also said to be Internal Node if the tree has more
than one node. Internal nodes are also called as 'Non-Terminal' nodes.
6
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
Degree: The degree of a node: It is the number of subtrees of the node.
In a tree data structure, the total number of children of a node is called as
DEGREE of that Node. In simple words, the Degree of a node is total number of
children it has. The highest degree of a node among all the nodes in a tree is
called as 'Degree of Tree'
The degree of a tree: It is the maximum degree of the nodes in the tree
Height: In a tree data structure, the total number of egdes 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'.
7
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
Depth: In a tree data structure, the total number of egdes 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'.
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 below example the path A - B - E - J
has length 4.
8
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
The depth of a tree: It also called height of a tree. It is the maximum level of
any node in the tree
Node A is the parent of node B if node B is a child of A
Node A is an ancestor of node B if A is a parent of B, or if some child of A is an
ancestor of B
In less formal terms, A is an ancestor of B if B is a child of A, or a child of a
child of A, or a child of a child of a child of A, etc.
Node B is a descendant of A if A is an ancestor of B
Nodes A and B are siblings if they have the same parent
The size of a binary tree is the
number of nodes in it
This tree has size 12
The depth of a node is its distance
from the root
a is at depth zero
e is at depth 2
The depth of a binary tree is the
depth of its deepest node
This tree has depth 4
9
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
4. Types of Trees
General Tree, Full Tree, Complete Tree, Binary Tree, Binary Search Tree, Heap
1. A complete binary tree (sometimes proper binary tree or 2-tree) is a tree in
which every node other than the leaves has two children.
2. Almost complete binary tree is a binary tree in which every level, except
possibly the last, is completely filled, and all nodes are as far left as possible.
3. Strictly Binary Tree: A tree where nodes should have either 0 or 2 child. Not
mandatory to put from left to right can be in any order
4. Binary Search Tree
10
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
A Binary Search Tree (BST) is a tree in which all the nodes follow the
below-mentioned properties −
● The left sub-tree of a node has a key less than or equal to its parent
node's key.
● The right sub-tree of a node has a key greater than or equal to its parent
node's key.
The representation of node in the tree structure:
data
left child right child
Define Structure Node for the tree:
typedef struct node *ptr_node;
sturct node
{
int data;
sturct node *left;
sturct node *right;
};
General Tree Graphical Picture:
11
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
5. Binary Tree
Definition: A binary tree is a finite set of nodes that is either empty or consists
of a root and two disjoint binary trees called the left subtree and the right
subtree.
Binary Tree Types:
Regular Binary Tree ( 2 )
Skewed Left Binary Tree ( 1 )
Skewed Right Binary Tree ( 3 )
Three Graphical Pictures of the Binary Tree:
6. Properties of Binary Trees
In particular, we want to find out the maximum number of nodes in a binary
tree of depth k, and the number of leaf nodes and the number of nodes of
degree two in a binary tree. We present both these observations as lemma.
Lemma 5.1 [Maximum number of nodes]:
(1) The maximum number of nodes on level i of a binary tree is 2i - 1, i ≥ 1
(2) The maximum number of nodes in a binary tree of depth k is 2k – 1, k ≥ 1
Proof:
(i) The proof is by induction on i.
Induction Base: The root is the only node on level i= 1. Hence the maximum
number of nodes on level
12
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
i = 1 is 2 = 2i - 1.
Induction Hypothesis: For all j, 1 j < i, the maximum number of nodes on
level j is 2j - 1.
Induction Step: The maximum number of nodes on level i - 1 is 2i - 2, by the
induction hypothesis. Since each node in a binary tree has maximum degree 2,
the maximum number of nodes on level i i s 2 times the maximum number on
level i-1 or 2i -1.
(ii) The maximum number of nodes in a binary tree of depth k is (maximum
number of nodes on level i. Next, let us examine the relationship between the
number of terminal nodes and the number of nodes of degree 2 in a binary
tree.
Lemma 5.2 [Relation between number of leaf nodes and nodes of degree
2]: Nonempty binary tree, T, if n0 is the number of leaf nodes and n2 the
number of nodes of degree 2, then n0 = n 2 + 1.
Proof: Let n1 be the number of nodes of degree 1 and n t he total number of
nodes. Since all nodes in T are of degree 2 we have:
n = n0 + n1 + n2
(1)
If we count the number of branches in a binary tree, we see that every node
except for the root has a branch leading into it. If B i s the number of branches,
then n = B + 1. All branches emanate either from a node of degree one or from
a node of degree 2. Thus, B = n1 + 2n2. Hence, we obtain
n = 1 + n1 + 2n2
(2)
Subtracting (2) from (1) and rearranging terms we get
n0 = n2 + 1
7. Binary Tree Representations
Array Representation
The numbering scheme used in it suggests out first representation of a binary
tree in memory. Since the nodes are numbered from 1 to n, we can use a
one-dimensional array to store the nodes. (We do not use the 0th position of
the array.) Using Lemma 5.1 we can easily determine the locations of the
parent, left child, and right child of any node, i, in the binary tree.
Lemma 5.3:
If a complete binary tree with n nodes (depth = ⎣ log2n +1 ⎦ ) is represented
sequentially, then for any node with index i, 1 ≤ i ≤ n, we have:
(1) parent ( i ) is at ⎣ i / 2 ⎦ if i ≠ 1 if i = 1, i is at the root and has no parent
13
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
(2) left_child( i ) is at 2i if 2i ≤ n. If 2i > n, then i has no left child
child
8. Linked Representation
While the sequential representation is acceptable for complete binary trees, it
wastes space for many other binary trees. In, addition, this representation
suffers from the general inadequacies of other sequential representations.
Thus, insertion or deletion of nodes from the middle of a tree requires the
movement of potentially many nodes to reflect the change in the level of these
nodes. We can easily overcome these problems by using a linked
representation. Each node has three fields, left_child, data, and right_child as
two pictures show the node representation of the binary tree below:
Binary Tree Traversals
There are many operations that we can perform on tree, but one that arises
frequently is traversing a tree, that is, visiting each node in the tree exactly
once. A full traversal produces a linear order for the information in a tree.
9. Binary Tree Traversals Types
Inorder Traversal (Left, Parent, Right)
Preorder Traversal (Parent, Left, Right)
Postorder Traversal (Left, Right, Parent)
Level Order Traversal (Top to Bottom, Left to Right)
Example of the Binary Tree:
14
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
10. Binary Tree Traversals Functions
10.1. Inorder Tree Traversal
Recursive function:
void inorder(NODE root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d ",root->info);
inorder(root->right);
}
}
Result of binary tree example:
H, D, I, B, J, E, K, A, L, F, M, C, N, G, O
10.2. Preorder Tree Traversal
Recursive function:
void preorder(NODE root)
{
if(root!=NULL)
{ printf("%d ",root->info);
preorder(root->left);
preorder(root->right);
}
}
Result of binary tree example:
A, B, D, H, I, E, J, K, C, F, L, M, G, N, O
15
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
16
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
12. Inserting Into A Binary Search Tree
To insert a new element, key, we must first verify that the key is different from
those of existing elements. To do this we search the tree. If the search is
unsuccessful, then we insert the element at the point the search terminated.
Node Defintion
struct node
{
int info;
struct node *left,*right;
};
typedef struct node *NODE;
Tree Creation – Function to insert into tree
NODE insert(NODE root,int data)
{
NODE newnode, parent, cur;
newnode=(NODE)malloc(sizeof(struct node));
newnode->left=newnode->right=NULL;
newnode->info=data;
if(root==NULL)
{
root=newnode; return root;
}
parent=NULL; cur=root;
while(cur!=NULL)
{
if(cur->info==data)
{
printf("\nNode is already present in the tree\n");
return(root);
17
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
}
if(cur->info < data)
{ parent=cur; cur=cur->right; }
else
{ parent=cur; cur=cur->left; }
}
if(parent->info<data)
parent->right=newnode;
else
parent->left=newnode;
return(root);
}
13. Searching A Binary Search Tree
Suppose we wish to search for an element with a key. We begin at the root. If
the root is NULL, the search tree contains no elements and the search is
unsuccessful. Otherwise, we compare key with the key value in root. If key
equals root’s key value, then the search terminates successfully. If key is less
than root’s key value, then no elements in the right subtree subtree can have a
key value equal to key. Therefore, we search the left subtree of root. If key is
larger than root’s key value, we search the right subtree of root.
Recursive Function for Binary Search Tree:
NODE* search (NODE* root, int key )
{
if ( root==NULL )
return NULL;
if ( key = = root->data )
return root;
if ( key < root->data )
return search ( root->left_child, key );
return search ( root->right_child, key );
}
Iterative Search Function for Binary Search Tree
void search_node(NODE root, int key)
{
NODE cur,parent,successor;
if(root==NULL)
{
printf("\nTree is empty\n");
return ;
}
cur=root;
18
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
while(cur!=NULL)
{
if(key==cur->info)
break;
parent=cur;
if(key < cur->info)
cur=cur->left;
else
cur=cur->right;
}
if(cur==NULL)
{
printf("\nData is not found\n");
return ;
}
printf("\nData %d is found\n",key);
return ;
}
int main()
{
NODE root=NULL;
NODE create(NODE,int);
NODE delete_node(NODE,int);
//clrscr();
while(1)
{
printf("1:Create Tree\n");
printf("2:TREE TRAVERSAL\n");
printf("3.SEARCH\n");
printf("4.Level_Order\n");
printf("5.EXIT\n");
printf("\nEnter your choice=\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\nEnter data to be inserted\n");
scanf("%d",&data);
root=create(root,data);
break;
case 2 : if(root==NULL)
printf("\nEMPTY TREE\n");
else
{ printf("\nThe inorder display : ");
inorder(root);
printf("\nThe preorder display : ");
19
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
preorder(root);
printf("\nThe postorder display : ");
postorder(root);
printf("\n");
}
break;
case 3 : printf("\nenter the key to search and delete:\n");
scanf("%d",&key);
search_node(root,key);
break;
case 4 : level_order(root); break;
case 5 : return 0;
}
}
return 0;
}
CONSTRUCTION OF AN EXPRESSION TREE
The evaluation of the tree takes place by reading the postfix expression one
symbol at a time. If the symbol is an operand, one-node tree is created and a
pointer is pushed onto a stack. If the symbol is an operator, the pointers are
popped to two trees T2 and T1 from the stack and a new tree whose root is the
operator and whose left and right children point to T1 and T2 respectively is
formed . A pointer to this new tree is then pushed to the Stack.
20
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
Example
The input is: a b + c d e + * *. Since the first two symbols are operands,
one-node trees are created and pointers are pushed to them onto a stack. For
convenience the stack will grow from left.
The next symbol is a '+'. It pops the two pointers to the trees, a new tree is
formed, and a pointer to it is pushed onto to the stack.
Next, c, d, and e are read. A one-node tree is created for each and a pointer to the
corresponding tree is pushed onto the stack.
21
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
Continuing, a '+' is read, and it merges the last two trees.
\
Now, a '*' is read. The last two tree pointers are popped and a new tree is formed with
a '*' as the root.
Finally, the last symbol is read. The two trees are merged and a pointer to the final
tree remains on the stack.[
22
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
15. Algebraic expressions
Binary algebraic expression tree equivalent to ((5 + z) / -8) * (4 ^ 2)
Algebraic expression trees represent expressions that contain numbers,
variables, and unary and binary operators. Some of the common operators are
× (multiplication), ÷ (division), + (addition), − (subtraction), ^ (exponentiation),
and - (negation). The operators are contained in the internal nodes of the tree,
with the numbers and variables in the leaf nodes.The nodes of binary operators
have two child nodes, and the unary operators have one child node.
Binary algebraic expression tree equivalent to ((5 + z) / -8) * (4 ^ 2)
23
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
Boolean expressions
Boolean expressions are represented very similarly to algebraic expressions,
the only difference being the specific values and operators used. Boolean
expressions use true and false as constant values, and the operators include
(AND) & ).
((TorF) and (~F))or(TorF)
Example for Expression Tree;
(a+(b*c))+(((d*e)+f*g)
24
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
16. Write a Program to Create a tree for Postfix Expression and Evaluate the
Expression using Binary Tree.
Also print the expression in infix, postfix, prefix form using inorder, postorder
and preorder respectively.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct root
{
char data;
struct root *left;
struct root *right;
};
typedef struct root NODE;
NODE *stack[30];
int top=-1;
int c=0, leaf=0, InternalNodes=0;
NODE* insert(char b)
{
NODE *temp;
temp=(NODE*)malloc(sizeof(NODE));
temp->data=b;
temp->left=NULL;
temp->right=NULL; c++; // count no of nodes in a tree
return(temp);
}
void inorder(NODE *t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%c",t->data);
inorder(t->right);
}
}
void preorder(NODE *t)
{
if(t!=NULL)
{
printf("%c",t->data);
25
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
preorder(t->left);
inorder(t->right);
}
}
void postorder(NODE *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%c",t->data);
}
}
int calculate(int lval, char data, int rval)
{ switch(data)
{
case '+' : return (lval + rval);
case '-' : return (lval - rval);
case '*' : return (lval * rval);
case '/' : return (lval / rval);
default : printf(" unknown operator"); return -1;
}
}
This function will take single digit numeric character and displays infix, postfix,
prefix and evaluates the expression by taking numeric value.
int evaluate(NODE *root)
{ int lval, rval, res;
if (root->left==NULL && root->right==NULL)
{ return (root->data-'0');
}
else
{ lval=evaluate(root->left);
rval=evaluate(root->right);
res=calculate(lval, root->data, rval);
printf("res=%d\n",res);
return res;
}
}
OR
This function will take single alphabetic character and displays infix, postfix,
prefix and evaluates expression the by taking the input from the keyboard.
26
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
postorder(temp);
res=evaluate(temp);
printf("\nResult of Expression=%d\n",res);
printf("\n Total no nodes =%d\n",c);
return 0;
}
18. Function for counting the number of leaf nodes and
internal nodes
Int leaf=0, InternalNodes=0;
void Leaf_NonLeafNodes(NODE *root)
{ if (root->left==NULL && root->right==NULL)
{ leaf++; //increment leaf nodes
}
else
{ InternalNodes++; // Increment internal nodes count
Leaf_NonLeafNodes(root->left);
Leaf_NonLeafNodes(root->right);
}
return ;
}
28
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
Multiple Choice
14
/ \
2 11
/ \ / \
1 3 10 30
/ /
7 40
1. There is a tree in the box at the top of this section. How many leaves does it
have?
o A. 2 B. 4 C. 6 D. 8 E. 9
2. There is a tree in the box at the top of this section. How many of the nodes have
at least one sibling?
o A. 5 B. 6 C. 7 D. 8 E. 9
3. There is a tree in the box at the top of this section. What is the value stored in
the parent node of the node containing 30?
o A. 10 B. 11 C. 14 D. 40
o E. None of the above
4. There is a tree in the box at the top of this section. How many descendants does
the root have?
o A. 0 B. 2 C. 4 D. 8
29
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
5. There is a tree in the box at the top of this section. What is the depth of the
tree?
o A. 2 B. 3 C. 4 D. 8 E. 9
6. There is a tree in the box at the top of this section. How many children does the
root have?
o A. 2 B. 4 C. 6 D. 8 E. 9
7. Consider the binary tree in the box at the top of this section. Which statement
is correct?
o A. The tree is neither complete nor full.
o B. The tree is complete but not full.
o C. The tree is full but not complete.
o D. The tree is both full and complete.
8. What is the minimum number of nodes in a full binary tree with depth 3?
o A. 3 B. 4 C. 8 D. 11 E. 15
9. What is the minimum number of nodes in a complete binary tree with depth 3?
o A. 3 B. 4 C. 8 D. 11 E. 15
10.Select the one true statement.
o A. Every binary tree is either complete or full.
o B. Every complete binary tree is also a full binary tree.
o C. Every full binary tree is also a complete binary tree.
o D. No binary tree is both complete and full.
11.Suppose T is a binary tree with 14 nodes. What is the minimum possible depth
of T?
o A. 0 B. 3 C. 4 D. 5
12.Select the one FALSE statement about binary trees:
o A. Every binary tree has at least one node.
o B. Every non-empty tree has exactly one root node.
o C. Every node has at most two children.
o D. Every non-root node has exactly one parent.
13.Consider the binary tree node. Which expression indicates that t represents an
empty tree?
o A. (t == NULL)
o B. (t->data( ) == 0)
o C. (t->data( ) == NULL)
o D. ((t->left( ) == NULL) && (t->right( ) == NULL))
14.Consider the node of a complete binary tree whose value is stored in data[i] for
an array implementation. If this node has a right child, where will the right
child's value be stored?
o A. data[i+1] B. data[i+2]
o C. data[2*i + 1] D. data[2*i + 2]
15.How many recursive calls usually occur in the implementation of the tree_clear
function for a binary tree?
o A. 0 B. 1 C. 2
16.Suppose that a binary taxonomy tree includes 8 animals. What is the minimum
number of NONLEAF nodes in the tree?
o A. 1 B. 3 C. 5 D. 7 E. 8
30
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.
DSA Module 18CS32- IV- Tree Data Structure
14
/ \
2 11
/ \ / \
1 3 10 30
/ /
7 40
17.There is a tree in the box at the top of this section. What is the order of nodes
visited using a pre-order traversal?
o A. 1 2 3 7 10 11 14 30 40
o B. 1 2 3 14 7 10 11 40 30
o C. 1 3 2 7 10 40 30 11 14
o D. 14 2 1 3 11 10 7 30 40
18.There is a tree in the box at the top of this section. What is the order of nodes
visited using an in-order traversal?
o A. 1 2 3 7 10 11 14 30 40 (B). 1 2 3 14 7 10 11 40 30
o C. 1 3 2 7 10 40 30 11 14 (D). 14 2 1 3 11 10 7 30 40
19.There is a tree in the box at the top of this section. What is the order of nodes
visited using a post-order traversal?
o A. 1 2 3 7 10 11 14 30 40
o B. 1 2 3 14 7 10 11 40 30
o C. 1 3 2 7 10 40 30 11 14
o D. 14 2 1 3 11 10 7 30 40
31
Dr. Ganga Holi, Professor & Head, Dept. of ISE, GLOBAL ACADEMY OF TECHNOLOGY, BENGALURU.