0% found this document useful (0 votes)
4 views

Data Structure Unit 5 Trees

The document provides a comprehensive overview of trees as a hierarchical data structure, detailing key terminologies such as root, edge, parent, child, and various types of trees including binary trees and binary search trees. It explains operations like insertion, deletion, and traversal methods (pre-order, post-order, in-order) along with the properties that distinguish binary search trees. Additionally, it describes tree representations and the concept of subtrees and forests.

Uploaded by

kunalk210706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Structure Unit 5 Trees

The document provides a comprehensive overview of trees as a hierarchical data structure, detailing key terminologies such as root, edge, parent, child, and various types of trees including binary trees and binary search trees. It explains operations like insertion, deletion, and traversal methods (pre-order, post-order, in-order) along with the properties that distinguish binary search trees. Additionally, it describes tree representations and the concept of subtrees and forests.

Uploaded by

kunalk210706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Unit 5

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,

Node A is the parent of nodes B and C


Node B is the parent of nodes D, E and F
Node C is the parent of nodes G and H
Node E is the parent of nodes I and J
Node G is the parent of node K
Basic Terminologies
4. Child-
• The node which is a descendant of some node is called as a child node.
• All the nodes except root node are child nodes.

Here,

Nodes B and C are the children of node A


Nodes D, E and F are the children of node B
Nodes G and H are the children of node C
Nodes I and J are the children of node E
Node K is the child of node G
Basic Terminologies
5. Siblings-
• Nodes which belong to the same parent are called as siblings.
• In other words, nodes with the same parent are sibling nodes.

Here,

Nodes B and C are siblings


Nodes D, E and F are siblings
Nodes G and H are siblings
Nodes I and J are siblings
Basic Terminologies
6. Degree-
• Degree of a node is the total number of children of that node.
• Degree of a tree is the highest degree of a node among all the nodes in the tree.

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.

• Here, nodes A, B, C, E and G are internal nodes.


Basic Terminologies
8. Leaf Node-
• The node which does not have any child is called as a leaf node.
• Leaf nodes are also called as external nodes or terminal nodes.

• Here, nodes D, I, J, F, K and H are leaf nodes.


Basic Terminologies
9. Level-
• In a tree, each step from top to bottom is called as level of a tree.
• The level count starts with 0 and increments by 1 at each level or step.
Basic Terminologies
10. Height-
• Total number of edges that lies on the longest path from any leaf node to a particular node is called
as height of that node.
• Height of a tree is the height of root node.
• Height of all leaf nodes = 0

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

• Consider the following tree:


1. List Representation
• In this representation, we use two types of nodes one for representing the node with data called
'data node' and another for representing only references called 'reference node'.
• We start with a 'data node' from the root node in the tree.
• Then it is linked to an internal node through a 'reference node' which is further linked to any other
node directly.
• This process repeats for all the nodes in the tree.
• The above example tree can be represented using List representation as follows...
2. Left Child - Right Sibling Representation
• In this representation, we use a list with one type of node which consists of three fields namely
Data field, Left child reference field and Right sibling reference field.
• Data field stores the actual value of a node, left reference field stores the address of the left child
and right reference field stores the address of the right sibling node.
• Graphical representation of that node is as follows..

• 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

• Another frequently used tree operation is traversal. Tree traversal is


the process of visiting each node present in a tree. There are three
methods of 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 1 : A + B (B + Preorder on D (D + Preorder on E


and F)) + C (C + Preorder on G and H)

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 1 : As we know, postorder traversal starts from


left subtree (last leaf) ((Postorder on E + Postorder on
F) + D + B )) + ((Postorder on G + Postorder on H) +
C) + (Root A)

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 1 : B + (Inorder on E) + D + (Inorder on F) +


(Root A ) + (Inorder on G) + C (Inorder on H)

Step 2 : B + (E) + D + (F) + A + G + C + H

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

Search Operation: Example


• Find element 4
• 4 is not found so, traverse through the right subtree of 3
Operations of Binary Search Tree

Search Operation: Example


• Find element 4
• 4 is not found so, traverse through the left subtree of 6
Operations of Binary Search Tree

Search Operation: Example


• Find element 4
• 4 is found
Operations of Binary Search Tree
• Search Operation: Example
• If the value is found, we return the value so that it gets propagated in each recursion step
as shown in the image below.
• If you might have noticed, we have called return search(struct node*) four times.
• When we return either the new node or NULL, the value gets returned again and again
until search(root) returns the final result.
• If the value is found in any of the subtrees, it is propagated up so that in the end it is
returned, otherwise null is returned.
• If the value is not found, we eventually reach the left or right child of a leaf node which is
NULL and it gets propagated and returned.
Operations of Binary Search Tree
Insert Operation:
• Inserting a value in the correct position is similar to searching because we try to maintain the rule
that the left subtree is lesser than root and the right subtree is larger than root.
• We keep going to either right subtree or left subtree depending on the value and when we reach a
point left or right subtree is null, we put the new node there.
• Algorithm:

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

You might also like