DSA Unit - II Trees
DSA Unit - II Trees
Trees
By
Ms. Ankita P. Shinde
Here, binary name itself suggests two numbers, i.e., 0 and 1. In a binary tree, each node in
a tree can have utmost two child nodes. Here, utmost means whether the node has 0
nodes, 1 node or 2 nodes.
In above figure, a normal binary tree is converted into full binary tree by adding dummy
nodes (In pink colour).
To represent a binary tree of depth 'n' using array representation, we need one
dimensional array with a maximum size of 2n + 1.
The example of the binary tree represented using Linked list representation is shown as
follows...
In this representation, every node's data field stores the actual value of that node. If that node has
left a child, then left reference field stores the address of that left child node otherwise stores NULL. If
that node has the right sibling, then right reference field stores the address of right sibling node
otherwise stores NULL.
The above example tree can be represented using Left Child - Right Sibling representation as
follows...
5.End If
Since we are not using recursion, we will use the Stack to store the traversal, we need to
remember that preorder traversal is, first traverse the root node then left node followed by the
right node.
Solution:
Preorder Sequence : 50, 17, 12, 9, 14, 23, 19, 72, 54, 67, 76.
Inorder Séquence : 9, 12, 14, 17, 23, 19, 50, 54, 67, 72, 76.
Postorder Sequence : 9, 14, 12, 19, 23, 17, 67, 54, 76, 72, 50.
Yellow cell are cells which are tested in search algorithm before needed node found.
Yellow cell are cells which are tested in search algorithm before needed node found. Take
into account that for some cases DFS require less nodes for processing. For some cases
it requires more.
Now min heap contains 5 nodes where 4 nodes are roots of trees with single element each,
and one heap node is root of tree with 3 elements
character Frequency
c 12
d 13
Internal Node 14
e 16
f 45
Now min heap contains 4 nodes where 2 nodes are roots of trees with single element each,
and two heap nodes are root of tree with more than one nodes
character Frequency
Internal Node 14
e 16
Internal Node 25
f 45
Every binary search tree is a binary tree but every binary tree need not to be binary
search tree.
For Example:
The predecessor of 6 is 4
The successor of 6 is 7
The predecessor of 10 is 8
The successor of 10 is 13
Answer: a
Explanation: The number of edges from the node to the deepest leaf is called height of
the tree.
Answer: a
Explanation: A full binary tree is a tree in which each node has exactly 0 or 2 children.
Answer: d
Explanation: Relation between number of internal nodes(I) and nodes(N) is N =
2*I+1.
Answer: d
Explanation: Here,
Postorder Traversal: N, P, Q, O, M
Inorder Traversal: N, M, P, O, Q
Root node of tree is the last visiting node in Postorder traversal. Thus, Root Node = ‘M’.
The partial tree constructed is:
Answer: d
Explanation: In order sequence of binary search trees will always give ascending order
of elements. Remaining all are true regarding binary search trees.
Answer: c
Explanation: Preorder Traversal is 10, 4, 3, 5, 11, 12. Inorder Traversal of Binary search
tree is equal to ascending order of the nodes of the Tree. Inorder Traversal is 3, 4, 5, 10,
11, 12. The tree constructed using Preorder and Inorder traversal is
Answer: d
Explanation: This type of tree traversal will not use stack or queue.
a) 6, 2, 5, 7, 11, 2, 5, 9, 4
b) 6, 5, 2, 11, 7, 4, 9, 5, 2
c) 2, 7, 2, 6, 5, 11, 5, 9, 4
d) 2, 7, 6, 5, 11, 2, 9, 5, 4
Answer: a
Explanation: In-order traversal follows LNR(Left-Node-Right).
Answer: d
Explanation: The binary tree contains values 7, 8, 13, 26, 35, 40,
70, 75. The given pre-order sequence is 35, 13, 7, 8, 26, 70, 40
and 75. So, the binary search tree formed is
Thus post-order sequence for the tree is 8, 7, 26, 13, 40, 75, 70 and 35.