0% found this document useful (0 votes)
38 views23 pages

2027 2st Unit

This document discusses different tree data structures including binary trees, binary search trees, and self-balancing trees like AVL trees and red-black trees. It defines key properties of different tree types and covers tree traversal methods and operations like insertion and deletion in binary search trees. The document also discusses B-trees and segment trees as well as applications of binary search trees.

Uploaded by

VIKRAM SHARMA
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)
38 views23 pages

2027 2st Unit

This document discusses different tree data structures including binary trees, binary search trees, and self-balancing trees like AVL trees and red-black trees. It defines key properties of different tree types and covers tree traversal methods and operations like insertion and deletion in binary search trees. The document also discusses B-trees and segment trees as well as applications of binary search trees.

Uploaded by

VIKRAM SHARMA
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/ 23

VISION INSTITUTE OF TECHNOLOGY,ALIGARH

Subject: data structure

Unit-4

Trees: Introduction to Tree & its Terminology, Binary trees, Types of Binary trees,
Representation of Binary Tree, Traversals (Inorder, Preorder, Postorder),Tree Expression,
Binary Search Tree, Insertion and Deletion in BST.

Outcome of this Unit:

 Understanding of fundamental tree concepts and terminology.


 Ability to work with binary trees and knowledge of different types of binary trees.
 Knowledge of various tree traversal methods.
 Understanding the representation of binary trees.
 Proficiency in creating and manipulating binary search trees, including insertion and
deletion operations.
Tree: A tree data structure is a hierarchical structure that is used to
represent and organize data in a way that is easy to navigate and
search. It is a collection of nodes that are connected by edges and
has a hierarchical relationship between the nodes.

Page1 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

Binary Tree: A binary Tree is defined as a Tree data structure with at


most 2 children. Since each element in a binary tree can have only 2
children, we typically name them the left and right child.
Example:
Consider the tree below. Since each node of this tree has only 2
children, it can be said that this tree is a Binary Tree

Types of Binary Tree:


1. Complete Binary Tree
2. Perfect Binary Tree
3. Balanced Binary Tree
Complete Binary tree: A complete binary tree is a special type of
binary tree where all the levels of the tree are filled completely except
the lowest level nodes which are filled from as left as possible.

2h+1-1 = 22+1-1 = 23-1 = 7.

Perfect Binary Tree?


A perfect binary tree is a special type of binary tree in which all the leaf
nodes are at the same depth, and all non-leaf nodes have two children. In
simple terms, this means that all leaf nodes are at the maximum depth of the
tree, and the tree is completely filled with no gaps.

Page2 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

The maximum number of nodes in a perfect binary tree is given by the


formula 2^(d+1) – 1, where d is the depth of the tree. This means that a
perfect binary tree with a depth of n has 2^n leaf nodes and a total
of 2^(n+1) – 1 nodes.
Properties of a Perfect Binary Tree:
 Degree: The degree of a node of a tree is defined as the number of
children of that node. All the internal nodes have a degree of 2. The leaf
nodes of a perfect binary tree have a degree of 0.
 Number of leaf nodes: If the height of the perfect binary tree is h, then
the number of leaf nodes will be 2h because the last level is completely
filled.
 Depth of a node: Average depth of a node in a perfect binary tree
is Θ(ln(n)).
 Relation between leaf nodes and non-leaf nodes: No. of leaf nodes =
No. of non-leaf nodes +1.
 Total number of nodes: A tree of height h has total nodes = 2h+1 – 1.
Each node of the tree is filled. So total number of nodes can be calculated
as 20 + 21 + . . . + 2h = 2h+1 – 1.
 Height of the tree: The height of a perfect binary tree with N number of
nodes = log(N + 1) – 1 = Θ(ln(n)). This can be calculated using the
relation shown while calculating the total number of nodes in a perfect
binary tree.
Balanced Binary Tree
A binary tree is balanced if the height of the tree is O(Log n) where n is the
number of nodes. For Example, the AVL tree maintains O(Log n) height by
making sure that the difference between the heights of the left and right
subtrees is at most 1. Red-Black trees maintain O(Log n) height by making
sure that the number of Black nodes on every root-to-leaf path is the same
and that there are no adjacent red nodes. Balanced Binary Search trees are
performance-wise good as they provide O(log n) time for search, insert and
delete.
AVL TREE: AVL tree is a self-balancing Binary Search Tree (BST) where the
difference between heights of left and right subtrees for any node cannot be more
than one. For all nodes.

Page3 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

Red-Black Tree
A red-black tree is a kind of self-balancing binary search tree where each
node has an extra bit, and that bit is often interpreted as the color (red or
black). These colors are used to ensure that the tree remains balanced
during insertions and deletions.
Although the balance of the tree is not perfect, it is good enough to reduce
the searching time and maintain it around O(log n) time, where n is the total
number of elements in the tree.

Rules That Every Red-Black Tree Follows:


1. Every node has a color either red or black.
2. The root of the tree is always black.
3. There are no two adjacent red nodes (A red node cannot have a red
parent or red child).
4. Every path from a node (including root) to any of its descendants’ NULL
nodes has the same number of black nodes.
5. All leaf (NULL) nodes are black nodes.

Page4 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

B-Tree
B-Tree is a self-balancing search tree. In most of the other self-balancing
search trees (like AVL and Red-Black Trees), it is assumed that everything is
in the main memory.
Properties of B-Tree:
 All leaves are at the same level.
 B-Tree is defined by the term minimum degree ‘t‘. The value of ‘t‘
depends upon disk block size.
 Every node except the root must contain at least t-1 keys. The root may
contain a minimum of 1 key.
 All nodes (including root) may contai n at most (2*t – 1) keys.
 The number of children of a node is equal to the number of keys in it
plus 1.
 All keys of a node are sorted in increasing order. The child between two
keys k1 and k2 contains all keys in the range from k1 and k2.
 B-Tree grows and shrinks from the root which is unlike Binary Search
Tree. Binary Search Trees grow downward and also shrink from
downward.

Page5 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

 Like other balanced Binary Search Trees, the time complexity to search,
insert and delete is O(log n).
 Insertion of a Node in B-Tree happens only at Leaf Node.

B+ Tree
B+ tree eliminates the drawback B-tree used for indexing by storing data
pointers only at the leaf nodes of the tree. Thus, the structure of leaf nodes
of a B+ tree is quite different from the structure of internal nodes of the B
tree.
Segment Tree
In computer science, a Segment Tree, also known as a statistic tree, is a
tree data structure used for storing information about intervals, or segments.
It allows querying which of the stored segments contain a given point. It is, in
principle, a static structure; that is, it’s a structure that cannot be modified
once it’s built. A similar data structure is the interval tree.

Binary Search Tree?

Binary Search Tree is a node-based binary tree data structure which has the following
properties:
 The left subtree of a node contains only nodes with keys lesser than the node’s key.
 The right subtree of a node contains only nodes with keys greater than the node’s key.
 The left and right subtree each must also be a binary search tree.

Applications of Binary Search tree:

Page6 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

 BSTs are used for indexing.


 It is also used to implement various searching algorithms.
 IT can be used to implement various data structures.
 BSTs can be used in decision support systems to store and quickly
retrieve data.
 BSTs can be used to store and quickly retrieve data in computer
simulations.
 BSTs can be used to implement fast autocomplete systems.

Real-time Application of Binary Search tree:


 BSTs are used for indexing in databases.
 It is used to implement searching algorithms.
 BSTs are used to implement Huffman coding algorithm.
 It is also used to implement dictionaries.
 Used for data caching.
 Used in Priority queues.
 Used in spell checkers.
Advantages of Binary Search Tree:
 BST is fast in insertion and deletion when balanced. It is fast with a
time complexity of O(log n).
 BST is also for fast searching, with a time complexity of O(log n) for
most operations.
Disadvantages of Binary Search Tree:
 The main disadvantage is that we should always implement a
balanced binary search tree. Otherwise the cost of operations may not
be logarithmic and degenerate into a linear search on an array.

Operations on Binary Search tree:


The four basic operations of BST:
1. Searching,
2. Insertion, and
3. Deletion
4. Traversals
Searching in a BST: Searching in BST involves the comparison of the key
values. If the key value is equal to root key then, search successful, if lesser
than root key then search the key in the left subtree and if the key is greater
than root key then search the key in the right subtree.
Searching in BST algorithm:-

Page7 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

 Check if tree is NULL, if the tree is not NULL then follow the following
steps.
 Compare the key to be searched with the root of the BST.
 If the key is lesser than the root then search in the left subtree.
 If the key is greater than the root then search in the right subtree.
 If the key is equal to root then, return and print search successful.
 Repeat step 3, 4 or 5 for the obtained subtree.
 2. Insertion in a BST:
 Insertion in BST involves the comparison of the key values. If the key
value is lesser than or equal to root key then go to left subtree, find an
empty space following to the search algorithm and insert the data and
if the key is greater than root key then go to right subtree, find an
empty space following to the search algorithm and insert the data.
3. Deletion in a BST:
Deletion in BST involves three cases:-
First, search the key to be deleted using searching algorithm and find the
node. Then, find the number of children of the node to be deleted.
 Case 1- If the node to be deleted is leaf node: If the node to be deleted
is a leaf node, then delete it.
 Case 2- If the node to be deleted has one child: If the node to be
deleted has one child then, delete the node and place the child of the
node at the position of the deleted node.
 Case 3- If the node to be deleted has two children: If the node to be
deleted has two children then, find the inorder successor or inorder
predecessor of the node according to the nearest capable value of the
node to be deleted. Delete the inorder successor or predecessor using
the above cases. Replace the node with the inorder successor or
predecessor.
4. Traversals in a BST:
There are 4 types of traversals of the Binary Search Tree.
Level Order Traversal: Each node of the tree is traversed level by level in
order of its appearance.
Pre-order Traversal: The nodes are traversed in the format of root and then
left subtree and then right subtree.
Inorder Traversal: The nodes are traversed in the format of left subtree and
then root and then right subtree.
Post Traversal: The nodes are traversed in the format of left subtree and
then right subtree and then root
Applications of Binary Search tree:
 BSTs are used for indexing.
 It is also used to implement various searching algorithms.

Page8 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

 IT can be used to implement various data structures.


 BSTs can be used in decision support systems to store and quickly
retrieve data.
 BSTs can be used to store and quickly retrieve data in computer
simulations.
 BSTs can be used to implement fast autocomplete systems.
A Tree Data Structure can be traversed in following ways:

1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
Inorder Traversal of Binary Tree

Inorder traversal is defined as a type of tree traversal technique which follows the
Left-Root-Right pattern, such that:
 The left subtree is traversed first
 Then the root node for that subtree is traversed
 Finally, the right subtree is traversed
Algorithm for Inorder Traversal of Binary Tree
The algorithm for Inorder traversal is shown as follows:
Inorder(root):
1. Follow step 2 to 4 until root != NULL
2. Inorder (root -> left)
3. Write root -> data
4. Inorder (root -> right)
5. End loop
How does Inorder Traversal of Binary Tree work?

Page9 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4


 If we perform an inorder traversal in this binary tree, then the traversal will be
as follows:
 Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from
2 to its left subtree root, i.e., 4. Now 4 has no left subtree, so it will be
visited. It also does not have any right subtree. So no more traversal
from 4

Page10 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4


 Step 2: As the left subtree of 2 is visited completely, now it read data
of node 2 before moving to its right subtree.

Page11 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4


Step 3: Now the right subtree of 2 will be traversed i.e., move to node 5.
For node 5 there is no left subtree, so it gets visited and after that, the

Page12 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

traversal comes back because there is no right subtree of node 5

 Step 4: As the left subtree of node 1 is, the root itself, i.e., node 1 will be
visited.

Page13 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

Step 5: Left subtree of node 1 and the node itself is visited. So now the
right subtree of 1 will be traversed i.e., move to node 3. As node 3 has no
left subtree so it gets visited.

Page14 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

Step 6: The left subtree of node 3 and the node itself is visited. So
traverse to the right subtree and visit node 6. Now the traversal ends as

Page15 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

all the nodes are traversed.

Preorder traversal is defined as a type of tree traversal that follows the


Root-Left-Right policy where:
 The root node of the subtree is visited first.
 Then the left subtree is traversed.
 At last, the right subtree is traversed

Postorder traversal is defined as a type of tree traversal which follows the
Left-Right-Root policy such that for each node:
 The left subtree is traversed first
 Then the right subtree is traversed
 Finally, the root node of the subtree is traversed
Algorithm for Postorder Traversal of Binary Tree:
The algorithm for postorder traversal is shown as follows:
Postorder(root):
1. Follow step 2 to 4 until root != NULL
2. Postorder (root -> left)

Page16 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

3. Postorder (root -> right)


4. Write root -> data
5. End loop
How does Postorder Traversal of Binary Tree
Work?

If we perform a Postorder traversal in this binary tree, then the traversal will
be as follows:
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its
left subtree root, i.e., 4. Now 4 has no subtree, so it will be visited.

Page17 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

Step 2: As the left subtree of 2 is visited completely, now it will traverse


the right subtree of 2 i.e., it will move to 5. As there is no subtree of 5, it

Page18 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

will be visited.

Step 3: Now both the left and right subtrees of node 2 are visited. So now
visit node 2 itself.

Page19 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

Step 4: As the left subtree of node 1 is traversed, it will now move to the
right subtree root, i.e., 3. Node 3 does not have any left subtree, so it will
traverse the right subtree i.e., 6. Node 6 has no subtree and so it is
visited.

Page20 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

Page21 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

Step 5: All the subtrees of node 3 are traversed. So now node 3 is visited.

Step 6: As all the subtrees of node 1 are traversed, now it is time for node
1 to be visited and the traversal ends after that as the whole tree is
traversed.

Page22 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com
VISION INSTITUTE OF TECHNOLOGY,ALIGARH
Subject: data structure

Unit-4

Page23 Faculty: VIKRAM SHARMA


Vikram1532018@gmail.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy