Final Tree
Final Tree
Prepared By,
Dr. Nikita Bhatt
Tree: Data Structure
It is non-linear data structure.
A tree represents hierarchy organization structure of corporation.
Unlike Arrays, Linked Lists, Stack and queues, which are linear data
structures, trees are hierarchical data structures.
2
Tree: Data Structure(Continue)
UNIX / WINDOWS FILE SYSTEM
3
Tree Terminology
A is the root node ( Does not have any parent)
B is parent of D & E.
A is ancestor of D & E.
D & E are descendants of A.
C is sibling of B.
Sibling : if n1 and n2 are left and right child of n, than n1 and n2 are said to
be sibling.(nodes with same parent node)
D & E are children of B.
D,E,F,G,I are leaves.
5
Tree Terminology(continue)
• Level/Depth Number of generations from the root, root node at level 0
• The level of a node is defined recursively defined.
• If node n is the root of tree T, its level is 0
• If node n is not the root of tree T, its level is 1 + the level of its parent
6
Tree Terminology(continue)
Height of tree –The height of a tree is the maximum number of levels of the
tree (starting from root).
Height of node –The height of a node is the maximum number of levels of the
tree (starting from the root to that node).
A
The height of this tree
B C is 2
7
Tree Terminology(continue)
level 0
level 1
level 2
level 3
8
Binary Tree
An ordered tree is one in each node are ordered.
Binary tree: ordered tree with all nodes having at most 2 children.
9
Binary Tree: Application
Arithmetic Expression
10
Binary Tree: Application(Continue)
Decision Tree
11
Binary Tree: Concepts
Maximum nodes of Binary Tree
12
Binary Tree: Concepts
Minimum height of a binary tree
A binary tree of height h has
At most 2𝑖 nodes at level i
At most 1+2+22 +…+2ℎ = 2ℎ+1 -1 nodes
h >= log2 (n+1)/2
13
Binary Tree: Concepts
Maximum height of a binary tree
Binary tree on n nodes has height at most n-1
This is obtained when every node (except the leaf) has exactly one child.
14
Binary Tree : Types
Full Binary Tree / strict binary tree: Full Binary Tree A Binary Tree is full
if every node has 0 or 2 children. Following are examples of full binary tree.
We can also say a full binary tree is a binary tree in which all nodes except
leaves have two children.
Complete Binary Tree: A Binary Tree is complete Binary Tree if all levels
are completely filled except possibly the last level and the last level has all
keys as left as possible
15
Binary Tree : Types (Continue)
Perfect Binary Tree : A Binary tree is Perfect Binary Tree in which all
internal nodes have two children and all leaves are at same level.
16
Binary Tree : Tree Walks/ Traversals
A tree walk is a way of visiting all the nodes exactly once in a specific order.
Preorder Traversal:
Processes each node before processing its children.
(Example: Reading a document from beginning to end) (Root, Left, Right)
Postorder Traversal:
Processes each node after processing its children.
(Example:du command in unix) (Left, Right, Root)
Inorder Traversal:
Processes node between the visit to left and right sub tree.
(Example: Arithmetic Expression) (Left, Root, Right)
17
Binary Tree : Tree Walks/ Traversals (Continue)
Preorder Traversal:
A,B,D,E,H,I,C,F,G
A
Postorder Traversal:
B C
D,H,I,E,B,F,G,C,A
D E F G
H I Inorder Traversal:
D,B,H,E,I,A,F,C,G
18
Binary Tree : Tree Walks/ Traversals (Continue)
Inorder Traversal
struct node Analysis:
{
char data; • Each node is visited 3 times
• Constant amount of time is spent on each node
struct node *left;
• 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 = 𝑶 𝟑 ∗ 𝒏 ∗ 𝑪 ≈ 𝑶(𝒏)
struct node *right; n is the number of nodes
} C specifies the constant amount of time
void inorder(struct node *t) each node is spending
{ if(t) • Space Complexity: Depends on the level of the
{ tree
𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 = 𝑶(𝒉)
𝐢𝐧𝐨𝐫𝐝𝐞𝐫 𝐭 → 𝐥𝐞𝐟𝐭 ;
𝐩𝐫𝐢𝐧𝐭𝐟 "%c" ,𝐭 → 𝐝𝐚𝐭𝐚 ;
𝐢𝐧𝐨𝐫𝐝𝐞𝐫 𝐭 → 𝐫𝐢𝐠𝐡𝐭 ;
}
19
Binary Tree : Tree Walks/ Traversals (Continue)
Preorder Traversal Postorder Traversal
struct node struct node
{ {
char data; char data;
struct node *left; struct node *left;
struct node *right; struct node *right;
} }
void preorder(struct node *t) void postorder(struct node *t)
{ if(t) { if(t)
{ {
𝐩𝐫𝐢𝐧𝐭𝐟 "%c" ,𝐭 → 𝒅𝒂𝒕𝒂 ; post𝐨𝐫𝐝𝐞𝐫 𝐭 → 𝐥𝐞𝐟𝐭 ;
𝐩𝐫𝐞𝐨𝐫𝐝𝐞𝐫 𝐭 → 𝐥𝐞𝐟𝐭 ; 𝐩𝐨𝐬𝐭𝐧𝐨𝐫𝐝𝐞𝐫 𝐭 → 𝐫𝐢𝐠𝐡𝐭 ;
𝐩𝐫𝐞𝐨𝐫𝐝𝐞𝐫 𝐭 → 𝐫𝐢𝐠𝐡𝐭 ; 𝐩𝐫𝐢𝐧𝐭𝐟 "%c" ,𝐭 → 𝒅𝒂𝒕𝒂 ;
}
}
20
Binary Tree : Tree Walks/ Traversals (Continue)
1. Preorder: a b c d f g e
Inorder: c b f d g a e
2 . Postorder: g h d i e b j f c a
Inorder: g d h b e i a f j c
3. Preorder: a b c d f g e
Inorder: c f g d b e a
4. Preorder: f b a d c e i h g k j
Inorder: a b c d e f g h i j k
5. Postorder: b f g d e c a
Inorder: b a f d g c e
21
Gate 1995
A binary tree T has n leaf nodes. The number of
nodes of degree 2 in T is
(a) logn (b) n-1 (c) n (d) 2n
22
Gate 2006
In a binary tree, the number of internal nodes of degree 1 is
5, and the number of internal nodes of degree 2 is 10. The
number of leaf nodes in the binary tree is
23
Gate 2000
The height of a binary tree is the maximum number of
edges in any root to leaf path. The maximum number of
nodes in a binary tree of height h is:
(a) 2^h -1
(b) 2^(h-1) – 1
(c) 2^(h+1) -1
(d) (d) 2*(h+1)
Ans: (C)
24
Self Assessment
Which of the following is a true about Binary Trees
(A) Every binary tree is either complete or full.
(B) Every complete binary tree is also a full binary tree.
(C) Every full binary tree is also a complete binary tree.
(D) No binary tree is both complete and full.
(E) None of the above
Answer : (E)
25
Self Assessment
Level of a node is distance from root to that node. For
example, level of root is 1 and levels of left and right
children of root is 2. The maximum number of nodes on
level i of a binary tree is (In the following answers, the
operator ‘^’ indicates power)
(A) 2^(i)-1
(B) 2^i
(C) 2^(i+1)
(D) 2^[(i+1)/2]
Answer : (A)
26
Self Assessment
Find Output of below function
27
Self Assessment
12. Find Output of below function
struct node
{
char data;
struct node *left;
struct node *right;
}
void order(struct node *t)
{ if(t)
{
𝐩𝐫𝐢𝐧𝐭𝐟 "%c" ,𝐭 → 𝒅𝒂𝒕𝒂 ;
𝐨𝐫𝐝𝐞𝐫 𝐭 → 𝐥𝐞𝐟𝐭 ;
𝐩𝐫𝐢𝐧𝐭𝐟 "%c" ,𝐭 → 𝒅𝒂𝒕𝒂 ;
𝐨𝐫𝐝𝐞𝐫 𝐭 → 𝒓𝒊𝒈𝒉𝒕 ;
𝐩𝐫𝐢𝐧𝐭𝐟 "%c" ,𝐭 → 𝒅𝒂𝒕𝒂 ;
}
}
28
Self Assessment
29
Number of binary trees
2nCn
Number of unlabeled binary trees with n nodes = (n+1)
2nCn
Number of labeled binary trees with n nodes = (n+1)
× n!
Binary trees with 3 nodes a,b and c which has preorder traversal abc. How
many binary trees are possible?
Binary trees with 3 nodes a,b and c which has preorder traversal abc and
postorder traversal cba. How many binary trees are possible?
Binary trees with 3 nodes a,b and c which has preorder traversal abc and
postorder traversal cba and inorder traversal is bca. How many binary trees
are possible?
30
Note
Given Pre-order, post-order and in-order- we have only one tree possible
which satisfies all the conditions
2nCn
With n node, number of preorder binary tree possible =
(n+1)
With given pre-order and post-order, multiple trees are possible
31
Height of a binary tree
𝐇 𝐭 = 𝟎 ; 𝐭 𝐢𝐬 𝐞𝐦𝐩𝐭𝐲
= 𝟎 ; 𝐭 𝐢𝐬 𝐚 𝐥𝐞𝐚𝐟
𝟏 + 𝐦𝐚𝐱 𝐇 𝐋𝐒𝐓 , 𝐇 𝐑𝐒𝐓 ; 𝐨𝐭𝐡𝐞𝐫𝐰𝐢𝐬𝐞
32
Binary Tree and Binary Search Tree
Binary tree property
• each node has 2 children
Search tree property
• all keys in left subtree smaller than root’s key
• all keys in right subtree larger than root’s key
• result:
• easy to find any given key
• Insert/delete by changing links
33
Example and Counter-Example
8
5
5 11
4 8
2 7 6 10 18
1 7 11
4 15 20
3
NOT A 21
BINARY SEARCH TREE BINARY SEARCH TREE
34
Searching a BST
To
… find an element with key k in a tree T
compare
… k with key[root[T]]
if k < key[root[T]], search for k in left[root[T]]
…
otherwise, search for k in right[root[T]]
…
6
Pseudocode for BST Search
…Recursive version
Search(T,k)
01 x ← T
02 if x = NIL then return NIL
03 if k = info[x] then return info[x]
04 if k < info[x]
05 then return Search(LPTR[x],k)
06 else return Search(RPTR[x],k)
…Iterative version
Search(T,k)
01 x ← T
02 while x ≠ NIL and k ≠ info[x] do
03 if k < info[x]
04 then x ← LPTR[x]
05 else x ← RPTR[x]
06 return
info[x] 9
Self Assessment
Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are
inserted in that order into an initially empty binary
search tree. Traverse the tree in Preorder, Inorder and
Postorder.
10
Gate 1996
A binary search tree is generated by inserting in order the following
integers:
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24 The number of nodes in the left
subtree and right subtree of the root respectively is
Answer: (B)
39
Gate 2018
Consider the following statements
(I) Let T be a binary search tree with 4 height. The minimum and
maximum possible nodes of T are 5 and 15 respectively.
(II)In a binary tree, the number of internal nodes of degree 2 is 6,
and the number of internal nodes of degree 1 is 8. The number
of leaf nodes in the binary tree is 15.
Which of the following statement(s) is/are correct?
(A) Only (I) (B) Only (II)
(C) Both (I) and (II) (D) Neither (I) nor (II)
Answer: (D)
Number of Leaf Nodes
= Number of Internal nodes with 2 children + 1 = 6 + 1 = 7
40
Gate 2005
A binary search tree contains the numbers 1, 2, 3, 4, 5, 6, 7, 8.
When the tree is traversed in pre-order and the values in each
node printed out, the sequence of values obtained is 5, 3, 1, 2,
4, 6, 8, 7. If the tree is traversed in post-order, the sequence
obtained would be
(A) 8, 7, 6, 5, 4, 3, 2, 1
(B) 1, 2, 3, 4, 8, 7, 6, 5
(C) 2, 1, 4, 3, 6, 7, 8, 5
(D) 2, 1, 4, 3, 7, 8, 6, 5
Answer: (D)
41
Self Assessment
Postorder traversal of a given binary search tree T produces the
following sequence of keys 10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40,
29.
42
Gate 2006
Suppose that we have numbers between 1 and 100 in a binary
search tree and want to search for the number 55. Which of the
following sequences CANNOT be the sequence of nodes
examined?
(A) {10, 75, 64, 43, 60, 57, 55}
(B) {90, 12, 68, 34, 62, 45, 55}
(C) {9, 85, 47, 68, 43, 57, 55}
(D) {79, 14, 72, 56, 16, 53, 55}
Answer: (C)
43
BST Minimum (Maximum)
Find
… the minimum key in a tree rooted at T
TreeMinimum(T)
01 while LPTR[T] ≠ NIL
02 do T ← LPTR[T]
03 return info(T)
Running
… time O(h), i.e., it is proportional to
the height of the tree
11
BST Insertion
The
… basic idea is similar to searching
take
… an element z (whose left and right
children are NIL) and insert it into T
find
… place in T where z belongs (as if
searching for z),
and add z there
…
The
… running on a tree of height h is O(h),
i.e., it is proportional to the height of the
tree
15
BST Insertion Example
Insert
… 8
16
Deletion
□ Delete node x from a tree T
□ We can distinguish three cases
□ x has no children
□ x has one child
□ x has two children
49
49
Deletion Case 1
□ If x has no children – just remove x
50
50
Deletion Case 2
□ If x has exactly one child, then to delete x, simply make
p[x] point to that child
51
51
Deletion Case 3
□ If x has two children, then to delete
it we have to
□ find its successor (or
predecessor) y
□ remove y (note that y has at most
one child – why?)
□ replace x with y
□ Inorder Successor: smallest element
in right sub tree and Inorder
Predecessor is the largest element in
left subtree 52
52
Self Assessment
Construct the Binary Search Tree using following data.
Show each steps.
32, 45, 12, 11, 13, 92, 78, 66, 17, 70, 98, 108.
Delete 92 and then 32 from the tree. Draw tree after
each deletion.
53
Self Assessment
Construct Binary Search Tree for following data.
45, 36, 76, 23, 89, 115, 98, 39, 41, 56, 69, 48
Also perform delete 23 operation on binary search tree.
54
Self Assessment
Consider a node X in a Binary Tree. Given that X has two children,
let Y be Inorder successor of X. Which of the following is true about
Y?
Answer: (B)
55
Gate 2015
The height of a tree is the length of the longest root-to-leaf
path in it. The maximum and minimum number of nodes in a
binary tree of height 5 are
Answer: (A)
56
Gate 2015
A binary tree T has 20 leaves. The number of nodes in T
having two children is
(A) 18
(B) 19
(C) 17
(D) Any number between 10 and 20
Answer: (B)
57
Gate 1998
Draw the binary tree with node labels a, b, c, d, e,
f and g for which the inorder and postorder
traversals result in the following sequences:
Inorder afbcdge
Postorder a f c g e d b
58
ISRO 1997
A strictly binary tree with 10 leaves
(A) cannot have more than 19 nodes
(B) has exactly 19 nodes
(C) has exactly 17 nodes
(D) has exactly 20 nodes
Answer: (B)
Explanation : Strictly binary tree is also called a full binary tree
which means each node has either 0 or 2 children. (10 leaves + 9
internal node = 19 total nodes)
59
Gate 2017
Find the inorder and postorder of the binary tree with the given preorder:
60, 40, 20, 10, 30, 33, 50, 44, 51, 90, 70, 65, 80, 110, 100, 95, 99, 120.
(A) In order: 110, 100, 99, 90, 80, 70, 65, 60, 51, 50, 44, 40, 33, 30, 20, 10.
Postorder: 110, 120, 100, 95, 99, 70, 80, 65, 60, 40, 50, 51, 44, 20, 30, 33, 10
(B) Inorder: 10, 20, 30, 33, 40, 44, 50, 51, 60, 65, 70, 80, 90, 95, 99, 100, 110
Postorder: 10, 33, 30, 20, 44, 51, 50, 40, 60, 65, 80, 70, 99, 95, 100, 120, 110,
(C) In order: 10, 33, 30, 20, 44, 51, 50, 40, 60, 65, 80, 70, 99, 95, 100, 120, 110,
Postorder: 10, 20, 30, 33, 40, 44, 50, 51, 60, 65, 70, 80, 90, 95, 99, 100, 110
(D) In order: 10, 33, 30, 20, 44, 51, 60, 65, 80, 70, 99, 95, 100, 120, 110,
Postorder: 110, 100, 99, 90, 80, 70, 65, 60, 51, 50, 44, 40, 33, 30, 20, 10.
Answer: (B)
60
UGC-NET | UGC NET CS 2016 July – III
Answer: (C)
61
Gate 2008
A Binary Search Tree (BST) stores values in the range 37 to 573. Consider the following sequence of keys.
Answer: (D)
62
Gate 2008
The following three are known to be the preorder, inorder and postorder sequences of a
binary tree. But it is not known which is which.
MBCAFHPYK
KAMCBYPFH
MABCKYFPH
Pick the true statement from the following.
(A) I and II are preorder and inorder sequences, respectively
(B) I and III are preorder and postorder sequences, respectively
(C) II is the inorder sequence, but nothing more can be said about the other two sequences
(D) II and III are the preorder and inorder sequences, respectively
Answer: (D)
63
Self Assessment
What is the worst case time complexity for search, insert and
delete operations in a general Binary Search Tree?
(A) O(n) for all
(B) O(Logn) for all
(C) O(Logn) for search and insert, and O(n) for delete
(D) O(Logn) for search, and O(n) for insert and delete
Answer: (A)
64
Self Assessment
In delete operation of BST, we need inorder successor (or predecessor) of a node when the
node to be deleted has both left and right child as non-empty. Which of the following is true
about inorder successor needed in delete operation?
(A) Inorder Successor is always a leaf node
(B) Inorder successor is always either a leaf node or a node with
empty left child
(C) Inorder successor may be an ancestor of the node
(D) Inorder successor is always either a leaf node or a node with
empty right child
Answer: (B)
65
ISRO | ISRO CS 2013
Which of the following number of nodes can form a full binary tree?
(A) 8
(B) 15
(C) 14
(D) 13
Answer: (B)
66
Gate 2005
How many distinct binary search trees can be created out of 4 distinct
keys?
(a) 5 (b) 14 (c) 24 (d) 42
Answer (b)
67
Gate 2011
We are given a set of n distinct elements and an
unlabeled binary tree with n nodes. In how many ways
can we populate the tree with the given set so that it
becomes a binary search tree?
Answer: (B)
68
AVL Tree
AVL Tree= Binary Search Tree + Height Balance Property
• Need: For BST, insertion, deletion, find successor, find predecessor
takes proportional to O(h) time which is very bad.
For each AVL tree node, the difference between the heights of its
left and right sub trees is either -1, 0 or +1
• this is called the balance factor of a node
Balance Factor= height(left subtree) –
height(right subtree)
• if balanceFactor > 1 or < -1 then the tree is unbalanced, or not
AVL and needs 'rearranging' to make it more balanced
• Height of AVL tree is : O(logn)
69
AVL Tree: Insertion
4 possible situations:
1. Insert into the left sub-tree of the left child(LL)
2. Insert into the right sub-tree of the right child(RR)
3. Insert into the left sub-tree of the right child(LR)
4. Insert into the right sub-tree of the left child(RL)
70
Self Assessment
1. Construct AVL tree for following data:
(a) 3,2,1,4,5,6,7,16,15
(b) 9,19,27,18,108,99,81
(c) 21, 26,30,9,4,14,28,18,15,10,2,3,7
71
Self Assessment (continue)
2. Insert 45, 12, 25 and 39 into the following AVL
Tree. Draw balanced tree after each insertion with
balance factor.
50
32 77
23 40 60 89
19
72
Maximum nodes in AVL Tree
𝐌𝐚𝐱 𝐧𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐧𝐨𝐝𝐞𝐬 𝐢𝐧 𝐛𝐢𝐧𝐚𝐫𝐲 𝐭𝐫𝐞𝐞 𝐚𝐭 𝐡𝐞𝐢𝐠𝐡𝐭 𝐡
= 𝐦𝐚𝐱 𝐧𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐧𝐨𝐝𝐞𝐬 𝐢𝐧 𝐀𝐕𝐋 𝐭𝐫𝐞𝐞 𝐚𝐭 𝐡𝐞𝐢𝐠𝐡𝐭
73
Minimum number of nodes in AVL tree
Minimum number of node at height h = N h = 1, ℎ = 0
= 2, ℎ = 1
= N h− 1 + 1 + N h − 2 ,h > 1
𝑁 2 =𝑁 1 +1+𝑁 0 =2+1+1=4
𝑁 3 =𝑁 2 +1+𝑁 1 =4+1+2=7
74
74
Self Assessment (continue)
3. Create AVL Tree for the given numbers. Show the tree after each
operation. 7, 1, 6, 2, 5, 4, 3. Apply Delete (1) and then Insert (9).
5. What is the maximum height of any AVL-tree with 7 nodes? Assume that
the height of a tree with a single node is 0.
75
Self Assessment (continue)
7. In the balanced binary tree in the figure given below, how many nodes
will become unbalanced when a node is inserted as a child of the node “g”?
76
Gate 2008
Which of the following is TRUE?
(A) The cost of searching an AVL tree is θ (log n) but that of a binary search
tree is O(n)
(B) The cost of searching an AVL tree is θ (log n) but that of a complete binary
tree is θ (n log n)
(C) The cost of searching a binary search tree is O (log n ) but that of an AVL
tree is θ(n)
(D) The cost of searching an AVL tree is θ (n log n) but that of a binary search
tree is O(n)
Answer: (A)
Explanation: AVL tree is a balanced tree.
AVL tree’s time complexity of searching (in all cases) = θ(logn)
But a binary search tree, may be skewed tree, so in worst case BST searching
time = O(n) 77
Gate 1998
Which of the following statements is false?
(a) A tree with n nodes has (n – 1) edges
(b) A labeled rooted binary tree can be uniquely constructed given its
postorder and preorder traversal results.
(c) A complete binary tree with n internal nodes has (n + 1) leaves.
(d) The maximum number of nodes in a binary tree of height h is
(2h+1-1)
Answer : (b)
78
Gate 2005
In a binary tree, for every node the difference between the
number of nodes in the left and right subtrees is at most 2. If
the height of the tree is h > 0, then the minimum number of
nodes in the tree is:
79
Gate 1997
A size-balanced binary tree is a binary tree in which for
every node, the difference between the number of nodes in
the left and right subtree is at most 1. The distance of a node
from the root is the length of the path from the root to the
node. The height of a binary tree is the maximum distance
of a leaf node from the root.
Answer (B)
81
Gate 2014
Consider the expression tree shown. Each leaf represents a numerical value,
which can either be 0 or 1. Over all possible choices of the values at the leaves,
the maximum possible value of the expression represented by the tree is ___.
Answer: (B)
82
Gate 2000
Consider the following nested representation of binary trees: (X Y
Z) indicates Y and Z are the left and right sub stress, respectively,
of node X. Note that Y and Z may be NULL, or further nested.
Which of the following represents a valid binary tree?
Answer: (C)
83
Gate 2005
The numbers 1, 2, …. n are inserted in a binary search tree in some
order. In the resulting tree, the right subtree of the root contains p
nodes. The first number to be inserted in the tree must be
Answer: (C)
84
Self Assessment
Consider the following code snippet in C. The function print() receives root of a Binary
Search Tree (BST) and a positive integer k as arguments.
What is the output of print(root, 3) where root represent root of the following BST.
15
/ \
10 20
/\ / \
8 12 16 25
(A) 10
(B) 16
(C) 20
(D) 20 10
Answer : (B)
85
Self Assessment (Continue)
// A BST node
struct node {
int data;
struct node *left, *right;
};
int count = 0;
void print(struct node *root, int k)
{
if (root != NULL && count <= k)
{
print(root->right, k);
count++;
if (count == k)
printf("%d ", root->data);
print(root->left, k);
}
}
86
Self Assessment
Which of the following traversals is sufficient to construct BST from given traversals
1) Inorder
2) Preorder
3) Postorder
(A) Any one of the given three traversals is sufficient
(B) Either 2 or 3 is sufficient
(C) 2 and 3
(D) 1 and 3
Answer : (B)
87
Gate 2014
Consider the pseudocode given below. The function DoSomething()
takes as argument a pointer to the root of an arbitrary tree represented
by the leftMostChild-rightSibling representation. Each node of the tree
is of type treeNode.
88
Gate 2014(Continue)
typedef struct treeNode* treeptr;
struct treeNode
{
treeptr leftMostChild, rightSibling;
};
int DoSomething (treeptr tree)
{
int value=0;
if (tree != NULL)
{
if (tree->leftMostChild == NULL)
value = 1;
else
value = DoSomething(tree->leftMostChild);
value = value + DoSomething(tree->rightSibling);
}
return(value);
}
89
Gate 1996
Which of the following sequences denotes the post order traversal sequence of the given
tree?
a
/ \
b e
/\ /
c d f
/
g
(A) f e g c d b a
(B) g c b d a f e
(C) g c d b f e a
(D) f e d g c b a
90
Gate 1997
A binary search tree contains the values 1, 2, 3, 4, 5, 6, 7, 8. The tree is
traversed in pre-order and the values are printed out. Which of the following
sequences is a valid output?
(A) 53124786
(B) 53126487
(C) 53241678
(D) 53124768
Answer: (D)
91
(Binary) Heaps
A binary tree that stores priorities (or priority-element) pairs at nodes
92
Examples of non-Heaps
Heap property violated
11
19 13
18 21 19 17
43 23 26 29 31
93
93
Example of non-heap
Last level not left-filled
11
17 13
18 21 19 17
43 26 29 31
94
94
Implementing Heaps
Parent (i)
return i/2
11
Left (i)
return 2i 17 13
Right (i)
18 21 19 17
return 2i+1
43 23 26
1 2 3 4 5 6 7 8 9 10
A 11 17 13 18 21 19 17 43 23 26
Level: 0 1 2 3
95
95
Heapify Process
96
Running time Analysis
A heap of n nodes has height O(log n).
While inserting we might have to move the element all the
way to the top.
Hence at most O(log n) steps required.
In Heapify, the element might be moved all the way to the
last level.
Hence Heapify also requires O(log n) time.
97
Delete-min in a Heap
10 11
18 21 13 12
43 23 26 29 31 19 17
98
98
Delete-min in a Heap (2)
10 11
16 21 13 12
43 23 26 29 31 19 17
99
99
Self Assessment
Demonstrate, step by step, the operation of Build-Min and
Max Heap on the array
[5, 3, 17, 10, 84, 19, 6, 22, 9]
Demonstrate, step by step, the operation of Build-Min and
Max Heap on the array
[103, 11, 101, 110, 111, 119, 19, 91]
The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by
one in the given order into a Max Heap. Show the resultant
Max Heap.
100
Self Assessment
A Priority-Queue is implemented as a Max-Heap. Initially,
it has 5 elements. The level-order traversal of the heap is
10, 8, 5, 3, 2. Two new elements '1' and '7' are inserted in
the heap in that order. The level-order traversal of the
heap after the insertion of the elements is:
(a) 10, 8, 7, 5, 3, 2, 1 (b) 10, 8, 7, 2, 3, 1, 5
(c) 10, 8, 7, 1, 2, 3, 5 (d) 10, 8, 7, 3, 2, 1, 5
101
Self Assessment
Consider a binary max-heap implemented using an array.
Which one of the following array represents a binary max-
heap?
(a) 25,12,16,13,10,8,14 (b) 25,14,13,16,10,8,12
(c) 25,14,16,13,10,8,12 (d) 25,14,12,13,10,8,16
What is the content of the array after two delete operations
on the correct answer to the previous question?
(a) 14,13,12,10,8 (b) 14,12,13,8,10
(c) 14,13,8,12,10 (d) 14,13,12,8,10
102
Gate CS 2020
Consider the array representation of binary min-
heap containing 1023 elements. The minimum
number of comparisons required to find the
maximum in the heap is _________.
Answer: (511)
103
Gate 2019
Let T be a full binary tree with 8 leaves. (A full
binary' tree has every level full.) Suppose two
leaves a and b of T are chosen uniformly and
independently at random. The expected value of
the distance between a and b in T (i.e., the number
of edges in the unique path between a and b) is
(rounded off to 2 decimal places) _________
Answer: (4.25)
104
Gate 2019
Consider the following statements:
I. The smallest element in a max-heap is always at a leaf node.
II. The second largest element in a max-heap is always a child of the root
node.
III. A max-heap can be constructed from a binary search tree in Θ(n) time.
IV. A binary search tree can be constructed from a max-heap in Θ(n) time.
Which of the above statements is/are TRUE?
(A) II, III and IV
(B) I, II and III
(C) I, III and IV
(D) I, II and IV
Answer: (B)
105
Gate 2018
The postorder traversal of a binary tree is 8, 9, 6, 7,
4, 5, 2, 3, 1. The inorder traversal of the same tree is
8, 6, 9, 4, 7, 2, 5, 1, 3. The height of a tree is the
length of the longest path from the root to any leaf.
The height of the binary tree above is_______
Answer: (4)
106
Gate 2011
A max-heap is a heap where the value of each parent
is greater than or equal to the value of its children.
Which of the following is a max-heap?
107
GATE CS 2012
The height of a tree is defined as the number of edges on the
longest path in the tree. The function shown in the
pseudocode below is invoked as height (root) to compute the
height of a binary tree rooted at the tree pointer root.
108
GATE CS 2012(Continue)
109
GATE CS 2012(Continue)
The appropriate expression for the two boxes B1 and B2 are
(A) B1 : (1 + height(n->right)), B2 : (1 + max(h1,h2))
(B) B1 : (height(n->right)), B2 : (1 + max(h1,h2))
(C) B1 : height(n->right), B2 : max(h1,h2)
(D) B1 : (1 + height(n->right)), B2 : max(h1,h2)
Answer: (A)
110
Heap Sort
Create a heap.
Do delete-min repeatedly till heap
becomes empty.
To do an in place sort, we move
8
deleted element to end of heap.
10 11
12 21 13 17
16 23 43 29 26 19 31
111
111
Running times of heap operations
Insert: O(log n)
Heapify: O(log n)
Find minimum: O(1)
Delete-min: O(log n)
Building a heap: O(n)
Heap Sort: O(nlog n)
112