Data Structure and Algorithm Unit 5 Trees
Data Structure and Algorithm Unit 5 Trees
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.
• 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.