Data Structure Unit 5 Trees
Data Structure Unit 5 Trees
Trees
What are trees?
• Tree is a hierarchical data structure which stores the information naturally in the form of hierarchy
style.
• Tree is one of the most powerful and advanced data structures.
• It is a non-linear data structure compared to arrays, linked lists, stack and queue.
• It represents the nodes connected by edges.
• The figure represents structure of a tree.
Tree has 2 sub trees.
A is a parent of B and C.
B is called a child of A and also parent of D, E, F.
Basic Terminologies
Basic Terminologies
1. Root-
• The first node from where the tree originates is called as a root node.
• In any tree, there must be only one root node.
• We can never have multiple root nodes in a tree data structure.
Basic Terminologies
2. Edge-
• The connecting link between any two nodes is called as an edge.
• In a tree with n number of nodes, there are exactly (n-1) number of edges.
Basic Terminologies
3. Parent-
• The node which has a branch from it to any other node is called as a parent node.
• In other words, the node which has one or more children is called as a parent node.
• In a tree, a parent node can have any number of child nodes.
Here,
Here,
Here,
Here,
Degree of node A = 2
Degree of node B = 3
Degree of node C = 2
Degree of node D = 0
Degree of node E = 2
Degree of node F = 0
Degree of node G = 1
Degree of node H = 0
Degree of node I = 0
Degree of node J = 0
Degree of node K = 0
Basic Terminologies
7. Internal Node-
• The node which has at least one child is called as an internal node.
• Internal nodes are also called as non-terminal nodes.
• Every non-leaf node is an internal node.
Height of node A = 3
Height of node B = 2
Height of node C = 2
Height of node D = 0
Height of node E = 1
Height of node F = 0
Height of node G = 1
Height of node H = 0
Height of node I = 0
Height of node J = 0
Height of node K = 0
Basic Terminologies
11. Depth-
• Total number of edges from root node to a particular node is called as depth of that node.
• Depth of a tree is the total number of edges from root node to a leaf node in the longest path.
• Depth of the root node = 0
• The terms “level” and “depth” are used interchangeably.
Depth of node A = 0
Depth of node B = 1
Depth of node C = 1
Depth of node D = 2
Depth of node E = 2
Depth of node F = 2
Depth of node G = 2
Depth of node H = 2
Depth of node I = 3
Depth of node J = 3
Depth of node K = 3
Basic Terminologies
12. Subtree-
• In a tree, each child from a node forms a subtree recursively.
• Every child node forms a subtree on its parent node.
Basic Terminologies
13. Forest-
• A forest is a set of disjoint trees.
Tree Representations
• A tree data structure can be represented in two methods. Those methods are as follows...
List Representation
Left Child - Right Sibling Representation
• In this representation, every node's data field stores the actual value of that node.
• If that node has left a child, then left reference field stores the address of that left child node
otherwise stores NULL.
• If that node has the right sibling, then right reference field stores the address of right sibling node
otherwise stores NULL.
2. Left Child - Right Sibling Representation
Binary Tree
• A binary tree is a tree data structure composed of nodes, each of which has at most, two children, referred to
as left and right nodes. The tree starts off with a single node known as the root.
• Each node in the tree contains the following:
Data
Pointer to the left child
Pointer to the right child
In case of a leaf node, the pointers to the left and right child point to null.
Types of binary Tree
1. Full Binary Tree
• If each node of binary tree has either two children or no child at all, is said to be a Full Binary
Tree.
• Full binary tree is also called as Strictly Binary Tree.
• Every node in the tree has either 0 or 2 children.
• Full binary tree is used to represent mathematical expressions.
Types of binary Tree
2. Complete Binary Tree
• If all levels of tree are completely filled except the last level and the last level has all keys as left as
possible, is said to be a Complete Binary Tree.
• Complete binary tree is also called as Perfect Binary Tree.
• In a complete binary tree, every internal node has exactly two children and all leaf nodes are at
same level.
• For example, at Level 2, there must be 22 = 4 nodes and at Level 3 there must be 23 = 8 nodes.
Types of binary Tree
3. Skewed Binary Tree
• If a tree which is dominated by left child node or right child node, is said to be a Skewed Binary
Tree.
• In a skewed binary tree, all nodes except one have only one child node. The remaining node has
no child.
• In a left skewed tree, most of the nodes have the left child without corresponding right child.
• In a right skewed tree, most of the nodes have the right child without corresponding left child.
Common operations
• Insertion
• Deletion
• Traversal
1. Insertion
• The very first insertion creates the tree.
• Afterwards, whenever an element is to be inserted, first locate its proper location.
• Start searching from the root node, then if the data is less than the key value, search for the empty
location in the left subtree and insert the data.
• Otherwise, search for the empty location in the right subtree and insert the data.
1. Insertion
Construct a Binary Search Tree by inserting the following sequence of numbers...
10,12,5,4,20,8,7,15 and 13
2. Deletion
• An element may also be removed from the binary tree.
• Since there is no particular order among the elements, upon deletion
of a particular node, it is replaced with the right-most element.
• Algorithm
1. Starting at root, find the deepest and rightmost node in binary tree
and node which we want to delete.
2. Replace the deepest rightmost node’s data with node to be deleted.
3. Then delete the deepest rightmost node.
2. Deletion
3. Tree traversal
1. In-order traversal
2. Post-order traversal
3. Pre-order traversal
1. Preorder Traversal
• Algorithm for preorder traversal
Step 1 : Start from the Root.
Step 2 : Then, go to the Left Subtree.
Step 3 : Then, go to the Right Subtree
Step 2 : A + B + D (E + F) + C (G + H)
Step 3 : A + B + D + E + F + C + G + H
Preorder Traversal : A B D E F C G H
1. Preorder Traversal
2. Postorder Traversal
• Algorithm for postorder traversal
Step 1 : Start from the Left Subtree (Last Leaf).
Step 2 : Then, go to the Right Subtree.
Step 3 : Then, go to the Root.
Step 2 : (E + F) + D + B + (G + H) + C + A
Step 3 : E + F + D + B + G + H + C + A
2. Postorder Traversal
3. Inorder Traversal
• Algorithm for inorder traversal
Step 1 : Start from the Left Subtree.
Step 2 : Then, visit the Root.
Step 3 : Then, go to the Right Subtree.
Step 3 : B + E + D + F + A + G + C + H
Inorder Traversal Example
Binary Search Tree(BST)
• Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers.
• It is called a binary tree because each tree node has a maximum of two children.
• The properties that separate a binary search tree from a regular binary tree is
All nodes of left subtree are less than the root node
All nodes of right subtree are more than the root node
Both subtrees of each node are also BSTs i.e. they have the above two properties
Operations of Binary Search Tree
Search Operation:
• The algorithm depends on the property of BST that if each left subtree has values below root
and each right subtree has values above the root.
• If the value is below the root, we can say for sure that the value is not in the right subtree;
we need to only search in the left subtree and if the value is above the root, we can say for
sure that the value is not in the left subtree; we need to only search in the right subtree.
• Algorithm:
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
Operations of Binary Search Tree
Search Operation: Example
• Find element 4
• 4 is not found so, traverse through the left subtree of 8
Operations of Binary Search Tree
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
Operations of Binary Search Tree
Insert Operation: suppose we want to add element 4 in the given tree.
• Start with the root node 8.
• 4<8 so, transverse through the left child of 8.
Operations of Binary Search Tree
Insert Operation: 4>3 so, transverse through the right of 3.
Operations of Binary Search Tree
Insert Operation: 4<6 so, transverse through the left child of 6.
Operations of Binary Search Tree
Insert Operation: Insert 4 as a left child of 6.
Operations of Binary Search Tree
Delete Operation: There are three cases for deleting a node from a binary search tree.
• Case I
• In the first case, the node to be deleted is the leaf node. In such a case, simply delete the node from
the tree.
• 4 is to be deleted.
Operations of Binary Search Tree
Delete Operation:
• Case I
• After deleting 4.
Operations of Binary Search Tree
Delete Operation:
• Case II
• In the second case, the node to be deleted lies has a single child node. In such a case follow the
steps below:
Replace that node with its child node.
Remove the child node from its original position.
6 is to be deleted
Operations of Binary Search Tree
Delete Operation:
• Case II
• copy the value of its child to the node and delete the child.
Operations of Binary Search Tree
Delete Operation:
• Case III
• In the third case, the node to be deleted has two children. In such a case follow the steps below:
• Replace least value node from right subtree or
• Replace highest value node from left subtree with target element.
• 3 is to be deleted.
Operations of Binary Search Tree
Delete Operation:
• Case III
• Copy the value of the least value node from right subtree to the node i.e 4
• Delete the node
Operations of Binary Search Tree
Min Operation:
To find min value element from tree
Traverse to left most note of BST
Operations of Binary Search Tree
MAX Operation:
To find MAX value element from tree
Traverse to Right most note of BST