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

Unit-2 Trees& AVL Tree

Uploaded by

omkarmande
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit-2 Trees& AVL Tree

Uploaded by

omkarmande
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 112

Module 2

Trees
Module 4: Trees

Weighta
Topics Hours Books
ge

Introduction, Tree Terminologies,


Binary Tree, Binary Tree
Representation, Types of Binary
Tree, Binary Tree Traversals, Binary
Search Tree, Operations on Binary T2, T3
Search Tree, Applications of Binary and T4,
Tree-Expression Tree, Huffman 11 28%
R1 . R2
Encoding, Search Trees-AVL,
rotations in AVL Tree, operations on , R5
AVL Tree, Introduction of B Tree, B+
Tree.

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.

● Linked representation of binary trees In the linked representation of


a binary tree, every node will have three parts:
1. the data element
2. a pointer to the left node,
3. a pointer to the right node.

So in C, the binary tree is built with a node type given below.


struct node
{
struct node *left;
int data;
struct node *right;
};
Binary Tree Components

There are three binary tree components. Every binary tree node has these three

components associated with it.

1. Data element
2. Pointer to left subtree
3. Pointer to right subtree
TYPES OF BINARY TREE
Types of Binary Tree

1. Full Binary Tree:

1. A Binary Tree is a full binary tree if every node


has 0 or 2 children.
2. We can also say a full binary tree is a binary tree
in which all nodes except leaf nodes have two
children.
3. A full Binary tree is a special type of binary tree
in which every parent node/internal node has
either two or no children.
4. It is also known as a proper binary tree.
2. Complete Binary Tree

A Binary Tree is a Complete Binary Tree if all the


levels are completely filled except possibly the last
level and the last level has all keys as left as
possible.
A complete binary tree is just like a full binary tree,
but with two major differences:

● Every level must be completely filled


● All the leaf elements must lean towards the left.
● The last leaf element might not have a right
sibling i.e. a complete binary tree doesn’t have
to be a full binary tree.
Skewed Binary Tree
● A skewed binary tree is a
pathological/degenerate tree in
which the tree is either dominated
by the left nodes or the right nodes.
● Thus, there are two types of
skewed binary tree: left-skewed
binary tree and right-skewed binary
tree.
Strictly Binary Tree
● If every non terminal node in a
binary tree consist of non empty
left subtree and right subtree then
such tree is called as strictly binary
tree.
● A node will have either two
children or no child at all
Extended Binary Tree
● In extended binary tree each empty sub
tree is replaced by a failure node
● It is represented by
● Node with 2 children are called internal
nodes
● Node with zero children are called as
external node
TRAVERSING A BINARY TREE
● Traversing a binary tree is the process of visiting each node
in the tree exactly once in a systematic way.

● Unlike linear data structures in which the elements are


traversed sequentially, tree is a non-linear data structure in
which the elements can be traversed in many different ways.

● There are different algorithms for tree traversals. These


algorithms differ in the order in which the nodes are visited.

○ 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:

1. Visiting the root node,


2. Traversing the left sub-tree, and finally
3. Traversing the right sub-tree.

Pre-order traversal: A-B-C


Pre-order Traversal

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:

1. Traversing the left sub-tree,


2. Visiting the root node, and finally
3. Traversing the right sub-tree.

In-order traversal: B-A-C


In-order Traversal

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:

1. Traversing the left sub-tree,


2. Traversing the right sub-tree, and finally
3. Visiting the root node.

In-order traversal: B-C-A


Post-order Traversal

Postorder traversal:-

G, L, H, D, E, B, I, K, J, F, C, and A
Constructing a Binary Tree from Traversal Results

● We can construct a binary tree if we are given at least two


traversal results.

● The first traversal must be the in-order traversal and the


second can be either pre-order or post-order traversal.

● The in-order traversal result will be used to determine the


left and the right child nodes

● the pre-order/post-order can be used to determine the root


node.
Constructing a Binary Tree from Traversal Results
For example, consider the traversal results given below:

● In–order Traversal: D B E A F C G
● Pre–order Traversal: A B D E C F G

Follow the steps given below to construct the tree:


Step 1 Use the pre-order sequence to determine the root node of
the tree. The first element would be the root node. here A is root
Step 2 Elements on the left side of the root node in the in-order
traversal sequence form the left sub-tree of the root node.
Similarly, elements on the right side of the root node in the in-
order traversal sequence form the right sub-tree of the root node.
Constructing a Binary Tree from Traversal Results
example cont…..
● In–order Traversal: D B E A F C G
● Pre–order Traversal: A B D E C F G

Step 3 Recursively select each element from pre-order traversal


sequence and create its left and right sub-trees from the in-order traversa
sequence.
Constructing a Binary Tree from Traversal Results
example-2
Construct the binary tree for the inorder and Post order traversal sequence given below

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

● Since the nodes in a binary search tree


are ordered, the time needed to search
an element in the tree is greatly reduced.

● Whenever we search for an element, we


do not need to traverse the entire tree.

● At every node, we get a hint regarding


which sub-tree to search in.
Binary Search Trees
● Since the nodes in a binary search tree are
ordered, the time needed to search an element
in the tree is greatly reduced.

● Whenever we search for an element, we do not


need to traverse the entire tree.

● At every node, we get a hint regarding which


sub-tree to search in.

● Binary search trees also speed up the insertion


and deletion operations.

● The tree has a speed advantage when the data


in the structure changes rapidly.

● Binary search trees are considered to be


efficient data structures especially when
compared with sorted linear arrays and linked
lists.
Binary Search Trees

● In worst case, a binary search


tree will take O(n) time to
search for an element. The
worst case would occur when
the tree is a linear chain of
nodes as given in Fig.
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

Case 1: Deleting a Node that has No Children


Operations On Binary Search Trees
Case 2: Deleting a Node with One Child
Operations On Binary Search Trees
Case 3: Deleting a Node with Two Children
Operations On Binary Search Trees
Case 3: Deleting a Node with Two Children
Operations On Binary Search Trees
Deleting a Node
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
Write a program to create a binary search tree and perform
all the operations
AVL TREES
● AVL tree is a self-balancing binary search tree invented
by G.M. Adelson-Velsky and E.M. Landis in 1962.

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

● In an AVL tree, the heights of the two sub-trees of a


node may differ by at most one.

● Due to this property, the AVL tree is also known as a


height-balanced tree.
AVL TREES
● In its structure, it stores an additional variable called the
BalanceFactor.

● Thus, every node has a balance factor associated with it.

● The balance factor of a node is calculated by subtracting the


height of its right sub-tree from the height of its left sub-tree.

● A binary search tree in which every node has a balance factor


of –1, 0, or 1 is said to be height balanced.

● A node with any other balance factor is considered to be


unbalanced and requires rebalancing of the tree.

Balance factor = Height (left sub-tree) – Height (right sub-tree)


AVL TREES
Balance factor = Height (left sub-tree) – Height (right sub-tree)

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:- If the balance factor of a node is –1, then it


means that the left sub-tree of the tree is one level lower than
that of the right sub-tree. Such a tree is therefore called as a
right-heavy tree

Right-heavy tree
Rebalancing an AVL tree

● Insertions and deletions from an AVL tree may disturb the


balance factor of the nodes and, thus, rebalancing of the tree
may have to be done.

● The tree is rebalanced by performing rotation at the critical


node.

There are four types of rotations:


● LL rotation
● RR rotation
● LR rotation
● RL rotation

The type of rotation that has to be done will vary depending


on the particular situation.
Operations on AVL Trees
Searching for a Node in an AVL Tree

● Searching in an AVL tree is performed exactly the same way as it is performed


in a binary search tree.
● Due to the height-balancing of the tree, the search operation takes O(log n) time
to complete.
● Since the operation does not modify the structure of the tree, no special
provisions are required.

Inserting a New Node in an AVL Tree


● Insertion in an AVL tree is also done in the same way as it is done in a binary
search tree.
● In the AVL tree, the new node is always inserted as the leaf node.
● But the step of insertion is usually followed by an additional step of rotation.
Rotation is done to restore the balance of the tree.
Operations on AVL Trees
Inserting a New Node in an AVL Tree
Operations on AVL Trees
Inserting a New Node in an AVL Tree
Operations on AVL Trees
Inserting a New Node in an AVL Tree

LL rotation The new node is inserted in the left sub-tree of


the left sub-tree of the critical node.

RR rotation The new node is inserted in the right sub-tree of


the right sub-tree of the critical node.

LR rotation The new node is inserted in the right sub-tree of


the left sub-tree of the critical node.

RL rotation The new node is inserted in the left sub-tree of


the right sub-tree of the
LL rotation
RR Rotation
LR Rotations
RL Rotations
Construct an AVL tree by inserting the following
elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following
elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following
elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Multi-way Search Trees
● M-way search tree which has M – 1 values per node and M subtrees.
● In such a tree, M is called the degree of the tree.
● Note that in a binary search tree M = 2, so it has one value and two
sub-trees.
● In other words, every internal node of an M-way search tree consists
of pointers to M sub-trees and contains M – 1 keys, where M > 2.

● In the structure shown, P0 , P1 , P2 , ..., Pn are pointers to the node’s


sub-trees and K0 , K1 , K2 , ..., Kn–1 are the key values of the node.
● All the key values are stored in ascending order. That is, Ki < Ki+1 for
0 £ i £ n–2
Multi-way Search Trees
B TREES
● A B tree is a specialized M-way tree developed by Rudolf
Bayer and Ed McCreight in 1970 that is widely used for disk
access.
● A B tree of order m can have a maximum of m–1 keys and m
pointers to its sub-trees.
● A B tree may contain a large number of key values and
pointers to sub-trees.
● Storing a large number of keys in a single node keeps the
height of the tree relatively small.
B TREES
● A B tree is designed to store sorted data and allows search,
insertion, and deletion operations to be performed in
logarithmic amortized time.
● A B tree of order m (the maximum number of children that
each node can have) is a tree with all the properties of an M-
way search tree.
B TREES
● In addition it has the following properties:
1. Every node in the B tree has at most (maximum) m children.

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.

4. All leaf nodes are at the same level.


Applications of B trees
B trees are often used to index the data and provide
fast access.

Consider a situation in which we have to search an un-indexed


and unsorted database that contains n key values. The worst case
running time to perform this operation would be O(n).
In contrast, if the data in the database is indexed with a B tree, the
same search operation will run in O(log n).

For example, searching for a single key on a set of one million


keys will at most require 1,000,000 comparisons. But if the same
data is indexed with a B tree of order 10, then only 114
comparisons will be required in the worst case.
B+ TREES
A B+ tree is a variant of a B tree which stores sorted data in a
way that allows for efficient insertion, retrieval, and removal of
records, each of which is identified by a key.

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

The advantages of B+ trees can be given as follows:

1. Records can be fetched in equal number of disk accesses


2. It can be used to perform a wide range of queries easily as
leaves are linked to nodes at the upper level
3. Height of the tree is less and balanced
4. Supports both random and sequential access to records 5.
Keys are used for indexing
Huffman Codes
(i) Data can be encoded efficiently using Huffman Codes.

(ii) It is a widely used and beneficial technique for compressing


data.

(iii) Huffman's greedy algorithm uses a table of the frequencies of


occurrences of each character to build up an optimal way of
representing each character as a binary string.

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:

How can we represent the data in a Compact way?


Algorithm of Huffman Code
Huffman (C)

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 3: Combining node u with an internal node having 4 as frequency


Huffman Codes
Step:-4

Fig 4: Combining node a wit an internal node having 8 as frequency


Huffman Codes
Step:-5

Fig 5: Combining nodes i and s


Huffman Codes
Step:-6

Fig 6: Combining node e with an internal node having 18 as frequency


Huffman Codes
Step:-7

Fig 7: Final Huffman tree obtained by combining internal nodes having 25 and 33 as
frequency
Huffman Codes
Step:-8

Fig 8: Assigning binary codes to


Huffman tree
https://www.youtube.com/watch?
v=vEZ3-bdPdUQ

https://www.youtube.com/watch?
v=VKV8PYhcQoo

You might also like