Trees Data Structure
Trees Data Structure
Overview
ancestor of B
Descendent – a node B is said to be descendent of A if B is either child of A or child of
some other descendent of A
Every node except the root has one parent
Sub-tree – Part of tree
Leaf node – node which does not have any child
Example
Terminology(Cont…)
Height of a node is the length of a longest path from this node to a leaf
All leaves are at height zero
Height of a tree is the height of its root (maximum level)
Depth of a node is the length of path from root to this node
Root is at depth zero
Depth of a tree is the depth of its deepest leaf that is equal to the height of this tree
Binary Tree
Tree with no node of out degree greater than two
Out degree of any node can be 0,1 or 2
Any node can have at most two children
Binary Tree (Cont…)
Is this Full Binary tree Is this Full Binary tree Is this Full Binary tree
YES YES NO
Some Important Facts
Let T be a nonempty, full binary tree Then:
Is this complete Binary tree Is this complete Binary tree Is this complete Binary tree
YES NO YES
Complete Binary Tree
Is this complete Binary tree Is this complete Binary tree Is this complete Binary tree
Full but Not Complete Complete but not full Neither Full nor complete
Perfect binary tree :- Every node except the leaf nodes have two children and every level
(last level too) is completely filled.
i.e. a binary tree where each level contains the maximum number of nodes
A A
B
B
C C
D
D
E
E
Height = N – 1
Representation of binary tree
Binary tree can be represented in two ways –
1. Array
2. Linked list
Array Representation –
Three arrays to be maintained.
0 1 2 3 4 5 6
Data A B C D E F G
A
Left Child 1 3 4 -1 -1 -1 -1
B C
Right Child 2 -1 5 6 -1 -1 -1
D E F
G
Note: Parenthetical representation A(B(D( null G) null )C(EF))
Representation of binary tree (Cont…)
Linked List Representation –
struct Node{
struct node *lc;
int data;
struct node *rc;
};
A A
B C B C
D E F D E F
G G
Operations on Tree
1. Traversing
2. Searching
3. Insertion
4. Deletion
Traversal
In order traversal
1. N (node)
2. L (left) 3. R (right)
Preorder – A B C D E F
InOrder (LNR)
Algorithm Inorder (root): Traverse a binary tree in left-node-right sequence.
2. N (node)
1. L (left) 3. R (right)
Inorder – C B D A E F
PostOrder (LRN)
Algorithm Postorder (root): Traverse a binary tree in left-right-node sequence.
3. N (node)
1. L (left) 2. R (right)
Postorder – C D B F E A
Expression Tree
A x
B C
Prefix traversal
Algorithm Prefix(tree): Print prefix expression for a given expression tree
+ D
A x
B C
Postfix traversal
Algorithm Postfix(tree): Print postfix expression for a given expression tree
+ D
A x
B C
Infix traversal
Algorithm Infix(tree): Print infix expression for a given expression tree
+ D
A x
B C
Exercises
PostFix Traversal –
1. ABC*+DE/-
2. AB*CD+-E+
3. ABC*DEF↑/G*-H*+
PreFix Praversal–
1. -+A*BC/DE
2. +-*AB+CDE
3. +A*-*BC*/D↑EFGH
Problem
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)
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)
Binary Search Tree (BST)
45 G
33 54 J
E
22 47 56 B K
H
25 A D
BST Examples
6 9
4 6 4 9 7 7
7 9 4 6
E e r i space
y s n a r l k .
Building a Tree
Scan the original text
E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
Building a Tree
y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8
E i
1 1
Building a Tree
y l k . r s n a sp e
1 1 1 1 2 2 2 2 2 4 8
E i
1 1
Building a Tree
k . r s n a sp e
1 1 2 2 2 2 2 4 8
E i
1 1
2
y l
1 1
Building a Tree
2
k . r s n a 2 sp e
1 1 2 2 2 2 4 8
y l
E i 1 1
1 1
Building a Tree
r s n a 2 2 sp e
2 2 2 2 4 8
y l
E i
1 1
1 1
k .
1 1
Building a Tree
r s n a 2 2 sp e
2
2 2 2 2 4 8
E i y l k .
1 1 1 1 1 1
Building a Tree
n a 2 sp e
2 2
2 2 4 8
E i y l k .
1 1 1 1 1 1
r s
2 2
Building a Tree
n a 2 sp e
2 4
2
2 2 4 8
E i y l k . r s
1 1 1 1 1 1 2 2
Building a Tree
2 4 e
2 2 sp
8
4
y l k . r s
E i
1 1 1 1 2 2
1 1
n a
2 2
Building a Tree
2 4 4 e
2 2 sp
8
4
y l k . r s n a
E i
1 1 1 1 2 2 2 2
1 1
Building a Tree
4 4 e
2 sp
8
4
k . r s n a
1 1 2 2 2 2
2 2
E i y l
1 1 1 1
Building a Tree
4 4 4
2 sp e
4 2 2 8
k . r s n a
1 1 2 2 2 2 E i y l
1 1 1 1
Building a Tree
4 4 4
e
2 2 8
r s n a
2 2 2 2 E i y l
1 1 1 1
2 sp
4
k .
1 1
Building a Tree
4 4 4 6 e
2 sp 8
r s n a 2 2
k . 4
2 2 2 2
E i y l 1 1
1 1 1 1
Building a Tree
4 6 e
2 2 2 8
sp
k . 4
E i y l
1 1 8
1 1 1 1
4 4
r s n a
2 2 2 2
Building a Tree
4 6 e 8
2 2 2 8
sp
4 4
k . 4
E i y l
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree
8
e
8
4 4
10
r s n a
4
2 2 2 2 6
2 2
2 sp
E i y l k . 4
1 1 1 1 1 1
Building a Tree
8 10
e
8
4 4 4
6
2 2 2
r s n a sp
2 2 2 2 4
E i y l k .
1 1 1 1 1 1
Building a Tree
10
16
4
6
2 2 e 8
2 sp
8
E i y l k . 4 4
4
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
10 16
4
6
e 8
2 2
2 sp 8
4 4
E i y l k . 4
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree
26
16
10
4 e 8
6
8
2 2 2 sp 4 4
E i y l k . 4
r s n a
1 1 1 1 1 1
2 2 2 2
Building a Tree
After enqueueing this node
there is only one node left in
priority queue.
26
16
10
4 e 8
6
8
2 2
2 sp 4 4
E i y l k . 4
r s n a
1 1 1 1 1 1
2 2 2 2
Building a Tree
Dequeue the single node left in the
queue.
26
This tree contains the new code-words
16
for each character. 10
4 e 8
Frequency of root node should equal 6
8
number of characters in text. 2 2 2 sp 4 4
E i y l k . 4
r s n a
1 1 1 1 1 1
2 2 2 2
Char Code
E 0000
i 0001
y 0010 26
l 0011
k 0100 10
16
. 0101
space 011 4
6
e 8
e 10 2 2 8
r 1100 2 sp 4 4
s 1101 E i y l k . 4
r s n a
n 1110 1 1 1 1 1 1
a 1111 2 2 2 2
Encoding the File
Results
• Have we made things 000010110000011001110001010110110100
any better? 111110101111110001100111111010010010
1
• 73 bits to encode the
text
• ASCII would take 8 *
26 = 208 bits
Decoding the File
• How does receiver know what the codes are?
• Tree constructed for each text file are then saved as a
header in the compressed file
– Doesn’t add a significant amount of size to the file for
large files (which are the ones you want to compress
anyway)
•
Decoding the File
• Once receiver has tree it
scans incoming bit stream 26
• 0 go left 10
16
• 1 go right 4 e 8
6
• Print character 2 2 8
2 sp 4 4
E i y l k . 4
r s n a
1 1 1 1 1 1
2 2 2 2
010011000001110111101111011110100011100
krisna sir
Important Facts:
1. It is an example of a greedy algorithm: It's called greedy because the two smallest nodes
are chosen at each step, and this local decision results in a globally optimal encoding
tree.
2. Huffman tree is an optimal tree: there are no other trees with the same characters that
use fewer bits to encode the same string.
3. Huffman codes are prefix free codes (prefix codes) i.e the codes (bit sequences) are
assigned in such a way that the code assigned to one character is not prefix of code
assigned to any other character. This is how Huffman Coding makes sure that there is
no ambiguity when decoding the generated bit stream.
4. Huffman coding is used to assign variable size code for symbol. In huffman coding, less
size code is assigned to frequently occurring symbols and greater size code is assign to
rarely occurring symbols.
Huffman Tree
Let following information is given –
Letter A B C D E F G
Frequency 16 11 7 20 25 5 16
100
43 57
20 23 25 32
11 12
16 16
5 7
Application of Huffman Tree
Answer: (B)
Problem
1. A networking company uses a compression technique to encode the message before
transmitting over the network. Suppose the message contains the following characters
with their frequency:
character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
If the compression technique used is Huffman Coding, how many bits will be saved in the
message?
(A) 224 (B) 800 (C) 576 (D) 324
Answer: (C) Total number of characters in the message = 100. Each character takes 1 byte.
So total number of bits needed = 800.
After Huffman Coding, the characters can be represented with: f: 0, c: 100, d: 101,
a: 1100, b: 1101, e: 111
Total number of bits needed = 224
Hence, number of bits saved = 800 - 224 = 576
Problem
1. 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
Which one of the following sequences of keys can be the result of an in-order
traversal of the tree T?
(A) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
(B) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
(C) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
(D) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
2. Construct a binary tree for the following preorder and inorder traversals:
Inorder sequence: DIBHJEAFLKCGM
Preorder sequence: ABDIEHJCFKLGM
Problem
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
(A) 2^(i-1)
(B) 2^i
(C) 2^(i+1)
(D) 2^[(i+1)/2]
Answer: (A)
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) 2*(h+1)
Answer: (C)
Problem
In a binary tree with n nodes, every node has an odd number of descendants.
Every node is considered to be its own descendant. What is the number of nodes
in the tree that have exactly one child?
(A) 0
(B) 1
(C) (n − 1) /2
(D) n-1
Answer: (A)
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
(A) 63 and 6, respectively
(B) 64 and 5, respectively
(C) 32 and 6, respectively
(D) Can not determine
Answer: (A)
Problem
1. In a complete k-ary tree, every internal node has exactly k children or no child. The
number of leaves in such a tree with I internal nodes is:
(A) I*k (B) (I – 1) k+ 1 (C) I*( k – 1) + 1 (D) I*(k – 1)
Answer: (C)
2. The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3
children is:
(A) n/2 (B) (n-1)/3 (C) (n-1)/2 (D) (2n+1)/3
Answer: (D)
L = (3-1)I + 1 = 2I + 1Total number of nodes(n) is sum of leaf nodes and internal
nodes
n = L + I After solving above two, we get L = (2n+1)/3
Problem
[Link] of the following is/are correct inorder traversal sequence(s) of binary search
tree(s)?
1. 3, 5, 7, 8, 15, 19, 25 2. 5, 8, 9, 12, 10, 15, 25
3. 2, 7, 10, 8, 14, 16, 20 4. 4, 6, 7, 9, 18, 20, 25
(A) 1 and 4 only (B) 2 and 3 only (C) 2 and 4 only (D) 2 only
Answer: (A)
In-order traversal of a BST gives elements in increasing order. So answer A is correct.
2. While inserting the elements 71,65,84,69,67,83 in an empty binary search tree (BST) in
the sequence shown, the element in the lowest level is
(A)65 (B) 67 (C) 69 (D) 83
3. Level of a node is distance from root to that node. For example, level of root is 0 and
levels of left and right children of root is 1. The maximum number of nodes on level i of a
binary tree (the operator '^' indicates power).
(A) 2^(i-1) (B) 2^i (C) 2^(i+1) (D) 2^[(i+1)/2]
Problem
1. There are 8, 15, 13, 14 nodes were there in 4 different trees. Which of them could
have formed a full binary tree?
A. 15 B. 8 C.14 d. NONE
Answer:
The number of the node in the left sub-tree and right sub-tree of the root, respectively, is
a) (4, 7)
b) (7, 4)
c) (8, 3)
d) (3, 8)