0% found this document useful (0 votes)
2 views50 pages

Unit 3 Notes

The document provides an overview of non-linear data structures, specifically trees, including definitions, terminologies, and types of trees such as binary trees, AVL trees, and B-trees. It covers tree traversals (in-order, pre-order, post-order), applications of trees in various fields, and operations on binary search trees including insertion and deletion. Additionally, it discusses the importance of AVL trees in maintaining balance for efficient operations in data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views50 pages

Unit 3 Notes

The document provides an overview of non-linear data structures, specifically trees, including definitions, terminologies, and types of trees such as binary trees, AVL trees, and B-trees. It covers tree traversals (in-order, pre-order, post-order), applications of trees in various fields, and operations on binary search trees including insertion and deletion. Additionally, it discusses the importance of AVL trees in maintaining balance for efficient operations in data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

DEPARTMENT OF CYBER SECURITY

NON-LINEAR DATA STRUCTURES – TREES


UNIT-III
Tree ADT – Tree Traversals– Binary Tree ADT – Expression trees-
Applications of Trees- Binary Search Tree ADT-AVL Tree –B-Tree–B+Tree–
Heap– Applications of Heap

Tree ADT
A tree is a widely used abstract data type (ADT) or data structure implementing this
ADT that simulates a hierarchical tree structure, with a root value and subtrees of
children, represented as a set of linked nodes.
A tree data structure can be defined recursively (locally) as a collection of nodes
(starting at a root node), where each node is a data structure consisting of a value,
together with a list of references to nodes (the "children"), with the constraints that no
reference is duplicated, and none points to the root.

Terminologies used in Trees


 Root - the top most node in a tree.
 Parent - the converse notion of child.
 Siblings - nodes with the same parent.
 Descendant - a node reachable by repeated proceeding from parent to child.
 Leaf - a node with no children.
 Internal node - a node with at least one child.
 Degree - number of sub trees of a node.
 Edge - connection between one node to another.
 Path - a sequence of nodes and edges connecting a node with a descendant.
 Level - The level of a node is defined by 1 + the number of connections between
the node and the root.

 Height - The height of a node is the length of the longest downward path
between the node and a leaf.
Tree traversal in a data structure is a type of graph traversal in the data structure that refers to the process of
visiting, verifying, and updating each node in a tree data structure just once. The order in which you examine the
nodes of the tree is used to classify these traversals.

There are three ways which we use to traverse a tree

 In-order Traversal
 Pre-order Traversal
 Post-order Traversal

In-Order Traversal ( Left-Root-Right)

1. Traverse left subtree

2. Traverse root node

3. Traverse right subtree

Pre-Order Traversal ( Root-Left-Right)

1. Traverse root node

2. Traverse left subtree

3. Traverse right subtree

Uses of pre-order traversal


To duplicate the tree, you need to use pre-order traversal. Pre-order traversal is used to obtain a
prefix expression from an expression tree.

Post-Order Traversal ( Left-Right-Root)

1. Traverse left subtree

2. Tereveser right subtree

3. Traverse root node

Uses of in-order traversal

You can delete the tree using post-order traversal. The postfix expression of tree data structure can
also be obtained using post-order traversal.

Binary Tree ADT

Binary Tree ADT

A binary tree is a tree in which no node can have more than two children. The
maximum degree of any node is two. This means the degree of a binary tree is either
zero or one or two.

In the above fig., the binary tree consists of a root and two sub trees T l & Tr. All nodes
to the left of the binary tree are referred as left subtrees and all nodes to the right of a
binary tree are referred to as right subtrees.
Implementation
A binary tree has at most two children; we can keep direct pointers to them. The
declaration of tree nodes is similar in structure to that for doubly linked lists, in that a
node is a structure consisting of the key information plus two pointers (left and right) to
other nodes.

Binary Tree node declaration

typedef struct tree_node *tree_ptr; struct tree_node

element_type element;

tree_ptr left; tree_ptr right; };

typedef tree_ptr TREE;

Pseudocode for insert algorithm


Types of Binary Tree

i. Strictly binary tree

Strictly binary tree is a binary tree where all the nodes will have either zero or two
children. It does not have one child in any node.

ii. Skew tree

A skew tree is a binary tree in which every node except the leaf has only one child
node. There are two types of skew tree, they are left skewed binary tree and right
skewed binary tree.

Left skewed binary tree

A left skew tree has node with only the left child. It is a binary tree with only left
subtrees.

Right skewed binary tree

A right skew tree has node with only the right child. It is a binary tree with only right
subtrees.
iii. Full binary tree or proper binary tree

A binary tree is a full binary tree if all leaves are at the same level and every non leaf
node has exactly two children and it should contain maximum possible number of
nodes in all levels. A full binary tree of height h has 2h+1 – 1 nodes.

iv. Complete binary tree

Every non leaf node has exactly two children but all leaves are not necessary at the
same level. A complete binary tree is one where all levels have the maximum number
of nodes except the last level. The last level elements should be filled from left to right.
v. Almost complete binary tree

An almost complete binary tree is a tree in which each node that has a right child also
has a left child. Having a left child does not require a node to have a right child.
Comparison between General Tree and Binary Tree

General Tree
1. General tree has any number of children.
2. Evaluating any expression is difficult in general trees.
Binary Tree
1. A binary tree has not more than two children
2. Evaluation of expression is easy in binary tree.
Application of trees

 Manipulation of arithmetic expression


 Symbol table construction
 Syntax Analysis
 Grammar
 Expression Tree
Expression trees
Definition: An expression tree is a binary tree in which the operands are attached as
leaf nodes and operators become the internal nodes.

For example -

From expression tree :

Inorder traversal: A+B*C(Left-Data-Right)

Preorder traversal: +A*BC(Data-Left-Right)

Postorder traversal: ABC*+ (Left-Right-Data)

If we traverse the above tree in inorder, preorder or postorder then we get infix, prefix
or postfix expressions respectively.

Key Point; Inorder traversal of expression tree fives infix expression. The preorder
traversal of expression tree gives prefix expression and post order traversal of
expression tree gives postfix expression.

Show the binary tree with arithmetic expression A/B * C * D + E. Give the
algorithm for inorder, preorder, postorder traversals and show the result of these
traversals.

Sol. :
Algorithm for inorder, preorder and postorder Traversal - Refer section 4.6.

Inorder Traversal A/B*C*D+E

Preorder Traversal + ** / ABCDE

Postorder Traversal AB/C*D*E+

Applications of Binary Tree


File Systems
Binary trees are used in file systems to organise and store files. Each node in the tree represents a
file or a directory, and the left child of a node represents a subdirectory or a file that is smaller than
the node, while the right child represents a subdirectory or a file that is larger than the node.

Search Engines
Binary trees are used in search engines to organise and index web pages. Each node in the tree
represents a web page, and the left child of a node represents a web page ranked lower than the node,
while the right child represents a web page ranked higher than the node.

Searching and Sorting Algorithms


In our daily life, organising and finding the data is very important. So here also, binary trees play the
role of searching and sorting. It is commonly used in searching and sorting algorithms,
including binary search, binary insertion sort, and quicksort.
You might have heard about binary search in this algorithm; it searches for a value in a sorted array
by repeatedly dividing the search length into half of the last one. Binary insertion sort uses a binary
search to find the insertion point. And the last quicksort is a popular sorting algorithm that uses a
binary tree to sort the data.
Expression Trees
Are you a maths lover? Because binary trees can also be used to represent maths expressions, such
as equations. These are known as expression trees. Expression trees are basically used in evaluating
and simplifying expressions and generating code.
Huffman Coding
Have you ever heard about Huffman coding? It is a data compression algorithm that uses binary
trees to encode characters. The binary tree used in Huffman coding is called a Huffman tree, and it is
a binary tree in which the nodes represent the characters to be encoded. The algorithm assigns
variable-length characters to each character, and the most frequent characters receive short codes.
Decision Trees
The decision tree is used in machine learning and data mining to model decisions and their
consequences. In the decision tree, the inner node represents the test on an attribute, while the leaf
node represents the result of the decision. Decision trees are used in various applications, such as
fraud detection, customer segmentation, and medical diagnosis.

Game AI
Two poles can be used in game artificial intelligence to model the different possibilities and
outcomes of a game. This allows the AI to make decisions based on possible outcomes and choose the
best option.
Trie
A trie is a specialised tree data structure used to store and retrieve strings. It is also known as a
prefix tree or a digital tree. In a trie, each node represents a prefix of a string, and the child nodes
represent the next character in the string. Tries are used in applications such as search engines, spell
checkers, and routers.

Fields Applications
File Systems organizing and storing files
Search Engines Indexing and ranking web pages
Sorting Sorting algorithms, such as binary insertion sort and quicksort
Math Representing math expressions for evaluating and symplifying
Data
Encoding characters using Huffman coding
Compression
Decision Trees Modeling decisions and consequences in machine learning
String Retrieval Storing and retrieving strings using trie data structures
Modeling possible moves and outcomes in game artificial
Game AI
intelligence
Network
Routing data through computer networks
Routing
Arithmetic Encoding characters with variable-length codes using a binary
Coding tree
Priority Queues Efficient access to the item with the highest priority
Image
Representing image region or shapes
Processing
Cryptography Generating and managing encryption and decryption keys
Binary Search Tree ADT
Binary search tree is a binary tree in which the nodes are arranged in specific order. That means the values at
left subtree are less than the root node value. Similarly the values at the right subtree are greater than the root
node.

The binary search tree is based on binary search algorithm.

Operations on Binary Search Tree

1. Insertion of a node in a binary

tree Algorithm:

1. Read the value for the node which is to be created and store it in a node called
New.

2. Initially if (root!=NULL) then root-New.

3. Again read the next value of node created in New.

4. If (New->value < root->value) then attach New node as a left child of root
otherwise attach New node as a right child of root.

5. Repeat steps 3 aand 4 for constructing required binary search tree

completely. void insert(node *root,node *New)

if(New->data <root->data)

if(root->left== NULL)
root->left=New;

else

insert(root->left, New);

if(New->data>root->data)

if(root->right==

NULL) root-

>right=New;

else

insert(root->right,New);

Simple Insertion Method: 10,6,20,8,15,22,9,21,24

Insert the Value of 23


2. Deletion of an element from the binary tree

For deletion of any node from binary search tree there are three cases which are
possible.

i. Deletion of leaf node.

ii. Deletion of a node having one child.

iii. Deletion of a node having two children.

Let us discuss the above cases one by one.

i. 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 tree, we want to delete the node having value 6 then we will set left
pointer of its parent node as NULL. That is left pointer of node having value 8 is set to
NULL.
ii. Deletion of a node having one child

To explain this kind of deletion, consider a tree as shown in the Fig. 4.9.6.

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. The inorder successor is always copied at the position of a node
being deleted.
void del(node *root,int key)

node *temp, *parent, *temp_succ;

temp=search(root,key,&parent);

/*deleting a node with two children*/

if(temp->left!= NULL&&temp->right!= NULL)

parent=temp;

temp_succ-temp->right;

while(temp_succ->left!= NULL)

parent=temp_succ;

temp_succ=temp_succ->left;

temp->data-temp_succ->data;

if(temp_succ == parent->left)

parent->left = NULL;

else

parent->right =NULL;

printf(" Now Deleted it!");

return;

/*deleting a node having only one child*/

/*The node to be deleted has left child*/


if(temp->left!= NULL &&temp->right== NULL)

if(parent->left==temp)

parent->left-temp->left;

else

parent->right-temp->left;

temp=NULL;

free(temp);

printf(" Now Deleted it!");

return;

/*The node to be deleted has right child*/

if(temp->left==NULL &&temp->right!=

NULL)

if(parent->left == temp)

parent->left-temp->right;

else

parent->right-temp->right;

temp=NULL;

free(temp);

printf(" Now Deleted it!");

return;

}
/*deleting a node which is having no child*/
if(temp->left==NULL &&temp->right== NULL)

if(parent->left==temp)

parent->left=NULL;

else

parent->right=NULL;

printf(" Now Deleted it!");

return;

Define binary search tree. Draw the binary search tree for the following input.
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Sol. Binary Search Tree (Refer section 4.9)


AVL Trees
AVL tree data structure is a self-balancing binary search tree (BST) that maintains balance to ensure
efficient operations. The full form of AVL tree is Adelson-Velsky and Landis. It is named after its
inventors.

The AVL tree in data structure ensures that the heights of subtrees differ by at most one.

This balance factor helps keep the tree height logarithmic, providing O(log n) time complexity for
search, insertion, and deletion operations.

Here, we will know the definition of AVL tree, properties, operations, rotations, traversal methods, code
implementation, applications, and much more.

AVL data structure, each node has a balance factor. This balance factor is the difference height between
the left and right subtrees. The balance factor can only be -1, 0, or 1. If the balance factor gets outside this
range, the tree does a rotation to fix itself. This keeps the tree short and wide, making it faster to search,
insert, and delete items.

The importance of AVL trees in data structures is that they ensure operations like search, insert, and
delete are always fast, even if we do them many times. This makes AVL trees very useful in
applications like databases and memory management, where we need to quickly find or update
information.

Properties of AVL Tree


The following are the key properties of AVL tree in data structure, explained in simple terms:

 Balanced Tree
An AVL tree is always balanced. This means that the heights of the left and right subtrees of any node
differ by at most one.

 Height-Balanced Property
The difference in height (also called the balance factor) between the left and right subtrees of any node is
-1, 0, or 1.

 Self-Balancing
An AVL tree automatically performs rotations to maintain balance after every insertion and deletion.
 Binary Search Tree Properties
An AVL tree follows the properties of a binary search tree (BST). This means that for any node, the
left subtree contains only nodes with values less than the node's value, and the right subtree contains only
nodes with values greater than the node's value.

 Rotations
To maintain balance, an AVL tree uses four types of rotations: left rotation, right rotation, left-right
rotation, and right-left rotation.

Balance Factor of AVL Tree


The AVL tree balance factor is a key concept used to maintain the tree's balanced property. In AVL
trees, the heights of the two child subtrees of any node differ by no more than one. This balance is
maintained through rotations during insertion and deletion operations.

The balancing factor (BF) of a node in an AVL tree is defined as the height difference between its left
and right subtrees.

For a node N, the balancing factor is calculated as:

BF(N) = height (left subtree) − height (right subtree)

Properties:
The balancing factor can be -1, 0, or 1 for a balanced AVL tree.

 BF = -1: The right subtree is one level higher than the left subtree.

 BF = 0: The left and right subtrees are of equal height.

 BF = 1: The left subtree is one level higher than the right subtree.

Balancing Factor Calculation:


 For each node, calculate the heights of its left and right subtrees.

 Subtract the height of the right subtree from the height of the left subtree to get the balancing
factor.

Example:
Consider the following AVL tree:

Balancing Factors:
 For node 10: Left height = 0, Right height = 0, BF = 0 - 0 = 0
 For node 20: Left height = 1, Right height = 0, BF = 1 - 0 = 1

 For node 50: Left height = 0, Right height = 0, BF = 0 - 0 = 0

 For node 40: Left height = 0, Right height = 1, BF = 0 - 1 = -1

 For node 30: Left height = 2, Right height = 2, BF = 2 - 2 = 0

This tree is balanced as all nodes have a balancing factor of -1, 0, or

1.

Operations on AVL Tree Data Structure


1. Insertion
Insertion in an AVL tree involves adding a new key while ensuring the tree remains balanced. After
inserting a new node, the balance factor of each node is checked, and rotations are performed if necessary
to maintain the AVL property.

Algorithm:
 Perform a standard BST insertion.

 Update the height of the current node.

 Calculate the balance factor of the current node.

 Perform rotations if the node becomes unbalanced.

Example:
Insert keys 10, 20, 30 into an AVL tree.

 Insert 10:

1. Let's understand with an example


1. Let the initial tree be:

Let the node to be inserted be:


2. Go to the appropriate leaf node to insert a newNode using the following

recursive steps. Compare newKey with rootKey of the current tree.

1. If newKey < rootKey, call the insertion algorithm on the left subtree of the
current node until the leaf node is reached.
2. Else if newKey > rootKey, call the insertion algorithm on the right subtree
of the current node until the leaf node is reached.
3. Else, return leafNode.

3. Compare leafKey obtained from the above steps with newKey:


1. If newKey < leafKey, make newNode as the leftChild of leafNode.
2. Else, make newNode as rightChild of leafNode.

4. Update balanceFactor of the nodes.


5. If the nodes are unbalanced, then rebalance the node.
6. If balanceFactor > 1, which means the height of the left subtree is greater than that of the
right subtree. So, do a right rotation or left-right rotation
7. If newNodeKey < leftChildKey do the right rotation.
8. Else, do left-right rotation.

The final tree is:


2. Deletion:A node is always deleted as a leaf node. After deleting a node, the balance
factors of the nodes get changed. To rebalance the balance factor, suitable rotations are
performed.

1. Locate nodeToBeDeleted (recursion is used to find nodeToBeDeleted in the code used


below).

There are three cases for deleting a node:

1. If nodeToBeDeleted is the leaf node (ie. does not have any child), then
remove nodeToBeDeleted.
2. If nodeToBeDeleted has one child, then substitute the contents of nodeToBeDeleted with
that of the child. Remove the child.
3. If nodeToBeDeleted have two children, find the in-order
successor w of nodeToBeDeleted (ie. node with a minimum value of key in the right
subtree).

o Substitute the contents of nodeToBeDeleted with that of w.


o Remove the leaf node w.

2. Update balanceFactor of the nodes.

3. Rebalance the tree if the balance factor of any of the nodes is not equal to -1, 0, or 1.
0. If balanceFactor of currentNode > 1,
1. If balanceFactor of leftChild >= 0, do right rotation.
2. Else do left-right rotation.
3. If balanceFactor of currentNode < -1,
4. If balanceFactor of rightChild <= 0, do left rotation.
5. Else do right-left rotation.
4. The final tree is:

3.

#include <stdio.h>
#include <stdlib.h>

typedef struct AVLNode


{ int key;
struct AVLNode *left; struct
AVLNode *right; int
height;
} AVLNode;

int max(int a, int b)


{ return (a > b) ? a :
b;
}

int height(AVLNode *N)


{ if (N == NULL)
return 0;
return N->height;
}

AVLNode* newNode(int key) {


AVLNode* node =
(AVLNode*)malloc(sizeof(AVLNode)); node->key = key;
node->left = NULL;
node->right =
NULL; node->height
= 1; return node;
}

AVLNode *rightRotate(AVLNode *y)


{ AVLNode *x = y->left;
AVLNode *T2 = x->right;

x->right = y;
y->left = T2;

y->height = max(height(y->left), height(y->right)) + 1;


x->height = max(height(x->left), height(x->right)) + 1;
return x;
}

AVLNode *leftRotate(AVLNode *x)


{ AVLNode *y = x->right;
AVLNode *T2 = y->left;

y->left = x;
x->right = T2;

x->height = max(height(x->left), height(x->right)) + 1;


y->height = max(height(y->left), height(y->right)) + 1;

return y;
}

int getBalance(AVLNode *N)


{ if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}

AVLNode* insert(AVLNode* node, int key) {


if (node == NULL)
return newNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else
return node;
node->height = 1 + max(height(node->left), height(node->right));

int balance = getBalance(node);

if (balance > 1 && key < node->left->key)


return rightRotate(node);

if (balance < -1 && key > node->right->key)


return leftRotate(node);

if (balance > 1 && key > node->left->key)


{ node->left = leftRotate(node->left);
return rightRotate(node);
}

if (balance < -1 && key < node->right->key)


{ node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

void preOrder(AVLNode *root)


{ if (root != NULL) {
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}

int main() {
AVLNode *root = NULL;

root = insert(root, 10);


root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);

printf("Pre-order traversal of the constructed AVL tree is \n");


preOrder(root);

return 0;
}

AVL Tree Rotations


AVL tree rotation is a fundamental operation used in self-balancing binary search trees,
specifically in AVL trees. Due to any operations like insertion or deletion, if any node of an
AVL tree becomes unbalanced, specific tree rotations are performed to restore the balance.
The tree rotations involve rearranging the tree structure without changing the order of
elements. The positions of the nodes of a subtree are interchanged. There are four types of
AVL rotations:

1. Left Rotation (LL Rotation)


In a left rotation, a node's right child becomes the new root, while the original node becomes
the left child of the new root. The new root's left child becomes the original node's right child.

2. Right Rotation (RR Rotation)


In a right rotation, a node's left child becomes the new root, while the original node becomes
the right child of the new root. The new root's right child becomes the original node's left
child.

3. Left-Right Rotation (LR Rotation)


An LR rotation is a combination of a left rotation followed by a right rotation. It is
performed when the left subtree of a node is unbalanced to the right, and the right subtree of
the left child of that node is unbalanced to the left.

4. Right-Left Rotation (RL Rotation)


An RL rotation is a combination of a right rotation followed by a left rotation. It is
performed when the right subtree of a node is unbalanced to the left, and the left subtree of the
right child of that node is unbalanced to the right.
B-Tree

B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree of order m can
have at most m-1 keys and m children. One of the main reason of using B tree is its capability to store
large number of keys in a single node and large key values by keeping the height of the tree relatively
small.A ‘B tree’ of order m contains all the properties of an M way tree. In addition, it contains the
following properties.

1. Every node in a B-Tree contains at most m children.


2. Every node in a B-Tree except the root node and the leaf node contain at least m/2
children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.

Basic Operations of B Trees

The operations supported in B trees are Insertion, deletion and searching with the time complexity
of O (log n) for every operation.

Insertion in B Trees

The insertion operation for a B Tree is done similar to the Binary Search Tree but the elements are
inserted into the same node until the maximum keys are reached. The insertion is done using the
following procedure.
Step 2 − The data is inserted into the tree using the binary search insertion and once the keys reach
the maximum number, the node is split into half and the median key becomes the internal node while
the left and right keys become its children.

Step 3 − All the leaf nodes must be on the same level.

The keys, 5, 3, 21, 9, 13 are all added into the node according to the binary search property but if we
add the key 22, it will violate the maximum key property. Hence, the node is split in half, the median
key is shifted to the parent node and the insertion is then continued.
Another hiccup occurs during the insertion of 11, so the node is split and median is shifted
to the parent.

While inserting 16, even if the node is split in two parts, the parent node also overflows as
it reached the maximum keys. Hence, the parent node is split first and the median key
becomes the root. Then, the leaf node is split in half the median of leaf node is shifted to its
parent.

The final B tree after inserting all the elements is achieved

Deletion

The deletion operation in a B tree is slightly different from the deletion operation of a
Binary Search Tree. The procedure to delete a node from a B tree is as follows −
Case 1 − If the key to be deleted is in a leaf node and the deletion does not violate
the minimum key property, just delete the node.

Case 2 − If the key to be deleted is in a leaf node but the deletion violates the minimum key
property, borrow a key from either its left sibling or right sibling. In case if both siblings
have exact minimum number of keys, merge the node in either of them.

Case 3 − If the key to be deleted is in an internal node, it is replaced by a key in either left
child or right child based on which child has more keys. But if both child nodes have
minimum number of keys, they’re merged together.

Case 4 − If the key to be deleted is in an internal node violating the minimum keys
property, and both its children and sibling have minimum number of keys, merge the
children. Then merge its sibling with its parent.
Pseudocode for deletion of B Tree

privateintremoveBiggestElement()
{
if (root has no child)
remove and return the last element else {
answer = subset[childCount-1].removeBiggestElement() if (subset[childCount-
1].dataCount< MINIMUM) fixShort (childCount-1)
return answer
}
}

B+ Tree
A B+ Tree is a type of data structure used in computer science to store and organize data. It is
a special kind of tree where all the values (data) are stored in the leaf nodes, and the internal
nodes are used only for indexing (like a table of contents). This structure helps in quickly
finding, inserting, and deleting data.

In a B Plus Tree, the leaf nodes are linked together like a linked list, which makes it easy to
find a range of values quickly.

Properties of B+ Tree
 Balanced Tree Structure: B+ Trees are balanced, meaning all leaf nodes are at the
same level.

 Leaf Nodes Store All Values: All actual data values are stored in the leaf nodes.

 Internal Nodes for Indexing: Internal nodes contain only keys and pointers to child
nodes.

 Linked List of Leaf Nodes: Leaf nodes are linked together in a linked list.

 Order of the Tree: The order (m) of a B+ Tree defines the maximum number of
children each internal node can have.

 Minimum Degree: The minimum degree (t) of a B+ Tree is defined as ⌈m/2⌉.

 Efficient Disk Read/Write: B+ Trees are designed to minimize disk I/O operations.

Basic Operations of B+ Trees


The operations supported in B+ trees are Insertion, deletion and searching with the time
complexity of O(log n) for every operation.

They are almost similar to the B tree operations as the base idea to store data in both data
structures is same. However, the difference occurs as the data is stored only in the leaf nodes of
a B+ trees, unlike B trees.

Insertion operation
The insertion to a B+ tree starts at a leaf node.

Step 1 − Calculate the maximum and minimum number of keys to be added onto the B+ tree
node.

Step 2 − Insert the elements one by one accordingly into a leaf node until it exceeds the
maximum key number.
Step 3 − The node is split into half where the left child consists of minimum number of keys
and the remaining keys are stored in the right child.

Step 4 − But if the internal node also exceeds the maximum key property, the node is split in
half where the left child consists of the minimum keys and remaining keys are stored in the right
child. However, the smallest number in the right child is made the parent.

Step 5 − If both the leaf node and internal node have the maximum keys, both of them are split
in the similar manner and the smallest key in the right child is added to the parent node.

Heap Tree

What is Heap Data Structure?


A heap is a tree where each parent node is greater than or equal to its child nodes in a max-
heap or less than or equal to its child nodes in a min-heap. This means the largest or smallest
value is always at the top (root) of the tree.

Types of Heap Data Structure (With Example)


There are two main types of heaps: Max-Heap and Min-Heap in data structure. Let's look at
each type with examples.

1. Max-Heap
In a max-heap, each parent node is greater than or equal to its child nodes. This ensures that
the largest value is always at the root of the tree.

Example of a Max-Heap:

 The root node (50) is greater than its children (30 and 20).

 Each parent node (30, 20) is greater than its children (15, 10 for 30 and 8, 5 for 20).

2. Min-Heap
In a min-heap, each parent node is less than or equal to its child nodes. This ensures that the
smallest value is always at the root of the tree.
Example of a Min-Heap:

 The root node (5) is smaller than its children (10 and 15).

 Each parent node (10, 15) is smaller than its children (30, 20 for 10 and 50, 40 for 15).

Properties of Heap Data Structure


1. Complete Binary Tree
A heap is always a complete binary tree, meaning:

 All levels are fully filled except possibly the last level.

 The last level is filled from left to right.

This property ensures that the tree remains balanced, which is crucial for maintaining the
efficiency of heap operations.

Example:

```
In this example, all levels are fully filled except the last level, which is filled from left to
right.

2. Heap Order Property


The heap must satisfy the heap order property, which differs for max-heaps and min-heaps:

 Max-Heap Order Property: In a max-heap, each parent node is greater than or equal to
its child nodes. This ensures that the largest element is always at the root.

 Min-Heap Order Property: In a min-heap, each parent node is less than or equal to its
child nodes. This ensures that the smallest element is always at the root.
Heapify Operations
Heapify operations are crucial for maintaining the heap property after insertion or deletion. The
two main heapify operations are Up-Heapify (Bubble-Up) and Down-Heapify (Bubble-Down).

1. Up-Heapify (Bubble-Up)
Up-Heapify is used after inserting a new element to restore the heap property by moving the
element up the tree.

Steps:
 Insert the new element at the end of the heap.

 Compare the inserted element with its parent.

 If the heap property is violated (the new element is greater than the parent in a max-heap
or smaller than the parent in a min-heap), swap the new element with its parent.

 Repeat this process until the heap property is restored or the element becomes the root.

Example (Max-Heap):
 Initial heap:

 Insert 40:

 Up-Heapify:
2. Down-Heapify (Bubble-Down)
Down-Heapify is used after deleting the root element to restore the heap property by moving
the new root element down the tree.

Steps:
 Replace the root element with the last element in the heap.

 Remove the last element from the heap.

 Compare the new root with its children.

 If the heap property is violated (the new root is smaller than a child in a max-heap or
larger than a child in a min-heap), swap the new root with the larger (in max-heap) or smaller (in
min-heap) child.

 Repeat this process until the heap property is restored or the element reaches a leaf node.

Example (Max-Heap):
 Initial heap:

 Extract-Max (50):
 Down-Heapify:

Basic Operations on Heap Tree


Heaps support several basic operations that are crucial for their functionality. Here, we will
cover three main operations: Insertion, Deletion (Extract-Min/Extract-Max), and Peek (Get-
Min/Get-Max).

1. Insertion
Insertion adds a new element to the heap while maintaining the heap properties.

Steps:
 Add the element to the end: Insert the new element at the end of the heap (the last
position in the array representation).

 Up-Heapify (Bubble-Up): Compare the inserted element with its parent. If the heap
property is violated, swap them. Repeat this process until the heap property is restored.

Example (Max-Heap):
 Initial heap:
 Insert 40:

 Up-Heapify:

2. Deletion (Extract-Min/Extract-Max)
Deletion removes the root element (maximum in a max-heap, minimum in a min-heap) and
restores the heap property.

Steps:
 Replace the root with the last element: Move the last element to the root position.

 Remove the last element: Remove the last element from the heap.

 Down-Heapify (Bubble-Down): Compare the new root with its children. If the heap
property is violated, swap it with the larger (in max-heap) or smaller (in min-heap) child. Repeat
until the heap property is restored.

Example (Max-Heap):
 Initial heap:
 Extract-Max (50):

 Down-Heapify:

3. Peek (Get-Min/Get-Max)
Peek retrieves the root element of the heap without removing it. This operation is efficient and
performed in constant time.

 Max-Heap: Returns the maximum element (root).

 Min-Heap: Returns the minimum element (root).

Example:
 For a max-heap:

Peek (Get-Max): The root element is 50.

 For a min-heap:
Peek (Get-Min): The root element is 5

Array Representation of Heaps


Heaps can be efficiently stored in arrays because they are complete binary trees. This
representation makes it easy to calculate the parent and child indices using simple arithmetic.

Storing Heaps in Arrays


In an array representation of a heap:

 The root node is stored at index zero.

 For any node at index i:

o Its left child is at index 2i + 1.

o Its right child is at index 2i + 2.

o Its parent is at index (i - 1) // 2 (if i is not zero).

Example:
Consider the following max-heap:

The array representation would be:

[50, 30, 20, 15, 10, 8, 5]

Applications of heap
The heap allows easy retrieval of these elements because they will always be present at the tree's
root node.
The accessing of elements from the root of a tree has a time complexity of O(1). Remember
that the rest of the array elements in your heap generally remain unsorted. Following are
some of the applications of the heap data structure.
1. Heap is used in the construction of priority queues. Using a priority queue, we can
insert, delete, identify the highest priority element, or insert and extract with priority,
among other things, in O(log N) time.
2. Although data structures such as the BST (Binary Search Tree), AVL (Adelson-Velsky
and Landis) trees, and the Red-Black tree can accomplish the same functionalities, they
are more complex than heaps.

A real-life example of the use of priority queues .This type of queue could be used
when customers who take a short time to serve are given priority instead of those who
arrive early. For example, customers with a small bill to pay may be given precedence at
a licensing center. The average waiting time for all clients in the queue can be reduced
due to this.
3. The priority queues implemented using heap data structure have more advanced uses
in graph algorithms. These include prims, Dijkstra's algorithm, Huffman coding, and
the BFS algorithm.

4. You can use the heap data structure to find the kth smallest / largest element in an array
quickly and effectively. You can go through this article to learn more about the problem.

5. Heap Sort is used in systems concerned with security and embedded systems such as the
Linux Kernel.

6. An efficient and straightforward sorting algorithm, the heap sort algorithm uses the
heap data structure. Heap sort is a robust in-place sorting algorithm whose time
complexity, in the worst case, is O(nlogn).

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