0% found this document useful (0 votes)
36 views114 pages

DS UNIT-3 Complete

Uploaded by

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

DS UNIT-3 Complete

Uploaded by

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

UNIT- III

Trees – Definition, terminology, Binary trees-definition,


Properties of Binary Trees, Binary Tree ADT, representation of
Binary Trees-array and linked representations, Binary Tree
traversals, threaded binary trees,
Binary Search Tree ADT, Definition, Operations- Searching,
Insertion, Height, Traversals and Deletion,
Balanced search trees-AVL Trees-Definition and Examples.
TREES:

Definition : A Tree is a data structure in which each element is attached to one or more
elements directly beneath it.
Terminology
•The connections between elements are called branches.
•A tree has a single root, called root node, which is shown at the top of the tree.
i.e. root is always at the highest level 0. Each node has exactly one node above it,
called parent. Eg: A is the parent of B,C and D.
•The nodes just below a node are called its children. i.e. child nodes are one level
lower than the parent node.
•A node which does not have any child called leaf or terminal node.
•Nodes with at least one child are called non terminal or internal nodes.
•The child nodes of same parent are said to be siblings.
•A path in a tree is a list of distinct nodes in which successive nodes are
connected by branches in the tree.
•The length of a particular path is the number of branches in that path.
•The degree of a node of a tree is the number of children of that node.
•The total number of sub-trees attached to the node is called the degree of the
node.
•Eg: For node A degree is 3. For node K degree is 0 .
•The maximum number of children a node can have is often referred to as the
order of a tree.
BINARY TREES
Binary tree is a tree in which each node has at most two children, a left child
and a right child. Thus the order of binary tree is 2.

A binary tree is either empty or consists of a) a node called the root b)left and
right sub trees are themselves binary trees.

A binary tree is a finite set of nodes which is either empty or consists of a root
and two disjoint trees called left sub-tree and right sub-tree.

In binary tree each node will have one data field and two pointer fields for
representing the sub branches.

The degree of each node in the binary tree will be at the most two.
Types Of Binary Trees:

There are 3 types of binary trees:


1.Left skewed binary tree: If the right sub-tree is missing in every node of a tree we call
it as left skewed tree.

2.Right skewed binary tree: If the left sub-tree is missing in every node of a tree we call
it is right skewed tree.
3. Complete binary tree:
The tree in which degree of each node is at the most two is called
a complete binary tree.
In a complete binary tree there is exactly one node at level 0, two
nodes at level 1 and four nodes at level 2 and so on.

So we can say that a complete binary tree depth d will contain


exactly 2l nodes at each level l, where l is from 0 to d.

Note:
1.A binary tree of depth n will have maximum 2n -1 nodes.
2.A complete binary tree of level l will have maximum 2l nodes at
each level, where l starts from 0.
3.Any binary tree with n nodes will have at the most n+1 null
branches.
4.The total number of edges in a complete binary tree with n
terminal nodes are 2(n-1).
Difference between Full and Complete Binary Tree

A binary tree is a type of data structure where each node can only have two offspring at
most named as “left” and “right” child.
Full Binary Tree:
A full binary tree is a binary tree in which all of the nodes have either 0 or 2 offspring. In
other terms, a full binary tree is a binary tree in which all nodes, except the leaf nodes, have
two offspring.
Complete Binary Tree:
When all of the levels of a binary tree are entirely filled, except for the last
level, which can contain 1 or 2 children nodes and is filled from the left, it
is said to be a complete binary tree.

There are 2 points that you can recognize from here:


•The leftmost side of the leaf node must always be filled first.
•It isn’t necessary for the last leaf node to have a right sibling.
Binary Tree Representation
A binary tree can be represented mainly in 2 ways:
a)Sequential Representation b)Linked Representation

a) Sequential Representation
The simplest way to represent binary trees in memory is the sequential representation
that uses one-dimensional array.

1)The root of binary tree is stored in the 1 st location of array


2)If a node is in the i th location of array, then its left child is in the location 2i+1 and its
right child in the location 2i+2
3)The maximum size that is required for an array to store a tree is 2 d+1 -1, where d is the
depth of the tree.
Advantages of sequential representation:
The only advantage with this type of representation is that the direct access to
any node can be possible and finding the parent or left children of any
particular node is fast because of the random access.

Disadvantages of sequential representation:


1.The major disadvantage with this type of representation is wastage of
memory. For example in the skewed tree half of the array is unutilized.

2.In this type of representation the maximum depth of the tree has to be fixed.
Because we have decide the array size. If we choose the array size quite
larger than the depth of the tree, then it will be wastage of the memory. And if
we choose array size lesser than the depth of the tree then we will be unable
to represent some part of the tree.

3.The insertions and deletion of any node in the tree will be costlier as other
nodes has to be adjusted at appropriate positions so that the meaning of
binary tree can be preserved. As these drawbacks are there with this
sequential type of representation, we will search for more flexible
representation. So instead of array we will make use of linked list to represent
the tree.
b) Linked Representation
Linked representation of trees in memory is implemented
using pointers. Since each node in a binary tree can have
maximum two children, a node in a linked representation has
two pointers for both left and right child, and one
information field. If a node does not have any child, the
corresponding pointer field is made NULL pointer. In linked
list each node will look like this:
Advantages of linked representation:

1.This representation is superior to our array representation as there is no


wastage of memory. And so there is no need to have prior knowledge of depth
of the tree. Using dynamic memory concept one can create as much
memory(nodes) as required. By chance if some nodes are unutilized one can
delete the nodes by making the address free.

2.Insertions and deletions which are the most common operations can be done
without moving the nodes.

Disadvantages of linked representation:

1.This representation does not provide direct access to a node and special
algorithms are required.

2. This representation needs additional space in each node for storing the left
and right subtrees.
Traversing A Binary Tree

Various Tree Traversals are:

a. Pre-order
b. In-order
c.Post-order
C-B-A-D-E is the inorder traversal i.e. first we go towards the leftmost node.
i.e. C so print that node C. Then go back to the node B and print B. Then root
node A then move towards the right sub-tree print D and finally E. Thus we
are following the tracing sequence of Left|Root|Right. This type of traversal
is called inorder traversal. The basic principle is to traverse left sub-tree then
root and then the right sub-tree.
A-B-C-D-E is the preorder traversal of the above fig. We are following Root|Left|
Right path i.e. data at the root node will be printed first then we move on the left
sub-tree and go on printing the data till we reach to the left most node. Print the
data at that node and then move to the right sub-tree. Follow the same principle at
each sub-tree and go on printing the data accordingly.
From figure the postorder traversal is C-D-B-E-A. In the postorder
traversal we are following the Left|Right|Root principle i.e. move to the
leftmost node, if right sub-tree is there or not if not then print the
leftmost node, if right sub-tree is there move towards the right most
node. The key idea here is that at each subtree we are following the Left|
Right|Root principle and print the data accordingly.
Threaded binary tree:-
"A binary tree is threaded by making all right child pointers
that would normally be null point to the inorder successor of
the node (if it exists), and all left child pointers that would
normally be null point to the inorder predecessor of the
node."
There are two types of threaded binary trees.
Single Threaded: Where a NULL right pointers is made to point to the inorder successor (if
successor exists)
Double Threaded: Where both left and right NULL pointers are made to point to inorder
predecessor and inorder successor respectively. The predecessor threads are useful for
reverse inorder traversal and postorder traversal.
The threads are also useful for fast accessing ancestors of a node.
Following diagram shows an example Single Threaded Binary Tree. The dotted lines
represent threads.

The idea of threaded binary trees is to make inorder traversal faster and do it without stack
and without recursion
C and C++ representation of a Threaded Node
Following is C and C++representation of a single-threaded node.
Use any below method to represent a Threaded Node

Method 1: Using "struct" to make user-define data type

struct node
{ Method 2: Using "class" to make user-define data type
int data; class Node
struct node* left; {
struct node* right; public:
bool rightThread; int data;
}; Node* left;
Node* right;
bool rightThread;
// Val is the key or the value that has to be added to the data
part
Node(int val)
{
data = val;
// Left and right child for node will be initialized to null
left = NULL;
right = NULL;
// rightThread will be initialized to false
rightThread = false;
}
};
Inorder Traversal using Threads // CPP code to do inorder traversal in a
Following is code for inorder traversal in a threaded binary tree
threaded binary tree. void inOrder(Node* root)
// Utility function to find leftmost node in {
a tree rooted with n Node* cur = leftMost(root);
Node* leftMost(Node* n) while (cur != NULL)
{ {
if (n == NULL) cout<<cur->data<<" ";
return NULL;
// If this node is a thread node, then go to inorder
while (n->left != NULL) successor
n = n->left; if (cur->rightThread)
cur = cur->right;
return n; else
// Else go to the leftmost child in right subtree
}
cur = leftmost(cur->right);
}
}
Advantages of Threaded Binary Tree
•In this Tree it enables linear traversal of elements.
•It eliminates the use of stack as it perform linear traversal, so save memory.
•Enables to find parent node without explicit use of parent pointer
•Threaded tree give forward and backward traversal of nodes by in-order fashion
•Nodes contain pointers to in-order predecessor and successor
•For a given node, we can easily find inorder predecessor and successor. So, searching
is much more easier.

Disadvantages of Threaded Binary Tree


•Every node in threaded binary tree need extra information(extra memory) to indicate
whether its left or right node indicated its child nodes or its inorder predecessor or
successor. So, the node consumes extra memory to implement.

•Insertion and deletion are way more complex and time consuming than the normal one
since both threads and ordinary links need to be maintained.
There are many ways to thread a binary tree these are—
1.The right NULL pointer of each leaf node can be replaced
by a thread to the successor of that node under in order
traversal called a right thread, and the tree will called a right
in- threaded tree or right threaded binary tree.

INORDER TRAVERSAL:
DBHEAFCG
2.The left NULL pointer of each node can be replaced
by a thread to the predecessor of that node under in
order traversal called left thread, and the tree will called
a left inthreaded tree.
INORDER TRAVERSAL:
DBHEAFCG
3.Both left and right NULL pointers can be used to point
to predecessor and successor of that node respectively,
under in order traversal. Such a tree is called a fully
threaded tree. A threaded binary tree where only one
thread is used is also known as one way threaded tree and
where both threads are used is also known as two way
threaded tree.
INORDER TRAVERSAL:
DBHEAFCG
TREES
A Tree is a data structure in which each element is attached to one or more elements
directly beneath it
Terminology
The connections between elements are called branches.
A tree has a single root, called root node, which is shown at the top of the tree. i.e. root is
always at the highest level 0.
Each node has exactly one node above it, called parent. Eg: A is the parent of B,C and D.
The nodes just below a node are called its children. ie. child nodes are one level lower than
the parent node.
A node which does not have any child called leaf or terminal node. Eg: E, F, K, L, H, I and
M are leaves.
Nodes with at least one child are called non terminal or internal nodes.
The child nodes of same parent are said to be siblings.
A path in a tree is a list of distinct nodes in which successive nodes are connected by
branches in the tree.
 The length of a particular path is the number of branches in that path.
 The degree of a node of a tree is the number of children of that node.
The maximum number of children a node can have is often referred to as the order of a
tree. The height or depth of a tree is the length of the longest path from root to any leaf.
1. Root: This is the unique node in the tree to which further sub trees are attached. Eg: A
2. Degree of the node: The total number of sub-trees attached to the node is called the
degree of the node.Eg: For node A degree is 3. For node K degree is 0
3.Leaves: These are the terminal nodes of the tree. The nodes with degree 0 are always the
leaf nodes. Eg: E, F, K, L,H, I, J
4.Internal nodes: The nodes other than the root node and the leaves are called the internal
nodes. Eg: B, C, D, G
5.Parent nodes: The node which is having further sub-trees(branches) is called the parent
node of those sub-trees. Eg: B is the parent node of E and F.
6.Predecessor: While displaying the tree, if some particular node occurs previous to some
other node then that node is called the predecessor of the other node. Eg: E is the
predecessor of the node B.
7.Successor: The node which occurs next to some other node is a successor node. Eg: B is
the successor of E and F.
8.Level of the tree: The root node is always considered at level 0, then its adjacent children
are supposed to be at level 1 and so on. Eg: A is at level 0, B,C,D are at level 1, E,F,G,H,I,J
are at level 2, K,L are at level 3.
9.Height of the tree: The maximum level is the height of the tree. Here height of the tree is
3. The height if the tree is also called depth of the tree.
10.Degree of tree: The maximum degree of the node is called the degree of the tree.
BINARY SEARCH TREE
In the simple binary tree the nodes are arranged in any fashion.
Depending on user’s desire the new nodes can be attached as a left
or right child of any desired node. In such a case finding for any
node is a long cut procedure, because in that case we have to
search the entire tree.

And thus the searching time complexity will get increased


unnecessarily. So to make the searching algorithm faster in a
binary tree we will go for building the binary search tree. The
binary search tree is based on the binary search algorithm.

While creating the binary search tree the data is systematically


arranged.

That means values at left sub-tree < root node value < right
sub-tree values.
Operations On Binary Search Tree:
The basic operations which can be performed on binary search tree are.
1. Insertion of a node in binary search tree.
2. Deletion of a node from binary search tree.
3. Searching for a particular node in binary search tree.

Insertion of a node in binary search tree. While inserting any node in binary
search tree, look for its appropriate position in the binary search tree. We
start comparing this new node with each node of the tree.
If the value of the node which is to be inserted is greater than the value of the
current node we move on to the right sub-branch otherwise we move on to
the left sub-branch.
As soon as the appropriate position is found we attach this new node as left or
right child appropriately.
Before Insertion
In the above fig, if we want to insert 23. Then we will start
comparing 23 with value of root node i.e. 10. As 23 is greater than
10, we will move on right sub-tree. Now we will compare 23 with
20 and move right, compare 23 with 22 and move right. Now
compare 23 with 24 but it is less than 24. We will move on left
branch of 24. But as there is node as left child of 24, we can
attach 23 as left child of 24.
Deletion of a node from binary search tree
For deletion of any node from binary search tree there are three which are
possible.
i. Deletion of leaf node.
ii. Deletion of a node having one child.
iii. Deletion of a node having two children.

Deletion of leaf node


This is the simplest deletion, in which we set the left or right pointer of parent node as
NULL.
From the above fig, we want to delete the node having value 5 then we will set left
pointer of its parent node as NULL. That is left pointer of node having value 7 is set to
NULL.
Deletion of a node having one child
To explain this kind of deletion, consider a tree as given below.

If we want to delete the node 15, then we will simply copy node
18 at place of 15 and then set the node free
Deletion of a node having two children
Consider a tree as given below.

Let us consider that we want to


delete node having value 7. We will
then find out the inorder successor
of node 7. We will then find out the
inorder successor of node 7. The
inorder successor will be simply
copied at location of node 7. That
means copy 8 at the position where
value of node is 7. Set left pointer
of 9 as NULL. This completes the
deletion procedure.
Inorder Traversal: 4567891011
Searching for a node in binary search tree.
In searching, the node which we want to search is called a key
node.
The key node will be compared with each node starting from
root node if value of key node is greater than current node
then we search for it on right sub branch otherwise on left
sub branch.
If we reach to leaf node and still we do not get the value of
key node then we declare ―node is not present in the tree.

In the above tree, if we want to search for value 9. Then we will compare 9 with root
node 10. As 9 is less than 10 we will search on left sub branch. Now compare 9 with 5,
but 9 is greater than 5. So we will move on right sub tree. Now compare 9 with 8 but 9
is greater than 8 we will move on right sub branch. As the node we will get holds the
value 9. Thus the desired node can be searched.
AVL TREES

Adelsion Velski and Lendis in 1962 introduced binary tree


structure that is balanced with respect to height of sub
trees. The tree can be made balanced and because of
this retrieval of any node can be done in Ο(log n) times,
where n is total number of nodes. From the name of
these scientists the tree is called AVL tree.
Definition:
An empty tree is height balanced if T is a non empty binary tree with TL and TR as its
left and right sub trees.
The T is height balanced if and only if :
i. TL and TR are height balanced.
ii. hL-hR <= 1 where hL and hR are heights of TL and TR.
The idea of balancing a tree is obtained by calculating the balance factor of a tree.

Definition of Balance Factor:


The balance factor BF(T) of a node in binary tree is defined to be hL-hR,where hL and
hR are heights of left and right sub trees of T.

For any node in AVL tree the balance factor i.e. BF(T) is -1, 0 or +1.
Height of AVL Tree: Theorem: The height of AVL tree with n elements
(nodes) is O(log n).

Proof: Let an AVL tree with n nodes in it.


Nh be the minimum number of nodes in an AVL tree of height h.
In worst case, one sub tree may have height h-1 and other sub tree may have
height h-2. And both these sub trees are AVL trees. Since for every node in
AVL tree the height of left and right sub trees differ by at most 1.
Hence
Representation of AVL Tree
The AVL tree follows the property of binary search tree. In
fact AVL trees are basically binary search trees with balance
factors as -1, 0, or +1. After insertion of any node in an AVL
tree if the balance factor of any node becomes other than -1, 0,
or +1 then it is said that AVL property is violated. Then we
have to restore the destroyed balance condition. The balance
factor is denoted at right top corner inside the node.
After insertion of a new node if balance condition gets
destroyed, then the nodes on that path(new node insertion
point to root) needs to be readjusted. That means only the
affected sub tree is to be rebalanced. The rebalancing
should be such that entire tree should satisfy AVL property.
In above given example-
Insertion of a node

There are four different cases when rebalancing is required after


insertion of new node.
1. An insertion of new node into left sub tree of left child. (LL).
2. An insertion of new node into right sub tree of left child. (LR).
3. An insertion of new node into left sub tree of right child. (RL).
4. An insertion of new node into right sub tree of right child.
(RR).
Example: Insert 1, 25, 28, 12 in the following AVL tree.
Insert 1 To insert node ‗1‘ we have to attach it as a left child of ‗2‘. This will
unbalance the tree as follows. We will apply LL rotation to preserve AVL property
of it.
Insert 25 We will attach 25 as a right child of 18. No balancing is required as entire
tree preserves the AVL property
Insert 28 The node ’28’ is attached as a right child of 25. RR rotation is
required to rebalance.
To rebalance the tree we have to apply LR
rotation.
Deletion:
Even after deletion of any particular node from AVL tree, the tree has to be
restructured in order to preserve AVL property. And thereby various
rotations need to be applied.

Algorithm for deletion:


The deletion algorithm is more complex than insertion algorithm.
1. Search the node which is to be deleted.
2. a) If the node to be deleted is a leaf node then simply make it NULL to
remove.
b) If the node to be deleted is not a leaf node i.e. node may have one or two
children, then the node must be swapped with its in order successor. Once
the node is swapped, we can remove this node.
3. Now we have to traverse back up the path towards root, checking the
balance factor of every node along the path. If we encounter unbalancing in
some sub tree then balance that sub tree using appropriate single or double
rotations. The deletion algorithm takes O(log n) time to delete any node.
Inorder Traversal:
1 2 5 7 10 11 12 13 14 18 25 28 10
Searching: The searching of a node in an AVL tree is very
simple. As AVL tree is basically binary search tree, the
algorithm used for searching a node from binary search tree is
the same one is used to search a node from AVL tree. The
searching of a node from AVL tree takes O(log n) time.
BTREES

Multi-way(m-way) trees are tree data structures with more than two branches
at a node. The data structures of m-way search trees, B trees and Tries
belong to this category of tree structures.

AVL search trees are height balanced versions of binary search trees, provide
efficient retrievals and storage operations.
The complexity of insert, delete and search operations on AVL search trees id
O(log n).

Applications such as File indexing where the entries in an index may be very
large, maintaining the index as m-way search trees provides a better option
than AVL search trees which are but only balanced binary search trees.

While binary search trees are two-way search trees, m-way search trees
are extended binary search trees and hence provide efficient retrievals.

B trees are height balanced versions of m-way search trees and they do
not recommend representation of keys with varying sizes.
Tries are tree based data structures that support keys with varying sizes.
Definition: A B tree of order m is an m-way search tree and
hence may be empty. If non empty, then the following
properties are satisfied on its extended tree representation:
i. The root node must have at least two child nodes and at
most m child nodes.
ii. All internal nodes other than the root node must have at
least |m/2 | non empty child nodes and at most m non empty
child nodes.
iii. The number of keys in each internal node is one less than
its number of child nodes and these keys partition the keys
of the tree into sub trees.
iv. All external nodes are at the same level.
Insertion For example construct a B-tree of order 5 using
following numbers. 3, 14, 7, 1, 8, 5, 11, 17, 13, 6, 23, 12, 20,
26, 4, 16, 18, 24, 25, 19 The order 5 means at the most 4 keys
are allowed. The internal node should have at least 3 non
empty children and each leaf node must contain at least 2
keys.
Thus the B tree is constructed.
Height of B-tree
The maximum height of B-tree gives an upper bound on number of disk access. The
maximum number of keys in a B-tree of order 2m and depth h is

You might also like