0% found this document useful (0 votes)
11 views251 pages

DSA Unit3 Tree

The document provides an overview of tree data structures, highlighting their hierarchical organization and various types including general trees, binary trees, and their specific characteristics. It explains key terminology such as root, edge, parent, child, and different traversal methods like preorder, inorder, and postorder. Additionally, it discusses representations of binary trees and their properties, including complete and extended binary trees.

Uploaded by

Vidushi Kochhar
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)
11 views251 pages

DSA Unit3 Tree

The document provides an overview of tree data structures, highlighting their hierarchical organization and various types including general trees, binary trees, and their specific characteristics. It explains key terminology such as root, edge, parent, child, and different traversal methods like preorder, inorder, and postorder. Additionally, it discusses representations of binary trees and their properties, including complete and extended binary trees.

Uploaded by

Vidushi Kochhar
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/ 251

TREES

linked list, stack, and queue are linear


data structures that store data
sequentially.

In order to perform any operation in a


linear data structure, the time complexity
increases with the increase in the data
size. But, it is not acceptable in today's
computational world.

Different tree data structures allow


quicker and easier access to the data as it
is a non-linear data structure.
Trees
A tree is recursively defined as a set of one or more nodes where one node is
designated as the root of the tree and all the remaining nodes can be partitioned
into non-empty sets each of which is a sub-tree of the root.

Types
General Tree: General trees are data structures that store elements hierarchically
Forest : disjoint union of trees.
Binary Tree :In a binary tree, the topmost element is called the root node, and each node
has 0, 1, or at the most 2 children.
Binary Search Tree :
Expression Tree
Tree Definition

• Tree is a non-linear data structure which


organizes data in hierarchical structure and this is
a recursive definition.
Tree terminology...
● 1.Root:
− The first node is called as Root Node.
− Every tree must have root node, there must be only one root node.
− Root node doesn't have any parent.
2. Edge
● In a tree data structure, the connecting link between any two
nodes is called as EDGE. In a tree with 'N' number of nodes
there will be a maximum of 'N-1' number of edges.
3. Parent

● In a tree data structure, the node which is predecessor of any node


is called as PARENT NODE. In simple words, the node which has
branch from it to any other node is called as parent node. Parent
node can also be defined as "The node which has child / children".
4. Child
● The node which has a link from its parent node is called as child node.
● In a tree, any parent node can have any number of child nodes.
● In a tree, all the nodes except root are child nodes.
5. Siblings

● The nodes with same parent are called as Sibling nodes.


6. Leaf Node

● The node which does not have a child is called as LEAF Node.
● The leaf nodes are also called as External Nodes or 'Terminal' node.
7. Internal Nodes

● An internal node is a node with atleast one child.


● Nodes other than leaf nodes are called as Internal Nodes.
● The root node is also said to be Internal Node if the tree has more
than one node. Internal nodes are also called as 'Non-Terminal'
nodes.
8. Degree

● In a tree data structure, the total number of


children of a node is called as DEGREE of that Node.
9. Level

● In a tree data structure, the root node is said to be at Level 0 and the
children of root node are at Level 1 and the children of the nodes which
are at Level 1 will be at Level 2 and so on...
● In a tree each step from top to bottom is called as a Level and the Level
count starts with '0' and incremented by one at each level (Step).
10. Height

● the total number of egdes from leaf node to a particular node in the
longest path is called as HEIGHT of that Node.
● In a tree, height of the root node is said to be height of the tree.
11. Depth

● In a tree data structure, the total number of egdes from root node to a
particular node is called as DEPTH of that Node.
12. Path

● In a tree data structure, the sequence of Nodes and Edges from one node
to another node is called as PATH between that two Nodes.
● Length of a Path is total number of nodes in that path. In below example
the path A - B - E - J has length 4.
13. Sub Tree

● Every child node will form a subtree on its parent node.


General Tree

• A general tree is a data structure in that each node can have infinite number of children .
• In general tree, root has in-degree 0 and maximum out-degree n.
• Height of a general tree is the length of longest path from root to the leaf of tree. Height(T) =
{max(height(child1) , height(child2) , … height(child-n) ) +1}


Binary tree
• A binary tree is a tree-type non-linear data structure with a
maximum of two children for each parent.
• Every node in a binary tree has a left and right reference along
with the data element.
• The node at the top of the hierarchy of a tree is called the root
node.
• A binary tree is a tree data structure whose all nodes have either
zero, one, or at most two children nodes.
• These two children are generally referred to as left and right
children respectively.
• The top-most node is known as the root node, while the nodes with
no children are known as leaf nodes.
Representation of Binary Tree
1. Array Representation
2. Linked List Representation.
Representation of Binary Tree

Struct node
{

int data;
struct node * left,right;
};
Array Representation
1. To represent a tree in one dimensional array nodes are marked
sequentially from left to right start with root node.
2. First array location can be used to store no of nodes in a tree.
Linked Representation
1. This type of representation is more efficient as compared to array.
2. Left and right are pointer type fields left holds address of left child and right holds
address of right child.
3. Struct node
{

int data;
struct node * left,*right;
};
Binary Tree Types

1. Extended Binary Tree


2. Complete Binary Tree
3. Full Binary Tree
4. Skewed Binary Tree
5. Strictly Binary Tree
6. Expression Binary tree
Complete Binary Tree

A complete binary tree is a tree in which


• 1.All leaf nodes are at n or n-1 level
• 2.Levels are filled from left to right
Extended Binary Tree

An extended binary tree is a transformation of any binary tree into a complete


binary tree. This transformation consists of replacing every null subtree of the
original tree with “special nodes”or “failure nodes” .The nodes from the
original tree are then internal nodes, while the “special nodes” are external
nodes.
Full Binary Tree

A full binary tree (sometimes proper binary tree or 2-tree) is


a tree in which every node other than the leaves has two children
or no childeren. Every level is completely filled up.
No of nodes= 2h+1 -1
Skewed Binary Tree

A binary tree is said to be Skewed Binary Tree if every node in the


tree contains either only left or only right sub tree. If the node
contains only left sub tree then it is called left-skewed binary
tree and if the tree contains only right sub tree then it is
called right-skewed binary tree.
Strictly Binary Tree
A nod will have either two children or no child at all.
Expression Binary tree

• Expression trees are a special kind of binary tree used to evaluate


certain expressions.
• Two common types of expressions that a binary expression tree
can represent are algebraic and boolean.
• These trees can represent expressions that contain both unary
and binary operators.
• The leaves of a binary expression tree are operands, such as
constants or variable names, and the other nodes contain
operators.
• Expression tree are used in most compilers.
Expression Tree

Operand : represented as leaf node


Operator : represented as internal node.
Subtree: If Root is not NULL then TL and TR are subtree.
Edge: A line drawn from node N to its successor
Path: a sequence of consecutive edges.
Branch: a path ending in a leaf
Depth The depth of a node N is given as the length of the path from the root R to the
node N. The depth of the root node is zero.
Height of a tree It is the total number of nodes on the path from the root node to the
deepest node in the tree. A tree with only a root node has a height of 1
A binary tree of height h has at least h nodes and at most 2h – 1 nodes.
The height of a binary tree with n nodes is at least log2(n+1) and at most n.
Similar tree: Tree having structure or shape
Copies: if they are similar and they have same content at corresponding nodes
Calaton number= C(2n,n)/(n+1)= 2n!/(n+1)!n!
Gives how many different binary tree are constructed using n nodes.
Binary
Tree
In a binary tree, the topmost element is called the root node, and each node has
0, 1, or at the most 2 children.
Properties of
Complete Binary Tree
• level r of T can have at most 2r nodes
• if K is a parent node, then its left child can be calculated as 2 × K and its right child can
be calculated as 2 × K + 1
• The parent of the node K can be calculated as | K/2 |
• The height of a tree T having exactly n nodes is given as: H = | log2 (n + 1) |
Extended Binary Tree
A binary tree T is said to be an extended
binary tree (or a 2-tree) if each node in the
tree has either no child or exactly two
children.
In an extended binary tree, nodes having two
children are called internal nodes and nodes
having no children are called external nodes.
The internal nodes are represented using
circles and the external nodes are represented
using squares.
• Maximum number of nodes in a binary tree of height ‘h’ is 2h – 1.
Here height of a tree is maximum number of nodes on root to leaf path.
Height of a tree with single node is considered as 0.
This result can be derived from point 2 above. A tree has maximum
nodes if all levels have maximum nodes. So maximumh-1
number of nodes
in a binary tree of height h is 1 + 2 + 4 + .. + 2 .
In some books, height of the rooth+1
is considered as 0. In this convention,
the above formula becomes 2 – 1

In a Binary Tree with N nodes, minimum possible height or minimum
number of levels is ? Log2(N+1) ?
This can be directly derived from point 2 above. If we consider the
convention where height of a leaf node is considered as 0, then above
formula for minimum possible height becomes Log2(N+1) ? – 1
Representation of Binary Trees in the Memo
Linked representation of binary trees
Sequential Representation/ Array Representation
Traversing a Binary Tree
In-order Post-order Traversal (LRN)
Pre-order Traversal (NLR)
Traversal(LNR)
What• Aisbinary
a Binary Tree
tree is a a collection of nodes that consists of the root and o
subsets to the root points, which are called the left and right subtrees

• Every parent node on a binary tree can have up to two child nodes (ro
the two subtrees); any more children and it becomes a general tree.

• A node that has no children is called a leaf node.


A Few Terms Regarding Binary Trees

B C

D E F

A is the root
B and C are A’s children
A is B’s parent G
B & C are the root of two subtrees
D, E, and G are leaves
This is NOT A Binary Tree

B C

D E F G H

This is a general tree because C has three child nodes


This is NOT A Binary Tree

B C

D E F G

This is a graph H
because A, B, E, H, F and C
form a circuit
Tree Traversal
• There are three common ways to traverse a tree:
• Preorder: Visit the root, traverse the left subtree (preorder)
and then traverse the right subtree (preorder)

• Postorder: Traverse the left subtree (postorder), traverse the


right subtree (postorder) and then visit the root.

• Inorder: Traverse the left subtree (in order), visit the root and
the traverse the right subtree (in order).
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDECFG
Postorder: DEBGFCA G
In Order: DBEAFGC
Tree Traversals: An Example
A

B C

D E F

Preorder: A
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: AB
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABD
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDE
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDEC
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDECF
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDECFG
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Postorder:
G
Tree Traversals: An Example
A

B C

D E F

Postorder:
G
Tree Traversals: An Example
A

B C

D E F

Postorder: D
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DE
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEB
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEB
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEB
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEBG
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEBGF
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEBGFC
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEBGFCA
G
Tree Traversals: An Example
A

B C

D E F

G
In Order:
Tree Traversals: An Example
A

B C

D E F

G
In Order:
Tree Traversals: An Example
A

B C

D E F

G
In Order: D
Tree Traversals: An Example
A

B C

D E F

G
In Order: DB
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBE
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEA
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEA
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEAF
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEAFG
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEAFGC
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDECFG
Postorder: DEBGFCA G
In Order: DBEAFGC
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
H I In Order: DHFIBEACG
Tree Traversals: Another Example

B C

D E
G

F
Preorder: A

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: AB

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABD

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDF

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFH

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHI

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIE

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIEC

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIECG

H I
Tree Traversals: Another Example

B C

D E
G

Postorder:
H I
Tree Traversals: Another Example

B C

D E
G

Postorder:
H I
Tree Traversals: Another Example

B C

D E
G

Postorder:
H I
Tree Traversals: Another Example

B C

D E
G

Postorder:
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: H
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HI
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIF
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFD
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDE
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEB
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEB
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEBG
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEBGC
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEBGCA
H I
Tree Traversals: Another Example

B C

D E
G

H I In Order:
Tree Traversals: Another Example

B C

D E
G

H I In Order:
Tree Traversals: Another Example

B C

D E
G

H I In Order: D
Tree Traversals: Another Example

B C

D E
G

H I In Order: D
Tree Traversals: Another Example

B C

D E
G

H I In Order: DH
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHF
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFI
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIB
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIBE
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIBEA
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIBEAC
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIBEACG
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
H I In Order: DHFIBEACG
A Few Terms Regarding Binary Trees

B C

D E F

A is the root
B and C are A’s children
A is B’s parent G
B & C are the root of two subtrees
D, E, and G are leaves
This is NOT A Binary Tree

B C

D E F G H

This is a general tree because C has three child nodes


This is NOT A Binary Tree

B C

D E F G

This is a graph H
because A, B, E, H, F and C
form a circuit
Tree Traversal
• There are three common ways to traverse a tree:
• Preorder: Visit the root, traverse the left subtree (preorder) and then traverse
right subtree (preorder)
• Postorder: Traverse the left subtree (postorder), traverse the right subtree
(postorder) and then visit the root.
• Inorder: Traverse the left subtree (in order), visit the root and the traverse the
subtree (in order).
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDECFG
Postorder: DEBGFCA G
In Order: DBEAFGC
Tree Traversals: An Example
A

B C

D E F

Preorder: A
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: AB
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABD
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDE
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDEC
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDECF
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDECFG
(visit each node as your reach it) G
Tree Traversals: An Example
A

B C

D E F

Postorder:
G
Tree Traversals: An Example
A

B C

D E F

Postorder:
G
Tree Traversals: An Example
A

B C

D E F

Postorder: D
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DE
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEB
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEB
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEB
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEBG
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEBGF
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEBGFC
G
Tree Traversals: An Example
A

B C

D E F

Postorder: DEBGFCA
G
Tree Traversals: An Example
A

B C

D E F

G
In Order:
Tree Traversals: An Example
A

B C

D E F

G
In Order:
Tree Traversals: An Example
A

B C

D E F

G
In Order: D
Tree Traversals: An Example
A

B C

D E F

G
In Order: DB
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBE
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEA
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEA
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEAF
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEAFG
Tree Traversals: An Example
A

B C

D E F

G
In Order: DBEAFGC
Tree Traversals: An Example
A

B C

D E F

Preorder: ABDECFG
Postorder: DEBGFCA G
In Order: DBEAFGC
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
H I In Order: DHFIBEACG
Tree Traversals: Another Example

B C

D E
G

F
Preorder: A

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: AB

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABD

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDF

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFH

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHI

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIE

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIEC

H I
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIECG

H I
Tree Traversals: Another Example

B C

D E
G

Postorder:
H I
Tree Traversals: Another Example

B C

D E
G

Postorder:
H I
Tree Traversals: Another Example

B C

D E
G

Postorder:
H I
Tree Traversals: Another Example

B C

D E
G

Postorder:
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: H
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HI
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIF
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFD
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDE
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEB
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEB
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEBG
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEBGC
H I
Tree Traversals: Another Example

B C

D E
G

Postorder: HIFDEBGCA
H I
Tree Traversals: Another Example

B C

D E
G

H I In Order:
Tree Traversals: Another Example

B C

D E
G

H I In Order:
Tree Traversals: Another Example

B C

D E
G

H I In Order: D
Tree Traversals: Another Example

B C

D E
G

H I In Order: D
Tree Traversals: Another Example

B C

D E
G

H I In Order: DH
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHF
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFI
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIB
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIBE
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIBEA
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIBEAC
Tree Traversals: Another Example

B C

D E
G

H I In Order: DHFIBEACG
Tree Traversals: Another Example

B C

D E
G

F
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
H I In Order: DHFIBEACG
Basic• Implementation
We can implement a binary of a Binary
in essentially Tree
the same way as a linked list, exce
there are two nodes that comes next:
• public class Node {
• private int data;
• private Node left;
• private Node right;
Constructing Binary tree from Traversal

Step 1 Use the pre-order/ post-order sequence to determine the root node of the
tree.
Step 2 Elements on the left side of the root node in the in-order traversal sequence
form the left sub-tree of the root node. Similarly, elements on the right side of the
root node in the in-order traversal sequence form the right sub-tree of the root node

Step 3 Recursively select each element from pre-order/post-order traversal


sequence and create its left and right sub-trees from the in-order traversal
sequence.
Question

• In-order traversal:
24,17,32,18,51,11,26,39,43
Pre-order traversal:
11,32,24,17,51,18,43,26,39
Binary Search Tree
A binary search tree is a binary tree with the following properties:
1. The left sub-tree of a node N contains values that are less than N’s value.
2. The right sub-tree of a node N contains values that are greater than N’s value.
3.Both the left and the right binary search trees also satisfy these properties
and, thus, are binary search trees.
Operations
Insertion It takes O(log n) time to execute in the average case and O(n) time in the
worst case.
Searching can be done in O(log2n) time to execute in the average case and O(n) time in
the worst case.
Searching in BST
Deletion from BST
Case 1: Deleting a Node that has No Children

Case 2: Deleting a Node with One Child : replace the node with its child.
Case 3: Deleting a Node with Two Children : replace the node’s
value with its in-order predecessor (largest value in the left sub-tree)
or in-order successor (smallest value in the right sub-tree). The in-
order predecessor or the successor can then be deleted using any of
the above cases.
Skewed
Tree
AVL Tree
An AVL tree defined as 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.

The difference between the heights of the left subtree and the right subtree for any node is
known as the balance factor of the node.

The AVL tree is named after its inventors, Georgy Adelson-Velsky and Evgenii Landis
Complexity

Algorithm Average case Worst case


Space o(n) o(n)
Search o(log n) o(log n)
Insert o(log n) o(log n)
Delete o(log n) o(log n)
Balancing factor
AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor
is other than -1, 0, and 1.
There are basically four types of rotations which are as
follows:
1. L L rotation: Inserted node is in the left subtree of left subtree of A
2. R R rotation : Inserted node is in the right subtree of right subtree of A
3. L R rotation : Inserted node is in the right subtree of left subtree of A
4. R L rotation : Inserted node is in the left subtree of right subtree of A

• The first two rotations LL and RR are single rotations and the next two rotations LR and RL are
double rotations.

• For a tree to be unbalanced, minimum height must be at least 2, Let us understand each rotation
• What is a complete binary tree?
• A complete binary tree is a binary tree in which all the levels except the last
level, i.e., leaf node should be completely filled, and all the nodes should be
left-justified.

• Let's understand through an example.

• Heap Data Structure


• In the above figure, we can observe that all the internal nodes are
completely filled except the leaf node; therefore, we can say that the above
tree is a complete binary tree.
Binary Heap
• A binary heap is a complete binary tree in which every node satisfies the heap
property which states that:
• If B is a child of A, then key(A) ≥ key(B)
• This implies that elements at every node will be either greater than or equal to the element at
its left and right child. Thus, the root node has the highest key value in the heap. Such a heap
is commonly known as a max-heap.
• Alternatively, elements at every node will be either less than or equal to the element at its
left and right child. Thus, the root has the lowest key value. Such a heap is called a
min-heap
Insertion in Heap
•Consider a max heap H with n elements. Inserting a new value
into the heap is done
•in the following two steps:
1.Add the new value at the bottom of H in such a way that H is still
a complete binary tree but not necessarily a heap.

2.Let the new value rise to its appropriate place in H so that H now
becomes a heap as well.
Insertion
Insertion
insertion of a single value may take O(log n) time, to build a
heap of n elements, the algorithm will execute in O(n log n) time.
Deleting an Element from a Binary Heap
Consider a max heap H having n elements. An element is
always deleted from the root of the heap. So, deleting
an element from the heap is done in the following three
steps:

1.Replace the root node’s value with the last node’s value so
that H is still a complete binary tree but not necessarily a
heap.

2. Delete the last node.

3. Sink down the new root node’s value so that H satisfies


the heap property. In this step, interchange the root
node’s value with its child node’s value (whichever is
largest among
its children).
• The m-way search trees are multi-way trees which are generalised versions
of binary trees where each node contains multiple elements.
• In an m-Way tree of order m, each node contains a maximum of m –
1 elements and m children.
• m represents the maximum number of children for a node in the tree.
• Binary trees are also called 2-way search trees as they have a maximum of
two children.
• The m-way search tree's primary use is to optimize searching and attain the
best time.

Multi-way Search Tree (m-way search tree)
•M-way search tree which has M – 1 values per node and M child nodes
•In such a tree, M is called the degree of the tree. In other words, every
internal node of an M-way search tree consists of pointers to M sub-trees
and contains M – 1 keys, where M
•> 2.
• To make the processing of m-way trees easier some type of constraint
or order will be imposed on the keys within each node, resulting in a
multiway search tree of order m (or an m-way search tree).
• By definition an m-way search tree is a m-way tree in which following
condition should be satisfied −
• Each node is associated with m children and m-1 key fields
• The keys in each node are arranged in ascending order.
• The keys in the first j children are less than the j-th key.
• The keys in the last m-j children are higher than the j-th key.
•In the structure shown, P , P , P , ..., P are pointers to the node’s sub-trees
0 1 2 n

and K , K , K , ..., K are the key values of the node. All the key values are
0 1 2 n–1

stored in ascending order. That is, K < K for 0 <= i <= n–2.
i i+1

• Properties of an M-way search tree.


• All the key values in the sub-tree pointed
by Pi are less than Ki, where 0 <= i <= n–
1.
• All the key values in the sub-tree pointed
by Pi are greater than Ki–1, where 0 <= i
<= n–1.
B-Trees
A B tree is a specialized M-way tree developed by Rudolf Bayer and Ed McCreight in 1970 that is widely
used for disk access.
A B tree of order m can have a maximum of m–1 keys and m pointers to its sub-trees.
A B tree of order m (the maximum number of children that each node can have) is a tree with all the
properties of an M-way search tree. In addition it has the following properties:

1. Every node in the B tree has at most (maximum) m children.


2.Every node in the B tree except the root node and leaf nodes has at least (minimum) m/2 children.
This condition helps to keep the tree bushy so that the path from the root node to the leaf is very
short, even in a tree that stores a lot of data.
3. The root node has at least two children if it is not a terminal (leaf) node.
4. All leaf nodes are at the same level.
B trees
• Need of a B Tree in a Data Structure
• The need for B-tree emerged as the demand for faster access to physical
storage media such as a hard disk grew. With a bigger capacity, backup
storage devices were slower. There was a demand for data structures that
reduced the number of disc accesses.
• A binary search tree, an AVL tree, a red-black tree, and other data
structures can only store one key in each node. When you need to store a
significant number of keys, the height of such trees grows quite vast, and
the time it takes to access them grows.
• B-tree, on the other hand, can store multiple keys inside a single node and
has several child nodes. It considerably reduces the height, allowing for
speedier disk access.
Properties of B-Tree:
• All leaves are at the same level.
• 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.
• 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.
Operations on B-Tree
Searching for an element in a B tree is similar to that in binary search trees.
Insertion

Construct a B-Tree of Order 3 by inserting numbers from 1 to 10.


1,2,3,4,5,6,7,8,9,10
Deletion Operation
• Before going through the steps below, one must know these facts
about a B tree of degree m.If degree is 3 then,

• A node can have a maximum of m children. (i.e. 3)


• A node can contain a maximum of m - 1 keys. (i.e. 2)
• A node should have a minimum of ⌈m/2⌉ children. (i.e. 2)
• A node (except root node) should contain a minimum of ⌈m/2⌉ - 1
keys. (i.e. 1)
• Refer this for examples below:
Deletion from
B-Tree
Case I: Delete a leaf node
A)The deletion of the key does not violate the property of the
minimum number of keys a node should hold.

b)The deletion of the key violates the property of the minimum


number of keys a node should hold. In this case, we borrow a key
from its immediate neighboring sibling node in the order of left to
right.

c) If both the immediate sibling nodes already have a minimum


number of keys then, merge the target node with either the left or the
right sibling along with the parent key of respective node.
2.If the target key is at the internal node:
If the target key is at an internal node, we further study the given data to check if any of
the following cases exist:

•Case 1: If the left child has more than the minimum number of keys, the target key in the
internal node is replaced by its inorder predecessor ,i.e, the largest element of the left
child node.

•Case 2: If the right child has more than the minimum number of keys, the target key in
the internal node is replaced by it’s inorder successor ,i.e, the smallest element of the
right child node.

•Case 3: If either child has exactly a minimum number of keys then, merge the left and the right
children.
Case II : Delete internal node
1.The internal node, which is deleted, is replaced by an in-order predecessor if the left child has more than the minimum number of keys.
2.The internal node, which is deleted, is replaced by an in-order successor if the right child has more than the minimum number of keys.
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.
B+ Tree
• A B+ tree is an advanced form of a self-balancing tree in which all the values are present in the leaf level.
• An important concept to be understood before learning B+ tree is multilevel indexing. In multilevel indexing,
the index of indices is created as in figure below. It makes accessing the data easier and faster.
• The B+ trees are extensions of B trees designed to make the insertion,
deletion and searching operations more efficient.
• The properties of B+ trees are similar to the properties of B trees, except
that the B trees can store keys and records in all internal nodes and leaf
nodes while B+ trees store records in leaf nodes and keys in internal
nodes.
• One profound property of the B+ tree is that all the leaf nodes are
connected to each other in a single linked list format and a data pointer is
available to point to the data present in disk file. This helps fetch the
records in equal numbers of disk access.
• Since the size of main memory is limited, B+ trees act as the data storage
for the records that couldn’t be stored in the main memory.
• For this, the internal nodes are stored in the main memory and the leaf
nodes are stored in the secondary memory storage.
• Properties of a B+ Tree
• All leaves are at the same level.
• The root has at least two children.
• Each node except root can have a maximum of m children and at
least m/2 children.
• Each node can contain a maximum of m - 1 keys and a minimum
of ⌈m/2⌉ - 1 keys.
B+ tree
B + Tree is a variation of the B-tree data structure. In a B + tree, data pointers are stored only at the leaf nodes of the
tree.
n a B+ tree structure of a leaf node differs from the structure of internal nodes.
The leaf nodes have an entry for every value of the search field, along with a data pointer to the record (or to the block
that contains this record).
The leaf nodes of the B+ tree are linked together to provide ordered access to the search field to the records. Internal
nodes of a B+ tree are used to guide the search. Some search field values from the leaf nodes are repeated in the
internal nodes of the B+ tree.

Difference between B-tree and B+tree: The data pointers are present only at the leaf
nodes on a B+ tree whereas the data pointers are present in the internal, leaf or root nodes
on a B-tree.
The leaves are not connected with each other on a B-tree whereas they are connected on a
B+ tree.
Operations on a B+ tree are faster than on a B-tree.
Searching on a B+tree
Search 45
Compare 45 with root . 45>25 move to right

45 found
Compare 45 with 35 move to 45.
45>=45 move to right
Insertion
Create a 3 way B+ tree 5,15,25,35,45
Deletion
Case I
The key to be deleted is present only at the leaf node not in the indexes (or internal nodes).
There are two cases for it:
a)There is more than the minimum number of keys in the node. Simply delete the
key.

Delete 40
b)There is an exact minimum number of keys in the node. Delete the key and borrow a key
from the immediate sibling. Add the median key of the sibling node to the parent.

Delete 5
Case II
The key to be deleted is present in the internal nodes as well. Then we have to remove them from the internal nodes as
well. There are the following cases for this situation.
a)If there is more than the minimum number of keys in the node, simply delete the key from th leaf node and delete
the key from the internal node as wel Fill the empty space in the internal node with the in-order successor.
b)If there is an exact minimum number of keys in the node, then delete the key and borrow a key
from its immediate sibling (through the parent). Fill the empty space created in the index (internal
node) with the borrowed key.
c)This case is similar to Case II(1) but here, empty space is generated above the
immediate parent node. After deleting the key, merge the empty space with its sibling.
Fill the empty space in the grandparent node with the in-order successor.
Case III In this case, the height of the tree gets shrinked.
Deleting 55 from the tree below leads to this condition.
B* tree
• Each node contains minimum of 2/3 nodes rather than half
• In B* tree , when a node overflows instead of being spilt immediately, the data are
redistributed among the nodes siblings, delaying the creation of new node.
END

You might also like