0% found this document useful (0 votes)
29 views15 pages

CA229 Unit 04

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

CA229 Unit 04

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

Smt.

Chandaben Mohanbhai Patel Institute of


Computer Applications
CHAROTAR UNIVERSITY OF SCIENCE AND TECHNOLOGY

B.C.A. (Semester-3)
CA229 || Fundamentals of Data Structures and Algorithms

Unit-4: Tree Data Structure


 Basic Terminologies,
 Definition and concepts,
 Representation of Binary Tree,
 Operations on Binary Tree,
 Types of Binary Trees,
 B Tree,
 B+ Tree Indexing,
Introduction:
A tree is a non-linear data structure where edges connect the nodes.

Definition:
Root: The topmost node of a tree is known as ROOT and it
does not have any parents.
Leaf Node: If the node does not have any child, it is known as the
Leaf Node. It is also known as the terminal node.
Sibling: If the two-child have the same parent node, then they
are called siblings.
Internal Nodes: A node is known as Internal Node, which has both
parents as well as children. It is also known as a Non-
Leaf Node.
Level of Node: The root has level zero, the child of the root has level
one, and the level of the child is one or more than that
of the parents.
Height: A tree's height is the maximum no. of nodes in a path
between root nodes and a leaf node. It is also called
DEPTH.

CA229- FDSA Page 2


Ancestor and Descendent: If there is a path from node A to node B, then A is
called an ancestor of B, and B is called a descendent of
A.
Binary Tree: A tree is known as a Binary tree in which each node
can have at most two children.
General Tree: The tree which does not have any constraint on the
number of children is known as General Tree. This
means it can have more than two children.
Strictly Binary Tree: If every non-leaf node in a binary tree act as a non-
empty left and right subtree, it is known as a Strictly
Binary Tree. In the Strictly binary tree, the total
number of nodes is 2n- 1 where n=no. of the leaf node.
Completed Binary Tree: A completed Binary Tree with a depth of D is a strictly
binary tree, all whose leaves are at level D. Where the
D=height of the tree. The total no. of nodes incomplete
binary tree is 2^D+1-1
Binary Search Tree: An element is arranged in a tree in such a way that
elements on the right side of the root contain higher
values than the root and those on the left side contain
a lesser value than the root.

Basic Operations:
The basic operations that can be performed on a binary search tree data structure are the
following –
 Insert − Insert an element in a tree/create atree.
 Search − Searches an element in a tree.
 Preorder Traversal − Traverses a tree in a pre-order manner.
 Inorder Traversal − Traverses a tree in an in-order manner.
 Postorder Traversal − Traverses a tree in a post-order manner.

CA229- FDSA Page 3


Insert Operation:
The very first insertion creates the tree. Afterward, 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 sub-tree and insert it.
Otherwise, search for the empty location in the right sub-tree and insert the data.

Algorithm for Insert Operation:


Step 1: If the root is NULL Then
create a root node
return
End If
Step 2: If root exists Then
compare the data with the node.
data until the insertion position is located
If data is greater than the node.
goto right subtree
else
goto left subtree
End If
End While inserting data
End If
Step 3: Exit

Search Operation:
Whenever an element is to be searched, start searching from the root node, then if the
data is less than the key value, search for the element in the left sub-tree. Otherwise,
search for the element in the right sub-tree. Follow the same algorithm for each node.

CA229- FDSA Page 4


Algorithm for Binary Search Tree:
Step 1: [Initialize]
Flag = 0
Step 2: [Repeat through step – 3]
While(node != NULL)
Step 3: [Compare]
If info (node 1) == info Then
Flag = 1
Return flag
Else
If info < info(node 1)
Node = leftchild(node)
else
Node = rightchild(node)
End If
End If
Step 4: Exit

Traversing Binary Trees:


There are three standard ways of traversing a binary tree T with Root R. These three
algorithms, called Preorder, Inorder, and Postorder, are as follows:
Preorder:
1. Process the root R.
2. Traverse the left subtree of R in Preorder.
3. Traverse the right subtree of R in Preorder.
Inorder:
1. Traverse the left subtree of R in Inorder.
2. Process the root R.
3. Traverse the right subtree of R in Inorder.
Postorder:
1. Traverse the left subtree of R in Postorder.
2. Traverse the right subtree of R in Postorder.
3. Process the root R.

CA229- FDSA Page 5


The three algorithms are sometimes called, respectively, the Node-Left-Right (NLR)
traversal, the Left-Node-Right (LNR) traversal, and the Left-Right-Node (LRN) traversal.
Example 1:

Inorder: DBFEAGCLJHK
Preorder: ABDEFCGHJLK
Postorder: DFEBGLJKHCA

Example 2:
Let E denote the following algebraic expression:
[a+(b–c)]*[(d–e)/(f+g–h)]
The corresponding binary tree T appears in the below figure.

Inorder: a+b–c*d–e/f+g-h
Preorder: *+a–bc/-de-+fgh
Postorder: abc-+de–fg+h-/*

CA229- FDSA Page 6


Algorithm for Inorder Traversal:
Step 1: [Check for an emptytree]
If T = null Then
Write "Tree is empty."
End If
Step 2: [Process the left subtree]
If leftptr(T) != NULL Then
Call inorder(leftptr(T))
End If
Step 3: [Visit the root node]
If T != NULL Then
Write (data(T))
End If
Step 4: [Process the right subtree]
If rightptr(T) != NULL Then
Call inorder(rightptr(T))
End If
Step 5: Exit

Algorithm for Preorder Traversal:


Step 1: [Process the rootnode]
If T != NULL Then
Write(data(T))
Else
Write “Tree is empty” Return
End If
Step 2: [Process the left subtree]
If leftptr(T) != NULL Then
Call preorder(leftptr(T))
End If
Step 3: [Process the right subtree]
If rightptr(T) != NULL Then

CA229- FDSA Page 7


Call preorder(rightptr(T))
End If
Step 4: Exit

Algorithm for Postorder Traversal:


Step 1: [Check for an emptytree]
If T = NULL Then
Write "Tree is empty."
Return
End If
Step 2: [Process the left subtree]
If leftptr(T) != NULL Then
Call postorder(leftptr(T))
End If
Step 3: [Process the right subtree]
If rightptr(T) != NULL Then
Call postorder(rightptr(T))
End If
Step 4: [Visit the root node]
If T != NULL Then
Write(data(T))
End If
Step 5: Exit

Representing Binary Tree in Memory:


There are two ways of representing a tree in the memory as follows.
1 Linked Representation of Binary Tree
2 Sequential Representation of Binary Tree.

Consider the following binary tree.

CA229- FDSA Page 8


Linear Representation of Binary Tree:
In Array representation or Linked representation of a binary tree, we use a one-
dimensional array (1-D Array) to represent a binary tree. Consider the above example of
a binary tree, and it is represented as follows.

A B C D E F G H I J

To represent a binary tree of depth 'n' using array representation, we need a one-

dimensional array with a maximum of 2n+1 - 1.

Linked List Representation:


We use a double-linked list to represent a binary tree. In a double-linked list, every node
consists of three fields. The first field is for storing the left child address, the second for
storing actual data, and the third for storing the right child address. The above example
of a binary tree represented using a Linked list representation is shown as follows.

CA229- FDSA Page 9


Deletion from a binary tree:
There is a need to consider the possibility of deleting the nodes for implementation from
a binary tree. They are:

1. Node is a terminal node:


If the node is the left child node of its parent, then the left pointer of its parent is set to
NULL. In all others, if the node is a right child node of its parent, then the right pointer of
its parent is set to NULL.
Example:

2. Node has only one child:


In this scenario, replace the node with its child node, which now contains the value which
is to be deleted. Simply replace it with the NULL and free the allocated space.
Example:

CA229- FDSA Page 10


In this example, node 12 is to be deleted. It has only one child. The node will be replaced
with its child node, and the replaced node 12 (Which is now the leaf node) will simply be
deleted.

3. Node has two children:


In this scenario, the node which is to be deleted is replaced with its in-order successor or
predecessor recursively until the node value (to be deleted) is placed on the leaf of the tree.
After the procedure, replace the node with NULL and free the allocated space.
Example:

In this example, node 50 is to be deleted, which is the tree's root node. The in-order
traversal of the tree is given below.
5, 25, 30, 50, 52, 60, 70, 75
Replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree,
which will simply be deleted.

Application of Binary trees:


Binary Search Tree: Used in many search applications where data is continuously
entering/leaving, such as the map and set objects in many languages' libraries.

Binary Space Partition: It is used in almost every 3D video game to determine what
objects need to be rendered.

Binary Trees: It is used in almost every high-bandwidth router for storing router-tables.

CA229- FDSA Page 11


Hash Trees: Used in p2p programs and specialized image signatures in which a hash
needs to be verified, but the whole file is not available.

Heaps: Used in heapsort; fast implementations of Dijkstra's algorithm; and in


implementing efficient priority queues used in scheduling processes in many operating
systems, Quality-of-Service in routers, and A* (the path-finding algorithm used in AI
applications, including video games).

Huffman Coding Tree (Chip Uni): Used in compression algorithms, such as those used
by the .jpeg and .mp3 fileformats.

GGM Trees: Used in cryptographic applications to generate a tree of pseudo-random


numbers.

Syntax Tree: Constructed by compilers and (implicitly) calculators to parse expressions.

T-tree: Though most databases use some form of B-tree to store data on the drive,
databases that keep all (most) their data in memory often use T-trees to do so.

CA229- FDSA Page 12


Exercise:
Q.1 Find Inorder, Preorder, and Postorder for the following tree.

CA229- FDSA Page 13


CA229- FDSA Page 14
Q.2 Draw a binary tree for the following expression and find out Inorder,
Preorder, and Postorder. Also, perform linked and linear
representation for the tree.
1. ( A + B * C ) / ( A + B ) * C
2. A + ( B – C ) * ( E * F ) / ( G / J )
3. ( ( C * F ) / G ) + ( ( A * B ) + ( C / D ) )
4. A * B – ( C + D ) * ( P / Q )
5. ( A / B + C ) * ( C + D * F ) + ( J / F ) * ( G / H )
6. ( ( C * F ) / G ) + ( ( A * B ) + ( C / D ) )

Q.3 Draw the binary tree from the following Inrder and Postorder.
Inorder n1 n2 n3 n4 n5 n6 n7 n8 n9
Postorder n1 n3 n5 n4 n2 n8 n7 n9 n6

Q.4 Draw the binary tree from the following Inorder and Preorder.
Inorder D B H E A I F J C G
Preorder A B D E H C F I J G

CA229- FDSA Page 15

You might also like