Professional Documents
Culture Documents
Data Structure & ALGORITHMS
Data Structure & ALGORITHMS
Data Structure & ALGORITHMS
Trees
• Trees, Properties of Trees, Binary trees, Binary Tree traversal,
Tree manipulation algorithms, Expression trees and their
usage, binary search trees, Heaps and their implementation,
Priority Queues
• Root
– In a tree data structure, the first node is called as Root Node.
– Every tree must have a root node.
– Root node is the origin of the tree data structure.
– In any tree, there must be only one root node.
• 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.
• Parent
– In a tree data structure, the node which is a predecessor of any
node is called as PARENT NODE.
– In simple words, the node which has a branch from it to any
other node is called a parent node.
– Parent node can also be defined as "The node which has child /
children".
• Child
– In a tree data structure, the node which is descendant of any
node is called as CHILD Node.
– In simple words, 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.
• Siblings
– In a tree data structure, nodes which belong to same Parent are
called as SIBLINGS.
– In simple words, the nodes with the same parent are called
Sibling nodes
• Leaf
– In a tree data structure, the node which does not have a child is
called as LEAF Node.
– In simple words, a leaf is a node with no child.
– In a tree data structure, the leaf nodes are also called
as External Nodes.
– In a tree, leaf node is also called as 'Terminal' node.
• Internal Nodes
– In a tree data structure, the node which has atleast one child is
called as INTERNAL Node.
– In a tree data structure, 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.
• Degree
– In a tree data structure, the total number of children of a node
is called as DEGREE of that Node.
– The highest degree of a node among all the nodes in a tree is
called as 'Degree of Tree'
• 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 simple words, 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).
• Height
– In a tree data structure, the total number of edges 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.
– In a tree, height of all leaf nodes is '0'.
• 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.
– In a tree, the total number of edges from root node to a leaf
node in the longest path is said to be Depth of the tree.
– In a tree, depth of the root node is '0'.
• 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.
• Sub Tree
– In a tree data structure, each child from a node forms a subtree
recursively.
– Every child node will form a subtree on its parent node.
– A complete binary tree is a binary tree in which all the levels are
completely filled except possibly the lowest one, which is filled
from the left.
– A complete binary tree is just like a full binary tree, but with two
major differences
• All the leaf elements must lean towards the left.
• The last leaf element might not have a right sibling i.e. a
complete binary tree doesn't have to be a full binary tree.
– Preorder (Node,Left,Right)
– Postorder (Left, Right, Node)
– Inorder(Left, Node, Right)
PreOrder : (NLR)
Ans – 1 7 2 6 3 8 5 9 4
InOrder: (LNR)
Ans – 2 7 3 6 8 1 5 9 4
PostOrder: (LRN)
Ans - 2 3 8 6 7 4 9 5 1
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
1. Pickup last key of post order sequence and create node of each.
2. Create left subtree of root node recursively by selecting keys which are
preceding the root node in Inorder.
3. Create right subtree of root node recursively by selecting keys which are
following the root node in Inorder
Inorder: 1 2 3 4 5 7 8
Postorder: 1 3 4 2 7 8 5
1. Pickup first key of pre order sequence and create node of each.
2. Create left subtree of root node recursively by selecting keys which are
preceding the root node in Inorder.
3. Create right subtree of root node recursively by selecting keys which are
following the root node in Inorder
Inorder: 1 2 3 4 5 7 8
Preorder: 5 2 1 4 3 8 7
Inorder: 1 2 3 4 5 6 7 8
Preorder: 5 3 1 2 4 6 8 7
Construct all possible Binary Search tree by inserting following keys in any order.
10, 20,30
SN Operation Description
Min Heap
• The value of the parent node should be less than or equal to
either of its children.
• Suppose we want to create the max heap tree. To create the max heap
tree, we need to consider the following two cases:
➢ First, we have to insert the element in such a way that the property of the
complete binary tree must be maintained.
➢ Secondly, the value of the parent node should be greater than the either
of its child.
• Where node A is the node whose balance Factor is other than -1, 0, 1.
• The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations.
1. Insert H, I, J
3. Insert E
On inserting E,
4. Insert C, F, D
5. Insert G
LR = RR + LL rotation.
6. Insert K
7. Insert L
On inserting the L tree is still balanced as the Balance Factor of each node is now either, -
1, 0, +1. Hence the tree is a Balanced AVL tree
• Deleting a node from an AVL tree is similar to that in a binary search tree. Deletion
may disturb the balance factor of an AVL tree and therefore the tree needs to be
rebalanced in order to maintain the AVLness.
• For this purpose, we need to perform rotations.
• The two types of rotations are L rotation and R rotation.
• If the node which is to be deleted is present in the left sub-tree of the critical node,
then L rotation needs to be applied else if, the node which is to be deleted is
present in the right sub-tree of the critical node, the R rotation will be applied.
• Let us consider that, A is the critical node and B is the root node of its left sub-tree.
• If node X, present in the right sub-tree of A, is to be deleted, then there can be
three different situations:
If the node B has 0 balance factor, and the balance factor of node A disturbed upon
deleting the node X, then the tree will be rebalanced by rotating tree using R0 rotation.
The critical node A is moved to its right and the node B becomes the root of the tree
with T1 as its left sub-tree. The sub-trees T2 and T3 becomes the left and right sub-tree
of the node A. the process involved in R0 rotation is shown in the following image.
Example:
Delete the node 30 from the AVL tree shown in the following image.
Solution
In this case, the node B has balance factor 0, therefore the tree will be rotated by using R0
rotation as shown in the following image. The node B(10) becomes the root, while the
node A is moved to its right. The right child of node B will now become the left child of
node A.
Example
Delete Node 55 from the AVL tree shown in the following image.
Deleting 55 from the AVL Tree disturbs the balance factor of the node 50 i.e. node A
which becomes the critical node. This is the condition of R1 rotation in which, the node A
will be moved to its right (shown in the image below). The right of B is now become the
left of A (i.e. 45).
The process involved in the solution is shown in the following image.
Example
Delete the node 60 from the AVL tree shown in the following image.
Solution:
in this case, node B has balance factor -1. Deleting the node
60, disturbs the balance factor of the node 50 therefore, it
needs to be R-1 rotated. The node C i.e. 45 becomes the
root of the tree with the node B(40) and A(50) as its left and
right child.
Definition of a B-tree
Case 1. When leaf element is not full then insert element into it.
Constructing a B Tree
Example
key :- P,A,Q,S,B,X,C,L,Z,Y,T,G,J,I,D,H,R,V,E,U,F,R.
Order = 5
1. Insert P
2. Insert P
Constructing a B Tree
Example
key :- P,A,Q,S,B,X,C,L,Z,Y,T,G,J,I,D,H,R,V,E,U,F,R,O.
1. Insert Q
2. Insert S
3. Insert B
4. Insert X
5. Insert C
6. Insert L
7. Insert Z
8. Insert Y
9. Insert T
10. Insert G
11 Insert J
12 Insert I
13. Insert D
14. Insert H
15. Insert R
16. Insert V
17. Insert E
18. Insert U
19. Insert F
20. Insert O
Constructing a B Tree
Example
key :- 1,12,8,2,25,6,14,28,17,7,52,16,48,68,3,26,29,53,55,45,67.
Order = 5
Procedure for adding key in b-tree
Step3. Same process applied until root node full. if root node full then
splitting process applied.
Deletion in B-Tree
For deletion in B-Tree we wish to remove from a leaf. There are three
possible case for deletion in b tree.
Let k be the key to be deleted, x the node containing the key. Then the cases
are:
Case-I
If the key is already in a leaf node, and removing it doesn’t cause that leaf
node to have too few keys, then simply remove the key to be deleted. key
k is in node x and x is a leaf, simply delete k from x.
Delete 6
6 deleted
Case-II
If key k is in node x and x is an internal node, there are three cases to
consider:
Case-II-a
If the child y that precedes k in node x has at least t keys (more than
the minimum), then find the predecessor key k' in the subtree rooted
at y. Recursively delete k' and replace k with k' in x
Case-II-b
Symmetrically, if the child z that follows k in node x has at least t keys, find
the successor k' and delete and replace as before. Note that finding k' and
deleting it can be performed in a single downward pass.
13 deleted
Case-II-c
Otherwise, if both y and z have only t−1 (minimum number) keys, merge k
and all of z into y, so that both k and the pointer to z are removed from x. y
now contains 2t − 1 keys, and subsequently k is deleted.
7 deleted
Case-III
If key k is not present in an internal node x, determine the root of the appropriate subtree that
must contain k. If the root has only t − 1 keys, execute either of the following two cases to
ensure that we descend to a node containing at least t keys. Finally, recurse to the appropriate
child of x.
Case-III-a
If the root has only t−1 keys but has a sibling with t keys, give the root an extra key by moving a
key from x to the root, moving a key from the roots immediate left or right sibling up into x,
and moving the appropriate child from the sibling to x.
Case-III-b
If the root and all of its siblings have t−1 keys, merge the root with one sibling. This involves
moving a key down from x into the new merged node to become the median key for that
node.
After deleting 2
• The number of edges from the root to the node is called __________
of the tree.
a) Height
b) Depth
c) Length
d) Width
• What is a full binary tree?
a) Each node has exactly zero or two children
b) Each node has exactly two children
c) All the leaves are at the same level
d) Each node has exactly one or two children
• What is the average case time complexity for finding the height of the binary tree?
a) h = O(loglogn)
b) h = O(nlogn)
c) h = O(n)
d) h = O(log n)