100% found this document useful (1 vote)
179 views

Data Structure and Algorithm Unit 5 Trees

This material is for students of 3rd semester of Diploma Computer Engineering.

Uploaded by

rutvi sheth
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
179 views

Data Structure and Algorithm Unit 5 Trees

This material is for students of 3rd semester of Diploma Computer Engineering.

Uploaded by

rutvi sheth
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Data Structure

and Algorithm
4330704
Rutvi S. Sheth
Unit-5 Trees
CO4: Illustrate algorithms to insert, delete and searching a
node in tree.
5.1 Non-linear data structures:
Tree, Graph
• In the Non linear data structure processing of data items is not
possible in linear fashion.
• Examples of the non linear data structure are: (A) Tree (B)
Graph
• (A) Tree: In tree data contains hierarchical relationship.
• (B) Graph: this data structure contains relationship between
pairs of elements.
5.2 General Tree
• A Tree in which each node having either
0 or more child nodes is called general tree. So we
can say that a Binary Tree is a specialized case of General
tree.
• General Tree is used to implement File System.
• Example:
5.2 Forest
• A forest is a set of n ≥ 0 disjoint trees. Thus, Forest is a
collection of disjoint trees.
• Example: Consider Following Figure in which a forest is a
collection of two disjoint tree.
5.2 Binary Trees
• A Binary Tree Data Structure is a hierarchical data structure
in which each node has at most two children, referred to as
the left child and the right child.
5.2 Level Number
• In tree data structures, the root node is said to be at level 0,
and the root node's children are at level 1, and the children of
that node at level 1 will be level 2, and so on. Thus, Level of a
node represents the generation of a node.
5.2 Degree
• In a tree the sum of edges comes into particular node and
edges comes out from particular node is called degree of that
particular node.
• It means Degree is the sum of in degree and out degree.
• It is also called total degree of node.
• Example: Total degree of node B is 3
5.2 In-Degree
• In a tree the sum of edges comes into particular node and
edges comes out from particular node is called degree of that
particular node.
• It means Degree is the sum of in degree and out degree.
• It is also called total degree of node.
• Example: Total degree of node B is 3
5.2 Out-Degree
• In a tree number of edges comes out from particular node is
called Out degree of that particular node.
• Leaf node having out degree always equals to 0 because it is
the last node of tree having no further sub tree.
• Example: Out-degree of node B is 2.
5.2 Root Node
• The node at the top of the tree is called root node. There is
only one root in a tree and one path from the root node to any
node.
5.2 Leaf Node
• The node which does not have any child node is called the leaf
node.
• Here in below diagram nodes D, E,F,G are leaf nodes.
5.2 directed edge
• An edge of directed tree that shows direction from source
node to destination node is called as directed edge.
• Here in below example an edge from A to B is called directed
edge.
5.2 path
• A sequence of connecting edges from one node to another
node is called Path.
• In below figure path from Node A to H is given as A->B->D-
>H
• In below figure path from Node A to I is given as A->C->G->I
5.2 depth
• The depth of a tree is the number of edges from the root node
to a specific node in a tree.
• In below figure depthof node E is 2
because the path from Root Node A to Node E is A->B-
>E.
• Here the length of the Path is 2 so depth of node E is 2.
5.2 complete binary tree
• A complete binary tree is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes in
the last level are as far left as possible.
5.2 strict binary tree
• If every non-leaf node in a binary tree has non-empty left and
right subtrees, the tree is termed a strictly binary tree. Or, all
of the nodes in a strictly binary tree are of degree zero or two,
never degree one.
• It is also known as proper or full binary tree.
5.2 conversion of general tree to
binary tree
• The process of converting general tree in to binary tree is given
below:
1. Root node of general tree becomes root node of
Binary Tree.
2. Now consider T1, T2, T3 ... Tn are child nodes of the root node
in general tree. The left most child (T1) of the root node in
general tree becomes left most child of root node in the binary
tree. Now Node T2 becomes right child of Node T1, Node T3
becomes right child of Node T2 and so on in binary tree.
3. The same procedure of step 2 is repeated for each leftmost
node in the general tree.
5.2 conversion of general tree to
binary tree
• Example:
• Consider General tree given in the figure:
• Step 1: Root Node of General tree
becomes the Root node of
binary tree.

• Step 2: Now Root Node (A) has three


child Nodes (B, H, E) in
general tree. The leftmost node (B)
of the root node (A) in the general tree
becomes the left most node of the root
node in binary tree.
5.2 conversion of general tree to
binary tree

• Step 3: Now Node H becomes the


right node of B and Node E becomes the
right node of H.

• Step 4: Now Node B has only one left child


node, which is C in general tree. So Node
C becomes left child of Node B in binary
tree.
5.2 conversion of general tree to
binary tree

• Step 5: Now Node E has two child


nodes (F, G). The leftmost node (F) of
the node (E) in the general tree
becomes the left most node of the
node E in the binary tree.
5.2 conversion of general tree to
binary tree

• Step 6: Now Node G becomes right


node of Node F in binary tree.
5.2 conversion of general tree to
binary tree
5.3 Binary Search Tree
• Binary Search Tree is a data structure used in computer
science for organizing and storing data in a sorted manner.
• Each node in a Binary Search Tree has at most two children, a
left child and a right child, with the left child containing values
less than the parent node and the right child containing values
greater than the parent node.
5.3 Construction of Binary Search
Tree
• Suppose we want to construct binary search tree for
following set of data:
• 45 68 35 42 15 64 78
• Step 1: First element is 45 so it is inserted as a root node of
the tree.

• Step 2: Now we have to insert 68. First we compare 68 with


the root node which is 45. Since the value of 68 is greater
then 45 so it is inserted to the right of the root node.
5.3 Construction of Binary Search
Tree
• Step 3: Now we have to insert 35. First we compare 35 with
the root node which is 45. Since the value of 35 is less then 45
so it is inserted to the left of the root node.

• Step 4: Now we have to insert 42. First we compare 42 with


the root node which is 45. Since the value of 42 is less then 45
so it is to be inserted to the left of the root node. Next 42 is
compared with 35, here 42 is greater than 35 and so is
inserted to the right of 35.
5.3 Construction of Binary Search
Tree

• Step 5: Now we have to insert 15


• so left of 45
• so left of 35
5.3 Construction of Binary Search
Tree
• Step 5: Now we have to insert 64.
• 64 so right of 45
• so left of 68
5.3 Construction of Binary Search
Tree
• Step 7: Now we have to insert 78
• 78 so right of 45
• so right of 68
5.3 Deletion of node in Binary
Search Tree
• There are three possibilities when we want to delete an element
from binary search tree.
1. If a node to be deleted has empty left and right sub tree then a
node is deleted directly.
2. If a node to be deleted has only one left sub tree or right sub tree
then the sub tree of the deleted node is linked directly with the
parent of the deleted node.
3. If a node to be deleted has left and right sub tree then we have to
do following steps:
1. Find Inorder Successor: The smallest node in the right subtree (the leftmost
child of the right child).
2. Then we need to replace the key of given node with key of successor node.
3. Delete successor node.
5.3 Deletion of node in Binary
Search Tree
5.3 Deletion of node in Binary
Search Tree
5.3 Deletion of node in Binary
Search Tree
5.3 Searching a node in Binary
Search Tree
• First the key to be searched is compared with root node. If it
is not in the root node then we have to possibilities :

1. If the key to be searched having value less than root node


then we search the key in left sub tree.

2. If the key to be searched having value greater then root node


then we search the key in right sub tree.
5.3 Searching a node in Binary
Search Tree
• Search X=17
• so move right side
• so move right side
• Node(17) found.

• Search X=19
• so move right side
• so move right side
• so move right side
• so move right side
• Right of 18 is NULL
• Node(19) not found.
5.3 Searching a node in Binary
Search Tree
• Algorithm Search(X,ROOT) • Step 3:[Decide which subtree to
• Step 1: [Check if the tree is search next.]
empty] If X < ROOT → INFO then
If ROOT == NULL then Call SEARCH (ROOT →
Write “Tree is Empty. Search LPTR, X)
Un Successful” Else
Exit Call SEARCH (ROOT →
• Step 2: [Check if the current RPTR, X)
node contains the value X] • Step 4:[Finished]
If X=ROOT→INFO then
EXIT
Write “Search is Successful”
Return (ROOT)
5.4 Binary Tree Traversal
• Traversal: Traversal is the method of
processing every nodesin the tree exactly once in a
systematic manner.
• Types of Traversal: There are three different types of tree
traversal.
1. Preorder Traversal
2. In order Traversal
3. Post order Traversal
5.4 Binary Tree Traversal-Preorder
Traversal
• The Preorder traversal follows the three steps in recursive
approach:
• Visit root node. (Process node)
• Visit all nodes in left subtree.
• Visit all nodes in right sub tree.
• display(root → data)
• preorder(root → left)
• preorder(root → right)
5.4 Binary Tree Traversal Preorder
Algorithm
• PREORDER(T) • Step 4:[Process the right
• Step 1:[Check for empty tree] sub tree]
If T == NULL then If RPTR (T) ≠ NULL then
Write “Tree is empty”
Call PREORDER (RPTR (T))
Exit
• Step 2:[Process the root node] • Step 5:[Finished]
Write (DATA(T)) Return
• Step 3:[Process the left sub
tree]
If LPTR (T) ≠ NULL then
Call PREORDER (LPTR (T))
Preorder traversal Example
Trick:
Follow bellow
steps:
1. Draw left
tick mark
from each
node.
2. Traverse
the nodes
from top-
left-right
3. Write each
node at the
point when
the path
intersects
the tick
mark.
5.4 Binary Tree Traversal-Inorder
Traversal
• The Preorder traversal follows the three steps in recursive
approach:
• Visit all nodes in left subtree.
• Visit root node. (Process node)
• Visit all nodes in right sub tree.
• inorder(root → left)
• display(root → data)
• inorder(root → right)
5.4 Binary Tree Traversal Inorder
Algorithm
• INORDER(T) • Step 4:[Process the right
• Step 1:[Check for empty tree] sub tree]
If T == NULL then If RPTR (T) ≠ NULL then
Write “Tree is empty”
Call INORDER (RPTR (T))
Exit
• Step 2:[Process the left sub • Step 5:[Finished]
tree] Return
If LPTR (T) ≠ NULL then
Call INORDER (LPTR (T))
• Step 3:[Process the root node]
Write (DATA(T))
Inorder traversal Example
Trick:
Follow bellow
steps:
1. Draw down
tick mark
from each
node.
2. Traverse
the nodes
from top-
left-right
3. Write each
node at the
point when
the path
intersects
the tick
mark.
5.4 Binary Tree Traversal-Postorder
Traversal
• The Preorder traversal follows the three steps in recursive
approach:
• Visit all nodes in left subtree.
• Visit all nodes in right sub tree.
• Visit root node. (Process node)
• postorder(root → left)
• postorder(root → right)
• display(root → data)
5.4 Binary Tree Traversal Postorder
Algorithm
• POSTORDER(T) • Step 3:[Process the right
• Step 1:[Check for empty sub tree]
tree] If RPTR (T) ≠ NULL then
If T == NULL then Call POSTORDER (RPTR (T))
Write “Tree is empty” • Step 4:[Process the root
Exit node]
• Step 2:[Process the left Write (DATA(T))
sub tree]
If LPTR (T) ≠ NULL then
• Step 5:[Finished]
Return
Call POSTORDER (LPTR (T))
Postorder traversal Example
Trick:
Follow bellow
steps:
1. Draw right
tick mark
from each
node.
2. Traverse
the nodes
from top-
left-right
3. Write each
node at the
point when
the path
intersects
the tick
mark.
5.5 Applications of Binary Tree
1. Expression Trees: Used to evaluate arithmetic expressions
in compilers.
2. Binary Search Trees (BSTs): Efficiently manage dynamic
sets and allow fast search, insert, and delete operations.
3. Priority Queues (Heap): Implemented using binary trees for
scheduling tasks and resource management.
4. Huffman Coding Trees: Used in data compression
algorithms to reduce the size of files.
5. Routing Algorithms: Binary trees help manage and optimize
routing tables in networks.

You might also like