Lab Manual 13 DSA
Lab Manual 13 DSA
Department
Information Technology
Batch/Year/Section 2018-2022
Marks Signature
Lab Instructor:
Ms. Humaira Anwer
02/06/2017
Lab Manual Tree
#13 s
Lab Manual # 13
Trees
“Tree is a non-linear data structure which organizes data in hierarchical structure and this
is a recursive definition.”
Tree represents the nodes connected by edges. A tree data structure can be described as a
collection of nodes (starting at a root node), where each node is a data structure consisting of
a value, together with a list of references to nodes (the "children"), with the constraints that
no reference is duplicated, and none points to the root. Tree is a nonlinear data structure.
The tree with no nodes is called the null or empty tree. A tree that is not empty consists of a
root node and potentially many levels of additional nodes that form a hierarchy. In a tree data
structure, if we have N number of nodes then we can have a maximum of N-1 number of
links.
A binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer,
and a data element as shown in fig. 12.2.
The "root" pointer points to the topmost node in the tree. The left and right pointers
recursively point to smaller "subtrees" on either side. A null pointer represents a binary tree
with no elements -- the empty tree.
A binary tree has the benefits of both an ordered array and a linked list as search is as quick
as in a sorted array and insertion or deletion operation are as fast as in linked list.
Tree Terminology
In a tree data structure,
following terminology is
used:
Root
Edge
Parent
Child
Siblings
Leaf
Internal Nodes
Degree
Level
In a tree data structure, the root node is said to be at Level 0 and the children of root node are
at Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In
Lab Manual Tree
#13 s
13.4.10 Height
.
In a tree data structure, the total
number of egdes from leaf node
to a particular node in the
longest path is called
as HEIGHT of that Node. In a
tree, height of the root node is
said to be height of the
tree. In a tree, height of
all leaf nodes is '0'.
Depth
Path
In a binary tree, every node can have a maximum of two children. But in strictly binary tree,
every node should have exactly two children or none. That means every internal node must
have exactly two children. A strictly
Binary Tree can be defined as follows...
A binary tree can be converted into Full Binary tree by adding dummy nodes to existing
nodes wherever required.
The full binary tree obtained by adding dummy nodes to a binary tree is called as Extended
Binary Tree.
In simple words a node's left child must have a value less than its parent's value and the
node's right child must have a value greater than its parent value.
TREE NODE
The code to write a tree node would be similar to what is given below. It has a data part and
references to its left and right child nodes.
1. struct node
2. {
3. int data;
4. node *leftChild;
5. node *rightChild;
6. };
The basic operations that can be performed on a binary search tree data structure are the
following −
1. Insert − Inserts an element in a tree/create a tree.
2. Search − Searches an element in a tree.
3. Preorder Traversal − Traverses a tree in a pre-order manner.
4. Inorder Traversal − Traverses a tree in an in-order manner.
5. Postorder Traversal − Traverses a tree in a post-order manner.
INSERT Operation
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.
INSERT Algorithm:
void insert(int data)
1. START
2. SET node *tempNode = new node
3. SET tempNode->data = data
4. SET tempNode->leftChild = NULL
5. SET tempNode->rightChild = NULL
6. DECLARE node *current
7. DECLARE node *parent
//if tree is empty
8. CHECK if(root == NULL)
a. SET root = tempNode
9. else
a. SET current = root
b. parent = NULL
c. REPEAT STEPS i to v while(1)
i. SET parent = current
//go to left of the tree
ii. CHECK if(data < parent->data)
SET current = current->leftChild
//insert to the left
CHECK if(current == NULL)
SET parent->leftChild = tempNode
return
END if
iii. END if
//go to right of the tree
iv. else
SET current = current->rightChild
//insert to the right
if(current == NULL)
parent->rightChild = tempNode
return
END if
v. END else
d. END while loop step c.
10. END else step 9
11. EXIT
SEARCH Operation
Whenever an element is to be searched, start searching from the root node. Then if the data is
less than the target value, search for the element in the left subtree. Otherwise, search for the
element in the right subtree. Follow the same algorithm for each node.
Search Algorithm:
node* search(int data)
Description: This algorithm searches for a target value in a tree.
1. START
2. SET node *current = root;
3. REPEAT steps a to i while(current->data != data)
a. if(current != NULL)
b. PRINT current->data
//go to left tree
c. if(current->data > data)
i. current = current->leftChild
d. END if
//else go to right tree
e. else
i. current = current->rightChild;
f. END else
//not found
g. CHECK if(current == NULL)
i. return NULL;
h. END if
i. END WHILE loop step 3
j. return current
k. EXIT
PREORDER TRAVERSAL
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.
Preorder Algorithm:
void pre_order_traversal(struct node* root)
1. START
2. CHECK if(root != NULL)THEN
a. PRINT root->data
b. CALL pre_order_traversal(root->leftChild)
c. CALL pre_order_traversal(root->rightChild)
3. END if
4. EXIT
INORDER TRAVERSAL
In this traversal method, the left subtree is visited first, then the root and later the right sub-
tree. We should always remember that every node may represent a subtree itself. If a binary
tree is traversed in-order, the output will produce sorted key values in an ascending order.
Inorder Algorithm:
void inorder_trave rsal(struct node* root)
1. START
2. CHECK if(root != NULL)THEN
a. CALL inorder_traversal(root->leftChild)
b. PRINT root->data
c. CALL inorder_traversal(root->rightChild)
3. END if
4. EXIT
POSTORDER TRAVERSAL
In this traversal method, the root node is visited last, hence the name. First we traverse the
left subtree, then the right subtree and finally the root node.
Postorder Algorithm:
void post_order_traversal(struct node* root)
1. START
2. CHECK if(root != NULL)THEN
a. CALL post_order_traversal(root->leftChild)
b. CALL post_order_traversal(root->rightChild)
c. PRINT root->data
3. END if
4. EXIT
LAB TASK
1. Write a single C++ code to implement all discussed algorithms for tree data structure.
Code Area
#include <iostream>
using namespace std;
//declaration for new tree node
struct node {
int data;
struct node *left;
struct node *right;
};
//allocates new node
struct node* newNode(int data) {
// declare and allocate new node
struct node* node = new struct node();
node->data = data; // Assign data to this node
// Initialize left and right children as NULL
node->left = NULL;
node->right = NULL;
return(node);
}
int main() {
/*create root node*/
struct node *rootNode = newNode(10);
cout<<"General tree created is as follows:"<<endl;
cout<<"\t\t\t "<<rootNode->data<<endl;
cout<<"\t\t\t "<<"/ "<<"\\"<<endl; rootNode->left =
newNode(20);
rootNode->right = newNode(30);
cout<<"\t\t\t"<<rootNode->left->data<<" "<<rootNode-
>right->data;
cout<<endl; rootNode->left->left = newNode(40);
cout<<"\t\t\t"<<"/"<<endl;
cout<<"\t\t "<<rootNode->left->left->data;
return 0;
}