DSA Unit3 Tree
DSA Unit3 Tree
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
● 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
● 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
• 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
• 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.
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
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)
• 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
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
• 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
• 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.
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.
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
•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