9-Trees
9-Trees
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
Is this Full Binary tree Is this Full Binary tree Is this Full Binary tree
YES YES NO
Some Important Facts
Note: the number of nodes N, the number of leaves L, and the number of
internal nodes I are related in such a way that if you know any one of them,
you can determine the other two.
A full binary tree of height h can have a maximum of 2h leaf nodes, it can
have a maximum of 2h – 1 internal nodes.
Maximum nodes in a full binary tree =maximum number of leaves +
maximum number of internal nodes =2h+1 – 1
Complete Binary Tree
A complete binary tree is a binary tree in which every level, except possibly the last level, is
completely filled, and all nodes are as far left as possible.
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
Perfect binary tree :- Every node except the leaf nodes have two children and every level
(last level too) is completely filled. i.e. Perfect binary tree is a binary tree where each
level contains the maximum number of nodes
If N is the total nodes in a Perfect binary tree then height (h) = log2(N+1)-1
Therefore, the total number of nodes in a Perfect binary tree of height h= 2h+1 – 1
Skewed tree
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
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
Inorder traversal
Postorder 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
-
Note that when expression tree is traversed using:
Inorder it produces infix expression + D
B C
Preorder traversal of expression tree
Algorithm Preorder(tree): Print prefix expression for a given expression tree
+ D
A x
B C
Post order traversal of expression tree
Algorithm Postorder(tree): Print postfix expression for a given expression tree
+ D
A x
B C
Inorder traversal of expression tree
Algorithm Inorder(tree): Print infix expression for a given expression tree
+ D
A x
B C
Exercises
Draw Expression Tree –
1. A+B*C-D/E
2. A*B-(C+D)+E
3. A + (B * C – (D / E ↑ F) * G) * H
Postorder Traversal –
1. ABC*+DE/-
2. AB*CD+-E+
3. ABC*DEF↑/G*-H*+
Preorder Traversal–
1. -+A*BC/DE
2. +-*AB+CDE
3. +A*-*BC*/D↑EFGH
Binary Search Tree (BST)
33 54 E J
22 47 56 B H K
25 A D
6 9
4 6 4 9 7 7
7 9 4 6
E e r i space
ysnarlk.
Building a Tree
Scan the original text
Eerie eyes seen near lake.
• What is the frequency of each character in the
text?
Building a Tree
Prioritize characters
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
2
1 1 1 1 2 2 2 2 4 8
E i
1 1
Building a Tree
k . r s n a sp e
2
1 1 2 2 2 2 4 8
E i
1 1
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
1 1
E i
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
4
2 2 2 2 k .
E i y l 1 1
1 1 1 1
Building a Tree
4 6 e
2 2 2 8
sp
4
E i y l k .
1 1 1 1 1 1
8
4 4
r s n a
2 2 2 2
Building a Tree
4 6 e 8
2 2 2 8
sp
4 4 4
E i y l k .
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
2 2 2 2 4
6
2 2
2 sp
4
E i y l k .
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
4
E i y l k . 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 8
2 sp
4 4 4
E i y l k .
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
4
E i y l k .
1 1 1 1 1 1 r s n a
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
4
E i y l k .
1 1 1 1 1 1 r s n a
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
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Char Code
E 0000
i 0001
y 0010
l 0011 26
k 0100 16
. 0101 10
space 011 4
e 10 e 8
6 8
r 1100 2 2 2
s 1101 sp 4 4
4
n 1110 E i y l k .
a 1111 1 1 1 1 1 1 r s n a
2 2 2 2
Encoding the File
Results
• Have we made things any
000010110000011001110001010110110100
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 the receiver has Huffman coding
tree, the compressed text can be
decoded as: 26
Read bit stream from left to right and
traverse tree from root to leaf 16
10
For 0 go left
For 1 go right 4 e 8
Print character 6 8
2 2 2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
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 large size code is assigned 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
• Example :Construct a binary tree for the following pre-order and in-order
traversals:
In-order sequence: DIBHJEAFLKCGM
Pre-order sequence: ABDIEHJCFKLGM
AVL Tree
• AVL Tree can be defined as height balanced binary search tree in which
each node is associated with a balance factor which is calculated by
subtracting the height of its right sub-tree from that of its left sub-tree.
• If balance factor of any node is 0, it means that the left sub-tree and right
sub-tree contain equal height.
AVL Tree
• If balance factor of any node is -1, it means that the left sub-tree is one
level lower than the right sub-tree.
Why AVL Tree?
• Most of the BST operations (e.g., search, max, min, insert, delete.. etc)
take O(h) time where h is the height of the BST.
• The cost of these operations may become O(n) for a skewed Binary tree. If
we make sure that the height of the tree remains O(log(n)) after every
insertion and deletion, then we can guarantee an upper bound
of O(log(n)) for all these operations.