Unit-2 Trees& AVL Tree
Unit-2 Trees& AVL Tree
Trees
Module 4: Trees
Weighta
Topics Hours Books
ge
2
Definition of Tree
● A tree is a finite set of one or more nodes
such that:
○ There is a specially designated node called
the root.
○ The remaining nodes are partitioned into
n>=0 disjoint sets T1, ..., Tn, where each of
these sets is a tree.
○ We call T1, ..., Tn the subtrees of the root.
3
Trees
4
Types of Trees
Trees are of following 6 types:
1. General trees
2. Forests
3. Binary trees
4. Binary search trees
5. Expression trees
6. Tournament trees
Binary Trees
● A binary tree is a data structure that is defined as a collection of
elements called nodes
● In a binary tree, the topmost element is called the root node, and
each node has 0, 1, or at the most 2 children.
● A node that has zero children is called a leaf node or a terminal
node.
● Every node contains a data element, a left pointer which points
to the left child, and a right pointer which points to the right child
Representation of Binary Trees in the Memory
● In the computer’s memory, a binary tree can be maintained either by
using a linked representation or by using a sequential
representation.
There are three binary tree components. Every binary tree node has these three
1. Data element
2. Pointer to left subtree
3. Pointer to right subtree
TYPES OF BINARY TREE
Types of Binary Tree
○ Pre-order Traversal
○ In-order Traversal
○ Post-order Traversal
Pre-order Traversal
To traverse a non-empty binary tree in pre-order, the
following operations are performed recursively at
each node. The algorithm works by:
preorder traversal:- + – a b * c d
In-order Traversal
To traverse a non-empty binary tree in In-order, the
following operations are performed recursively at
each node. The algorithm works by:
Inorder traversal:-
G, D, H, L, B, E, A, C, I, F, K, and J
Post-order Traversal
To traverse a non-empty binary tree in Post-order, the
following operations are performed recursively at
each node. The algorithm works by:
Postorder traversal:-
G, L, H, D, E, B, I, K, J, F, C, and A
Constructing a Binary Tree from Traversal Results
● In–order Traversal: D B E A F C G
● Pre–order Traversal: A B D E C F G
Solution:-
Step 1 Step 2
Constructing a Binary Tree from Traversal Results
example-2
Construct the binary tree for the inorder and Post order traversal sequence given
below
Step 3 Step 4
Step 5
Constructing a Binary Tree from Traversal Results
example-3
Construct the binary tree for the inorder and preorder traversal sequence given below:
INORDER: ENGINEERING PRE ORDER: EGNENIIRENG (May 2017
10 marks)
Solution:-
Constructing a Binary Tree from Traversal Results
example-3
Construct the binary tree for the inorder and preorder traversal sequence given below:
INORDER: ENGINEERING PRE ORDER: EGNENIIRENG (May 2017
10 marks)
Solution:-
Constructing a Binary Tree from Traversal Results
example-3
Construct the binary tree for the inorder and preorder traversal sequence given below:
INORDER: ENGINEERING PRE ORDER: EGNENIIRENG (May 2017
10 marks)
Solution:-
Exercise
Construct the binary tree for the inorder and preorder
traversal sequence given below:
1) Inorder Traversal : { 4, 2, 1, 7, 5, 8, 3, 6 }
Preorder Traversal: { 1, 2, 4, 3, 5, 7, 8, 6 }
2) Inorder sequence: D B E A F C
Preorder sequence: A B D E C F
Binary Search Trees
● A binary search tree, also known as an
ordered binary tree, is a variant of
binary trees in which the nodes are
arranged in an order.
● In a binary search tree, all the nodes in
the left sub-tree have a value less than
that of the root node. Correspondingly,
all the nodes in the right sub-tree have
a value either equal to or greater than
the root node.
● The same rule is applicable to every sub-
tree in the tree. (Note that a binary
search tree may or may not contain
duplicate values, depending on its
implementation.)
Binary Search Trees
● State whether the binary trees in Fig. are binary search trees
or not.
Binary Search Trees
● Create a binary search tree using the following data elements:
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81
Solution
Binary Search Trees
● Create a binary search tree using the following data elements:
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81
Solution cont...
Binary Search Trees
● Create a binary search tree using the following data elements:
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81
Solution cont...
Operations On Binary Search Trees
● Searching for a Node in a Binary Search Tree
● Inserting a New Node in a Binary Search Tree
● Deleting a Node from a Binary Search Tree
○ Case 1: Deleting a Node that has No Children
○ Case 2: Deleting a Node with One Child
○ Case 3: Deleting a Node with Two Children
● Determining the Height of a Binary Search Tree
● Determining the Number of Nodes
○ Determining the Number of Internal Nodes
○ Determining the Number of External Nodes
● Finding the Mirror Image of a Binary Search Tree
● Deleting a Binary Search Tree
● Finding the Smallest Node in a Binary Search Tree
● Finding the Largest Node in a Binary Search Tree
Operations On Binary Search Trees
● Searching for a Node in a Binary Search Tree
Operations On Binary Search Trees
● Searching for a Node in a Binary Search Tree
Operations On Binary Search Trees
● Inserting a New Node in a Binary Search Tree
Operations On Binary Search Trees
● Inserting a New Node in a Binary Search Tree
Operations On Binary Search Trees
Left-heavy tree
Right-heavy tree
Balanced Tree
AVL TREES
Balance factor = Height (left sub-tree) – Height (right sub-tree)
left-heavy tree:-If the balance factor of a node is 1, then it means that the
left sub-tree of the tree is one level higher than that of the right sub-tree.
Such a tree is therefore called as a left-heavy tree.
left-heavy tree.
AVL TREES
Balance factor = Height (left sub-tree) – Height (right sub-tree)
Balanced Tree:- if the balance factor of a node is 0, then it means that the
height of the left sub-tree (longest path in the left sub-tree) is equal to the
height of the right sub-tree.
AVL TREES
Balance factor = Height (left sub-tree) – Height (right sub-tree)
Right-heavy tree
Rebalancing an AVL tree
2. Every node in the B tree except the root node and leaf nodes has at least
(minimum) m/2 children. This condition helps to keep the tree bushy so that
the path from the root node to the leaf is very short, even in a tree that stores
a lot of data.
3. The root node has at least two children if it is not a terminal (leaf) node.
While a B tree can store both keys and records in its interior nodes, a
B+ tree, in contrast, stores all the records at the leaf level of the
tree; only keys are stored in the interior nodes.
B+ trees store data only in the leaf nodes. All other nodes (internal
nodes) are called index nodes or i-nodes and store index values.
This allows us to traverse the tree from the root down to the leaf
node that stores the desired data item.
B+ TREES
Suppose we have 105 characters in a data file. Normal Storage: 8 bits per
character (ASCII) - 8 x 105 bits in a file. But we want to compress the file
and save it compactly. Suppose only six characters appear in the file:
1. n=|C|
2. Q ← C
3. for i=1 to n-1
4. do
5. z= allocate-Node ()
6. x= left[z]=Extract-Min(Q)
7. y= right[z] =Extract-Min(Q)
8. f [z]=f[x]+f[y]
9. Insert (Q, z)
10. return Extract-Min (Q)
Huffman Codes
Example: Find an optimal Huffman Code for the following set of
frequencies:
1. a: 50 b: 25 c: 15 d: 40 e: 75
Solution:
Huffman Codes
Huffman Codes
Again for i=2
Huffman
Codes
Huffman Codes
Similarly, we apply the same
process we get
Huffman Codes
Huffma
n
codes:
c - 000
b- 001
d- 01
a- 10
e- 11
Huffman Codes
Example 2 Create Huffman Tree for the following characters
along with their frequencies using the above algorithm-
Huffman Codes
Huffman Codes
Step:-2
Huffman Codes
Step:-3
Fig 7: Final Huffman tree obtained by combining internal nodes having 25 and 33 as
frequency
Huffman Codes
Step:-8
https://www.youtube.com/watch?
v=VKV8PYhcQoo