0% found this document useful (0 votes)
7 views

9-Trees

The document provides a comprehensive overview of tree data structures, including definitions, terminology, and types such as binary trees, full binary trees, complete binary trees, and binary search trees (BST). It covers key concepts such as traversal methods, insertion, deletion, and searching algorithms specific to BSTs, along with examples and algorithms for each operation. Additionally, it discusses the properties and characteristics of different tree types, emphasizing their applications in data organization and retrieval.

Uploaded by

23f3002692
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

9-Trees

The document provides a comprehensive overview of tree data structures, including definitions, terminology, and types such as binary trees, full binary trees, complete binary trees, and binary search trees (BST). It covers key concepts such as traversal methods, insertion, deletion, and searching algorithms specific to BSTs, along with examples and algorithms for each operation. Additionally, it discusses the properties and characteristics of different tree types, emphasizing their applications in data organization and retrieval.

Uploaded by

23f3002692
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

Overview

 A tree is a nonlinear hierarchical data structure that consists of nodes connected by


edges.
 A tree is a graph which does not contain any cycle, hence there is exactly one path from
the root node to each node in a tree.
Terminology

 Nodes are vertices and Edges are connections between nodes


 Root is top most node or base of tree where from tree starts
 Parent, Child, Siblings (children of same parent), Grand parent, Grand child
 Ancestor – a node A is said to be ancestor of B if A is either parent of B or parent of some
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
 Leaf node – node with zero child
 Sub-tree – Part of tree
Every node except the root has one parent
Terminology(Cont…)

 Degree of a node is number of nodes connected to it


In a directed tree :
In-degree : Number of edges coming toward to particular node
Out-degree : Number of edges going out from particular node
 Internal node – out degree>0
 Size of tree: The number of nodes in a tree.
Height and Depth

 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

Depth of this tree is 3


Binary Tree
 Tree where each and every node has at most two children
 In binary tree the out degree of any node can be 0,1 or 2

Binary tree Not a binary tree


Full Binary Tree
A full binary tree is a binary tree in which every node other than the leaves has two
children. This is also called strictly binary tree or proper binary tree or 2-tree.

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:


(a) If T has I internal nodes, the number of leaves L = I + 1.
(b) If T has I internal nodes, the total number of nodes N = 2I + 1.
(c) If T has a total of N nodes, the number of internal nodes I = (N – 1)/2.
(d) If T has a total of N nodes, the number of leaves L = (N + 1)/2.
(e) If T has L leaves, the total number of nodes N = 2L – 1.
(f) If T has L leaves, the number of internal nodes I = L – 1.

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

YES YES YES


But this binary tree is complete. It is called the empty
It has only one node, the root. tree, and it has no
nodes, not even a root.
More Examples

Is this Full or complete Is this Full or complete


Full but Not Complete Complete but not full

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

Left Skewed Tree Right 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

 Traversal is the process of visiting every node exactly once


 Three recursive techniques for binary tree traversal
 Preorder traversal
1. Visit the root
2. Traverse left subtree
3. Traverse right subtree

 Inorder traversal

1. Traverse left subtree


2. Visit the root
3. Traverse right subtree

 Postorder traversal

1. Traverse left subtree


2. Traverse right subtree
3. Visit the root
PreOrder (NLR)
Algorithm Preorder (root): Traverse a binary tree in node-left-right sequence.

1. If (root is not null)


2. process (root)
3. Preorder (leftSubtree)
4. Preorder (rightSubtree)

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.

1. If (root is not null)


2. Inorder (leftSubtree)
3. process (root)
4. Inorder (rightSubtree)

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.

1. If (root is not null)


2. Postorder (leftSubtree)
3. Postorder (rightSubtree)
4. process (root)

3. N (node)

1. L (left) 2. R (right)

Postorder – C D B F E A
Expression Tree

 An expression tree is a binary tree with the following properties:


1. Leaf nodes represent operands
2. The internal nodes represent operators
3. Sub trees are sub expressions, with the root being an operator

Example – Infix expression A + (B * C) – D Expression tree

-
Note that when expression tree is traversed using:
 Inorder it produces infix expression + D

 Preorder it produces prefix expression


A *
 Postorder it produces postfix expression

B C
Preorder traversal of expression tree
Algorithm Preorder(tree): Print prefix expression for a given expression tree

1. If (root is not NULL)


2. print token
3. Preorder (leftSubtree)
4. Preorder (rightSubtree)
Prefix expression - + A x B C D

+ D

A x

B C
Post order traversal of expression tree
Algorithm Postorder(tree): Print postfix expression for a given expression tree

1. If (root is not NULL)


2. Postorder (leftSubtree)
3. Postorder (rightSubtree)
4. print token
Postfix expression A B C x + D -

+ D

A x

B C
Inorder traversal of expression tree
Algorithm Inorder(tree): Print infix expression for a given expression tree

1. If (root is not NULL)


2. If (token is operand)
3. print token
4. else
5. print left parenthesis Infix expression ((A + (B x C)) – D)
6. Inorder (leftSubtree)
7. print token
8. Inorder (rightSubtree) -
9. print right parenthesis

+ 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)

Binary Search Trees (BST) is a binary tree with following properties –


1. Every element has a unique key.
2. The left sub tree of a node contains only nodes with keys less than the node's key.
3. The right sub tree of a node contains only nodes with keys greater than the node's key.
4. The left and right sub trees are also binary search trees.

 BST are type of binary trees with a special organization of data


 This data organization leads to O(log n) complexity for the operations like searching,
insertion and deletion etc. in certain types of the BST (height balanced trees).
 However, in the worst case it would be O(n).
 For getting all advantages of BST, it should be AVL or height balance tree.
BST Examples
45 G

33 54 E J

22 47 56 B H K

25 A D

A binary search tree Not a binary search tree


BST Examples
Two binary search trees representing the same set:
Searching in BST

• In the below figure, if we are searching for 15, then we are


done.
• If we are searching for a key < 15, then we should search in the
left subtree.
• If we are searching for a key > 15, then we should search in the
right subtree.
Searching in BST
Searching a node in BST
Algorithm Search_BST(ROOT , Key): This algorithm searches Key in a BST . This procedure finds
the location LOC of Key in tree. ROOT is a variable pointing to root of tree. This algorithm also
finds the parent location PAR of Key , Initially LOC =NULL and PAR=NULL.
1. Set LOC =NULL and PAR=NULL
2. If ROOT == NULL // Empty Tree
3. Exit
4. If ROOT->Data == Key // Searched element is found as root node
5. LOC = ROOT // Location of searched element
6. PAR = NULL // location of the parent of the searched element
7. Exit
8. Temp = ROOT //Tree has more than one nodes
10. While (Temp !=NULL)
11. PAR = Temp
12. If Key< (Temp->Data)
13. Temp = Temp ->lc
14. Else Temp = Temp ->rc
15. If Temp!=NULL && Temp->Data == Key
16. LOC = Temp
17. BREAK
18. If LOC == NULL
19. Print Search Unsuccessful.
Insertion in BST
Algorithm Insert_BST(ROOT, Key): This algorithm inserts a new leaf node with value Key in
BST at appropriate location. ROOT is variable pointing to root of tree. This algorithm uses sub
algorithm Search_BST to find the Key location LOC and parent location PAR of Key.
1. Call Search_BST(ROOT, Key)
2. If LOC != NULL // exit if Key is already present
3. Exit
4. Node * New_Node= Allocate memory
5. New_Node ->Data = Key
6. New_Node -> lc = NULL
7. New_Node -> rc = NULL
8. If ROOT == NULL // if Tree is Empty previously
9. ROOT = New_Node
10. Else If (Key<(PAR->Data) )
11. PAR->lc = New_Node // make left child of parent
12. Else
13. PAR->rc = New_Node // make right child of parent
Deletion in BST

• When we delete a node, we need to consider


how we handling the children of the deleted
node.
– This has to be done such that the property of the
BST is maintained.
– Hence, three cases to be consider for deletion
Deletion in BST

Three cases in deletion –Suppose we want to delete a node X from BST,


Case 1: X has no child (i.e. it is a leaf node ), then delete X from tree and make its location
in parent node null
Case 2: X has exactly one child, then X will be deleted by replacing the location of X in its
parent node by the location of its only child

Case 3: X has two children


Case A (case 1 and case 2)
Algorithm CaseA_delete(ROOT, Key): This algorithm deletes a node X with value Key from
tree, if this node is not having two children. This algorithm uses sub algorithm Search_BST to
find the Key location LOC and parent location PAR of Key. CHILD is a pointer variable pointing
to child of node X.

1. Call Search_BST(ROOT, Key)


2. Node *CHILD
3. If LOC == NULL // Node with Key does not exist
4. Exit
5. If LOC ->lc ==NULL
6. CHILD = LOC ->rc
7. Else
8. CHILD = LOC ->lc
9. If PAR == NULL // node to be deleted is the root node
10. ROOT = CHILD
11. Else
12. If LOC == PAR->lc
13. PAR ->lc = CHILD
14. Else
15. PAR ->rc = CHILD
16. Free LOC
Deletion in BST (case 3)
X has two children
– Replace the data value of node X with the minimum element in the
right subtree (i.e. replace with in-order successor of X)
– Delete in-order successor
• Note : in-order successor always has either no child or only right child
because if it has a left child, that left child would be smaller and would
have been chosen. So invoke case 1 or 2.
Case B (Delete Node which is having two children)
Algorithm CaseB_delete(ROOT, Key): This algorithm deletes a node X with Key from tree, if this node is
having two children. This algorithm uses sub algorithm Search_BST to find the Key location LOC and
parent location PAR of Key. Inorder_successor is a pointer variable pointing in order successor of node X.

1. Call Search_BST(ROOT, Key)


2. Node *Temp,* Inorder_successor
3. Node *LOC_Temp=LOC
4. Temp = LOC ->rc
5. While Temp->lc != NULL // find inorder successor
6. Temp = Temp->lc
7. Inorder_successor = Temp
8. Inorder_successor_data= Inorder_successor ->Data;
9. CaseA_delete(ROOT, Inorder_successor_data)
10. LOC_Temp->data= Inorder_successor_data
Deletion in BST (Cont…)
Algorithm Delete_BST(ROOT, Key): This algorithm deletes a node X with Key from BST. ROOT
is variable pointing to root of tree T. This algorithm uses sub algorithm Search_BST to find the
Key location LOC and parent location PAR of Key.

1. Call Search_BST(ROOT, Key)


2. If LOC == NULL
3. Exit
4. If LOC ->lc !=NULL and LOC ->rc !=NULL // If both children exist
5. Call CaseB_delete(ROOT,Key)
6. Else
7. Call CaseA_delete(ROOT,Key)
Inorder traversal of BST

• Inorder traversal of BST always gives sorted order of


the elements

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20


Extended Binary Tree
An extended binary tree is a transformation of any binary tree into a full binary tree.
This transformation consists of replacing every null subtree of the original tree with
“special nodes.”
The nodes from the original tree are then become internal nodes, while the “special
nodes” are external nodes.
Extended Binary Tree

6 9

4 6 4 9 7 7

7 9 4 6

(A) (B) (C)

(A) Total path length of Tree A= 4*2 + 7*3 + 9*3 + 6*1 = 62


(B) Total path length of Tree B= 6*2 + 4*2 + 9*2 + 7*2 = 52
(C) Total path length of Tree C= 7*2 + 4*3 + 6*3 + 9*1 = 53
Huffman Coding Algorithm

• An Application of Binary Tree and Priority Queue.


• Huffman coding is a lossless data compression
algorithm.
• In this algorithm, a variable-length code is assigned to
different characters.
Huffman Coding : The Basic Idea

• Not all the characters occur with the same frequency!


• Yet all characters are allocated the same amount of
space
– 1 char = 1 byte, whether it is a or x
• In Huffman encoding the basic idea is that instead of
storing each character in a file as an 8-bit ASCII value, we
will instead store the more frequently occurring
characters using fewer bits and less frequently occurring
characters using more bits.
Huffman Coding Algorithm

1. Scan text to be compressed and count


occurrence of all characters.
2. Sort characters based on number of occurrences in
text.
3. Build Huffman code tree based on sorted
list using the priority queue.
4. Perform a traversal of tree to determine all code words.
5. Scan text again and create new file using the
Huffman codes.
Building a Tree
Scan the original text

• Consider the following text:

Eerie eyes seen near lake.

• Count up the occurrences of all characters in the


text
Building a Tree
Scan the original text

Eerie eyes seen near lake.


• What characters are present?

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

• Create binary tree nodes with character


and frequency of each character
• Place nodes in a priority queue
– The lower the occurrence, the higher the
priority in the queue
Building a Tree
• While priority queue contains two or more nodes
– Create new node
– Dequeue node and make it left subtree
– Dequeue next node and make it right subtree
– Frequency of new node equals sum of frequency of left
and right children
– Enqueue new node back into queue
Building a Tree

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

Eerie eyes seen near lake.  26 characters


Encoding the File
Traverse Tree for Codes

• Perform a traversal of the


tree to obtain new code
words 26
• Going left is a 0 going right is 16
10
a1
• code word is only completed 4
6
e 8
8
when a leaf node is reached 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
Encoding the File
Traverse Tree for Codes

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

Huffman tree is used to compress the text(message) if we want to send a message on


network.
 JPEG image format uses Huffman algorithm for compression.
 GZIP, PKZIP (winzip etc) and BZIP2,
 Image format PNG also uses Huffman coding
 used in Video format MPEG
Exercise
Draw the huffman tree and find the codes for the following statements –

1. the quick brown fox jumps over the lazy dog


2. Eerie eye seen near the lake.
3. A cat may look at a king
4. Fortune favours the brave
Binary Tree from given inorder and preorder sequence
• Method:
• Step 1: To find the root node we select leftmost element in a preorder
sequence, let say this node is X.
• Step 2: Search this node X in Inorder sequence, all elements coming to
the left side of X are the part of left subtree and elements on right are the
part of right subtree.
• Step 3: Repeat step1 and 2 recursively for each and every node .

• 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 is invented by GM Adelson - Velsky and EM Landis in 1962.

• The tree is named AVL in honour of its inventors.

• 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.

• Tree is said to be balanced if balance factor of each node is in between -1


to 1, otherwise, the tree will be unbalanced tree and need to be balanced.

• Balance Factor (k) = height (left(k)) - height (right(k))

• 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.

• The height of an AVL tree is always O(log(n)) where n is the number of


nodes in the tree.

You might also like