Chapter 6 TREE
Chapter 6 TREE
Tree
Data Structure
DATA STRUCTURE AND ALGORITHM 1
CONTENTS
Definition of tree,
Basic terminologies,
Types of trees:-
n-ary tree, Binary tree, BST, AVL tree, full BT, complete BT ,Balanced BT .
Heap data Structure:- definition, creation, insertion, update, deletion, print etc. Examples of Expression trees
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
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.
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 maximum depth of any node within the tree)
Descendant: - A node reachable by repeated proceeding from parent to child.(child, grandchild, grand-
grandchild, etc)
Ancestor: -A node reachable by repeated proceeding from child to parent.(parent, grandparent, grand-
grandparent, etc.)
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.
Degree of 8 = 2
Degree of 3 = 3
Degree of 4 = 1
Degree of the Tree = 3
Types of Tree
General tree, Binary Tree, Binary search tree, n-ary tree, AVL 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 most basic form 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.
A, B, C, D, E,
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
maximum 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 tree is a binary tree in which all internal nodes have two children and all leaves
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 may print their values too.
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all
the values it contains.
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 first, then the left subtree and finally the right
subtree.
A→B→D→E→C→F→G
In this traversal method, the root node is visited last, hence the name. First we 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
• AVL Tree
• Heap Data Structure
• Heap Operations
• Expression tree
DATA STRUCTURE AND ALGORITHM 28
OPERATIONS ON BINARY SEARCH TREE
1. Inserting a key: - Insert a new node with a given key in its correct place in BST
2. Searching a key: - Search a node in the BST with the given key
5. Finding maximum values: - Go to the right most leaf starting from root
6. Finding minimum values: - Go to the left most leaf starting from the root
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 new key is less than the root’s key, search for the empty location in the left subtree and
insert the new key.
Otherwise, search for the empty location in the right subtree and insert the data.
Remember no matter the value of the new key to be inserted, it will be inserted as leaf node of the
tree. (unless the new key is already in the BST).
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
-1 9 56 -1 9 56 -1 9 56
56 41 56 & 41<56
-1 9 go left
DATA STRUCTURE AND ALGORITHM 32
8 41 73
BST- DELETION OPERATION
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.
Case 1: The node to be deleted is a leaf (has no children).
Simply remove the leaf node from the tree by removing
the link with its parent. 23
23
56 -1 9 56
-1 9
DATA STRUCTURE AND ALGORITHM 33
X 8 41 73 41 73
CONT..
23 23 23
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
5 34
5 41
Right sub-tree
-1 9 30 56
-1 9 30 56
8 32 41 73
8 32 73
in-order successor
23 30
Delete 23
5 34 5 34
in-order
-1 9 30 successor 56 -1 9 32 56
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 right
child of its grand parent
5 Left sub-tree 34 5 32
-1 9 30 56 -1 9 30 56
8 32 41 73 8 73
41
in-order predecessor
23 9
Delete 23
5 34 5 34
-1 9 30 56 -1 8 30 56
8 in-order 32 41 73 32 41 73
predecessor
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 memory allocation is not possible (such as BASIC,
fortran ), array representation is the only means to store a tree.
Disadvantages:
1. Other than full binary trees, majority of the array entries may be empty.
2. It allows only static representation.
3. 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 AND ALGORITHM 44
ADVANTAGES AND DISADVANTAGES
Advantages:
1. The drawbacks of the sequential representation are overcome in this representation. In addition, for
unbalanced trees, the memory is not wasted.
2. Insertion and deletion operations are more efficient in this representation.
3. It is useful for dynamic data.
Disadvantages:
1. In this representation, there is no direct access to any node.
2. 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.
3. The programming languages not supporting dynamic memory management would not be useful for this representation.
Most BST operations takes O(h) time where h is the height of BST.
The cost of this operations may become O(n) for a skewed binary tree.
If we make sure that height of the tree remains O(Log n) after every insertion and deletion, then
The height of AVL 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 hR are the heights of
the left and right subtrees of T, respectively.
If a node is inserted or deleted from a balanced tree, then it may become unbalanced.
So to rebalance it, the position of some nodes can be changed in proper sequence.
Depends on the scenario whether a rotation should be performed towards left or right.
To balance itself, an AVL tree may perform the following four kinds of rotations
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree,
then we perform a single left rotation −
23 23 23
R
34
34 34 34
R
23 56
56 56
Balanced AVL
Unbalanced AVL
Node 23 has become unbalanced Left Rotation Balanced AVL
as 56 is inserted in the right subtree
of 23's right subtree.
DATA STRUCTURE AND ALGORITHM 51
RIGHT ROTATION
If a tree becomes unbalanced, when a node is inserted into the left subtree of the left subtree,
then we perform a single right rotation −
56 56 56
L
34
34 34 34
L
23 56
23 23
Balanced AVL
Unbalanced AVL
Node 23 has become unbalanced Left Rotation Balanced AVL
as 56 is inserted in the left subtree
of 23's left subtree.
DATA STRUCTURE AND ALGORITHM 52
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 AVL becomes unbalanced we
perform left-right rotation.
We first perform the left rotation on the We shall now right-rotate the tree,
Unbalanced AVL left subtree of C. This makes A, the left making B the new root node of this
Node C has become unbalanced as subtree of B. subtree. C now becomes the right
node B is inserted in the right subtree subtree of its own left subtree.
of Node C's left subtree. Node C is still unbalanced, however now, it 53
is because of the left-subtree of the left- The tree is now balanced.
DATA STRUCTURE AND ALGORITHM
subtree.
RIGHT-LEFT ROTATION
We first perform the right rotation along C A left rotation is performed by making
Unbalanced AVL node, making C the right subtree of its own B the new root node of the subtree. A
Node A has become unbalanced as left subtree B. Now, B becomes the right becomes the left subtree of its right
node B is inserted in the left subtree subtree of A. subtree B.
of Node A's Right subtree. 54
Node A is still unbalanced because of the The tree is now balanced.
DATA STRUCTURE AND ALGORITHM
right subtree of its right subtree and
requires a left rotation..
Create AVL Tree for the following unsorted list of keys.
21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7
EXERCISE
21 21 21 26 26
26 26 21 30 21 30
30 9
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 AVL 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
57
reheapUP 61
Original Heap After Inserting 36 reheapUP
36 Exchanged with 23 36 Exchanged with 32
DATA STRUCTURE AND ALGORITHM
ReheapDown
Deleting a node in the heap is always done at the top of the heap (root). And then the last node
in the heap replace the root position.
Sometimes the new 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 down by repeatedly exchanging
parent-child keys until it reaches the correct location in the heap.
reheapDown 62
Original Heap After deleting 53 reheapDown
43 Exchanged with 21 41 Exchanged with 21
DATA STRUCTURE AND ALGORITHM Note: 43 > 32 Note: 41 > 31
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, we need to search the first empty leaf in the array. We find it immediately
after the last node in the tree.
To insert a node, we move the new data to the first empty leaf and perform reheapUp.
63
Heap after insertion of 87 Heap after reheapUp Operation
If we traverse this tree in pre-order, we visit the nodes in the order of: /+xAB-CD-CE, and this is
a prefix form of the infix expression.
On the other hand, if we 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.