Unit 3 Notes
Unit 3 Notes
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.
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.
In-order Traversal
Pre-order Traversal
Post-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.
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.
element_type element;
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.
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.
A left skew tree has node with only the left child. It is a binary tree with only left
subtrees.
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.
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
For example -
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.
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.
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.
tree Algorithm:
1. Read the value for the node which is to be created and store it in a node called
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.
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);
For deletion of any node from binary search tree there are three cases which are
possible.
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)
temp=search(root,key,&parent);
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;
return;
if(parent->left==temp)
parent->left-temp->left;
else
parent->right-temp->left;
temp=NULL;
free(temp);
return;
if(temp->left==NULL &&temp->right!=
NULL)
if(parent->left == temp)
parent->left-temp->right;
else
parent->right-temp->right;
temp=NULL;
free(temp);
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;
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
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.
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.
The balancing factor (BF) of a node in an AVL tree is defined as the height difference between its left
and right subtrees.
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 = 1: The left subtree is one level higher than the right subtree.
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
1.
Algorithm:
Perform a standard BST insertion.
Example:
Insert keys 10, 20, 30 into an AVL tree.
Insert 10:
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.
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).
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>
x->right = y;
y->left = T2;
y->left = x;
x->right = T2;
return y;
}
return node;
}
int main() {
AVLNode *root = NULL;
return 0;
}
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.
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.
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.
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.
Efficient Disk Read/Write: B+ Trees are designed to minimize disk I/O operations.
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
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).
All levels are fully filled except possibly the last level.
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.
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.
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.
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:
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.
Example:
For a max-heap:
For a min-heap:
Peek (Get-Min): The root element is 5
Example:
Consider the following max-heap:
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).