DSA Chapter 6 - Tree
DSA Chapter 6 - Tree
D A T A STRUCTURES A N D ALGORITHMS
Tree
Data
DATA STRUCTURE A N D ALGORITHM
Structure 1
CONTENTS
◾ Tree Data
Structure:
◾ D efinition of tree,
◾ Basic
terminologies,
◾ Types of trees:-
◾ n-ary tree,
Binary tree, BST,
A V L tree, full
BT, complete B T
,Balanced B T .
◾ Basic operations on
tree,
DATA STRUCTURE A N D ALGORITHM 2
◾ Tree traversal
m e thods:
6.1. T R E E D A T A S T R U C T U R E
◾ A tree is a collection of elements called nodes connected by directed (or undirected) edges
(that contains no cycles). Each node contains some value.
◾ A tree can be empty with no nodes or a tree is a structure consisting of one node
called the root and zero or one or more subtrees.
◾ Every node (exclude a root) is connected by a directed edge from exactly one other node; A
direction
is: parent -> children
DATA STRUCTURE A N D ALGORITHM 5
DATA STRUCTURE A N D ALGORITHM 6
6.2. BASIC TERMINOLOGIES IN TREE
◾ Root: - The node at the top of the tree is called root. There is only one root per tree and
one path from the root node to any node.(Root – node with no parent)
◾ Child: - The node below a given node connected by its edge downward is called its child
node (left child or right child).
◾ Edge: - The connection between one node and another.
◾ Parent: - Any node except the root node has one edge upward to a node called parent.
◾ Leaves:- Nodes with no children are called leaves, or external nodes.
◾ Internal nodes:- Nodes which are not leaves, are called internal nodes. Internal nodes have
at least one child.
10
CONT..
◾ Depth of a node:- The depth of a node is the number of edges from the root to the node.
◾ The height of a node:- is the number of edges from the node to the deepest leaf.
◾ The height of a tree:- is a height of a root. Or (the m a x i m u m depth of any node within the tree)
5, 6 and 7 are
siblings
◾ Length of this path: is the number of edges connecting the nodes in the path.
◾ Level: -Level of a node represents the generation of a node. If the root node is at level 0, then its
next
child node is at level 1, its grandchild is at level 2, and so on.
◾ Subtree − Subtree represents the descendants of a node.
Degree of 8 = 2
D egree of 3 = 3
D egree of 4 = 1
D egree of the Tree =
3
Types of Tree
◾ General tree, Binary Tree, Binary search tree, n-ary tree, A V L tree or height balanced binary
tree,
Red-Black tree…..etc.
General Tree or Tree
◾ Every node can have any number of sub-trees, there is no maximum. Different number is
possible of each node.
Binary Tree
◾ The m ost basic for m of tree structure.
◾ A binary tree is a tree where each node is restricted to having at most two children, and each
child is designated as either a left child or right child.
DATA STRUCTURE A N D ALGORITHM 13
Which one of the following is Binary Tree?
A, B, C, D, E,
◾ Example; find the m a x i m u m and m i n i m u m possible number of nodes for a binary tree with Height = 2
?
◾ M a x i m u m height 2+1 = 3
◾ M i n i m u m height 𝟐(𝒉+𝟏) − 𝟏 = 7
◾ A binary tree is a full binary tree if it contains the maximum possible number of nodes in
all levels.
◾ In a full binary tree, each node has two children or no child at all (for leaves node ).
◾ The total number of nodes in a full binary tree of height h is 2h+1 - 1 (considering the root
at level 0).
◾ A binary tree is said to be a complete binary tree if all its levels except the last level
have the m a x i m u m number of possible nodes, and all the nodes of the last level appear as
far left as possible.
◾ In a complete binary tree, all the leaf nodes are at the last and the second last level, and
the levels are filled from left to right.
◾ A perfect binary is a binary tree in which all internal have two children and all leaves
tree nodes
have the same depth or same level.
◾ BST is a tree in which all the nodes follow the below-mentioned properties
−
◾ The left sub-tree of a node has a key less than or equal to its parent node's key.
◾ The right sub-tree of a node has a key greater than or equal to its parent node's
key.
◾ Traversal is a process to visit all the nodes of a tree and m a y print their values too.
◾ In-order Traversal
◾ Pre-order Traversal
◾ Post-order Traversal
◾ Generally, w e traverse a tree to search or locate a given item or key in the tree or to print
all
the values it contains.
D ATA STRUCTURES & 12/19/2016 24
ALGO RITHM
IN-ORD ER TRAVERSAL
◾ In this traversal method, the left subtree is visited first, then the root and later the right sub-
tree.
◾ If a binary search tree is traversed in-order, the output will produce sorted key values in an
ascending order.
D →B→E→A→F→C →
G
◾ In this traversal method, the root node is visited last, hence the name. First w e traverse the
left subtree, then the right subtree and finally the root node.
D →E→B→F→G→C →
A
• BST Operations
• BST Implementation
• Using array
• Using linked list
• A V L Tree
• Heap Data Structure
• Heap Operations
• Expression tree
DATA STRUCTURE A N D ALGORITHM 28
OPERATIONS O N BINARY SEARCH TREE
1. Inserting a key: - Insert a n e w node with a given key in its correct place in
BST
2. Searching a key: - Search a node in the BST w ith the given key
5. Finding maximum values: - G o to the right most leaf starting from root
6. Finding minimum values: - G o to the left most leaf starting from the root
DATA STRUCTURE A N D ALGORITHM 29
BST- INSERTION OPERATION
◾ If the tree is empty, the very first insertion creates the tree. Then the first key becomes the
root.
◾ First locate its proper location. Start searching from the root node, then
◾ If the n e w key is less than the root’s key, search for the empty location in the left subtree
and
insert the ne w key.
◾ Otherwise, search for the empty location in the right subtree and insert the data.
◾ Remember no matter the value of the n e w key to be inserted, it will be inserted as leaf node of
the
DATA STRUCTURE A N D ALGORITHM 30
tree. (unless the n e w key is already in the BST).
Example: Insert the following key’s in BST. 23, 5, 34, 56, 9, 41, -1, 8, 73
23 23 23
23 23
23
5 5 34 5 34 5 34 5 34
56 9 56 9 56
23 23 23 41
5 34 5 34 5 34
- 9 56 - 9 56 - 9 56
1 1 1
DATA STRUCTURE A N D ALGORITHM
41 8 41 8 41 73 31
BST- SEARCHING OPERATION
56 41 56 & 41<56
- 9 go left
1
DATA STRUCTURE A N D ALGORITHM 32
8 4 73
1
BST- DELETION O PERATION
◾ Complexity of deletion depends on the node
◾ There are three cases to consider:
◾ Case 1: The node to be deleted is a leaf (has no children).
◾ Case 2: The node to be deleted has one child.
◾ Case 3: The node to be deleted has two children.
56 -1 9 56
-1 9
DATA STRUCTURE A N D ALGORITHM 33
X 8 41 73 41 73
CONT..
5 34 5 34 5 34
-1 9 56 -1 56 -1 8 56
8 41 73 8 41 73 41 73
◾ The question is which subtrees should the parent of the deleted node be linked to, what
should
be done with the other subtrees, and where should the remaining subtrees be linked.
◾ Method 1:
◾ Select the smallest node called in-order successor and Replace with the deleting node.
◾ If the node (in-order successor node) to replace the deleting node has right child, make this node a
left
child of its grand parent
DATA STRUCTURE A N D ALGORITHM 35
23 Delete 34 23
5 34
5 41
Right sub-
tree
- 9 30 56
- 9 30 56
1
1
8 73
32 41 8 32 73
in-order
23 successor 30
Delete 23
5 34 5 34
in-order
- 9 30 successo 56 - 9 32 56
1 1
DATA STRUCTURE A N D ALGORITHM r 36
8 32 41 73 8 41 73
CONT..
◾ Method 2:
◾ Select the greatest node called in-order predecessor and Replace with the deleting node.
◾ If the node (in-order predecessor node) to replace the deleting node has left child, make this node a
5 Left sub- 34 5 32
tree
- 9 30 56 - 9 30 56
1 1
8 8 73
32 41 41
73
23
in-order predecessor 9
Delete 23
5 34 5 34
-1 9 30 56 - 8 30 56
1
DATA STRUCTURE A N D ALGORITHM 38
8 in-order 32 41 73 32 41 73
predecesso
BST- OTHER OPERATIONS
Advantages:
1. Any node can be accessed from any other node by calculating the index.
2. The data is stored without any pointers to its successor or predecessor.
3. In the programming languages, where dynamic m e m o r y allocation is not possible (such as
BASIC, fortran ), array representation is the only means to store a tree.
Disadvantages:
4. Other than full binary trees, majority of the array entries m a y be empty.
5. It allows only static representation.
6. Insertion and deletion operation requires lots of data movement.
Here, 0 (zero) stored at Lchild or Rchild fields represents that the respective child is
not present. Alternately NULL can be used instead of zero.
DATA STRUCTURE A N D ALGORITHM 44
ADVA N TAGES A N D DISADVA N TAGES
Advantages:
1. The drawbacks of the sequential representation are overcome in this representation. In addition, for
unbalanced trees, the m e m o r y is not wasted.
2. Insertion and deletion operations are more efficient in this representation.
3. It is useful for dynamic data.
Disadvantages:
4. In this representation, there is no direct access to any node.
5. As compared to sequential representation, the memory needed per node is more. This is due to two link fields
(left child and right child for binary trees) in the node.
6. The programming languages not supporting dynamic memory management would not be useful for this
representation.
DATA STRUCTURE A N D ALGORITHM 45
AVL TREES
◾ Most BST operations takes O(h) time where h is the height of BST.
◾ The cost of this operations m a y become O(n) for a skewed binary tree.
◾ If w e make sure that height of the tree remains O(Log n) after every insertion and deletion,
then w e can guarantee an upper bound of O(Log n) for all these operations.
◾ The height of A V L tree is always O(Log n) where n is the number of nodes in the tree.
◾ The balance factor of a node T, BF(T), in a binary tree is hL - hR, where hL and h R are the
◾ So to rebalance it, the position of some nodes can be changed in proper sequence.
23 23 23
R
34
34 34 34
R
23 56
56 56
Balanced A V L
Unbalanced A V L
Node 23 has become Left Balanced A V L
unbalanced R o tation
as 56 is inserted in
DATA STRUCTURE A N D ALGORITHM the right subtree of 23's right 51
subtree.
RIGHT ROTATION
◾ If a tree becomes unbalanced, w he n a node is inserted into the left subtree of the left
subtree, then w e perform a single right rotation −
56 56 56
L
34
34 34 34
L
23 56
23 23
Balanced A V L
Unbalanced A V L
Node 23 has become Left Balanced A V L
unbalanced R o tation
as 56 is inserted in
DATA STRUCTURE A N D ALGORITHM the left subtree of 23's left 52
subtree.
LEFT-RIGHT ROTATION
◾ Double rotations are slightly complex version of already explained versions of rotations.
◾ A left-right rotation is a combination of left rotation followed by right rotation.
◾ If a node inserted into the right subtree of the left subtree and the A V L becomes unbalanced
we
perform left-right rotation.
R
We first perform the left rotation on W e shall n o w right-rotate the
Unbalanced A V L the left subtree of C .This makes A, tree, making B the n e w root
Node C has become unbalanced as the left subtree of B. node of this subtree. C n o w
node B is inserted in the right becomes the right subtree of its
subtree N ode C is still unbalanced, however o w n left subtree. 53
of N ode C 's left subtree. now, it is because of the left-subtree The tree is now
DATA STRUCTURE A N D ALGORITHM
of the left- subtree. balanced.
RIGHT-LEFT ROTATION
◾ A right-left rotation is a combination of right rotation followed by left rotation.
◾ If a node inserted into the left subtree of the right subtree and the A V L becomes unbalanced
we perform left-right rotation.
of N ode A's Right subtree. N ode A is still unbalanced because of The tree is now
DATA STRUCTURE A N D ALGORITHM
the right subtree of its right subtree balanced.
and requires a left rotation..
Create A V L Tree for the following unsorted list of
keys.
◾ 21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3,
7
26 26
21 21 21
21 30 21 30
26
30 9
26
A. B. C. D.
26 26 26 26 21
21 30 9 30 9 30 21 30 26
9
9 4 21 4 21 9 4 30
14
4 14 4 14 55
E. F
Create A V L Tree for the following unsorted list of
keys.
◾ 21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3,
7
21
9 26
4 14 30
Continue inserting … … … … … … . .
57
◾ In addition, every path from root to leaf should be sorted in ascending order.
◾ In addition, every path from root to leaf should be sorted in descending order.
◾ In general, whenever the term ‘heap’ is used by itself, it refers to a max-heap
reheapU P 61
O riginal After Inserting reheapUP
36 36 Exchanged with 36 Exchanged with
Heap
DATA STRUCTURE A N D ALGORITHM 23 32
ReheapDown
◾ Deleting a node in the heap is always done at the top of the heap (root). A nd then the last
node in the heap replace the root position.
◾ Sometimes the n e w root becomes out of order.
◾ The reheapDown operation repairs the structure so that it is a heap by lifting the root element
down in the tree until that element reaches a proper position in the tree.
◾ ReheapDown repairs a broken heap by lifting the last element d o w n by repeatedly
exchanging parent-child keys until it reaches the correct location in the heap.
reheapDown 62
O riginal After deleting reheapDown
53 43 Exchanged with 41 Exchanged with
Heap
DATA STRUCTURE A N D ALGORITHM 21 21
INSERT
◾ A node can be inserted in a heap which has already been built, if there is an empty
location in the array.
◾ To insert a node, w e need to search the first empty leaf in the array. W e find it
immediately
after the last node in the tree.
◾ To insert a node, w e m o v e the n e w data to the first empty leaf and perform reheapUp.
63
Heap after insertion of 87 Heap after reheapUp Operation
◾ To reconstruct the heap, w e m ov e the data in the last heap node to the root and perform
reheapDown.
◾ If w e traverse this tree in pre-order, w e visit the nodes in the order of: /+xAB-CD-CE, and
this is a prefix for m of the infix expression.
◾ O n the other hand, if w e traverse the tree in post-order, the nodes are visited in the
following
order: ABxCD-E-/, which is a postfix equivalent of the infix notation.