0% found this document useful (0 votes)
19 views7 pages

Unit Iii

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

Unit Iii

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

TREES UNIT III

It is non-linear data structure which contains a set of nodes and edges that have
parent-child relationship. The starting node of a tree is called root node. Every other node
other than root node have parent node. Each nodes of a tree can have any number of
children.
E.g.: -

Tree Terminology
1. Root
In a tree data structure first node is known ad root node. Any tree must have a root
node and there must be only one root node foe a tree.
E.g.: - In the above tree node A is called root node.

2. Edge
The connecting link between any two nodes is called as edge. In a tree with n no. of
nodes there will be maximum of (n-1) edges.
E.g.: - Edge AB connects node A and B

3. Parent Node
The node which is predecessor of any node is called as parent node.
Eg:- B is parent of D and E and A is the parent of B and C.

4. Child Node
Node which is successor of any node is called child node. In a tree any parent node can
have any number of child nodes. The entire node except root node is child node.
Here B and C are children of A.
5. Leaf Node
Nodes which do not have a child are called as leaf nodes. They are also called as terminal
nodes.
E.g.: - Here H,I,J,F,G are leaf nodes.

6. Intermediate Node / Internal Node


The nodes which have at least one child is called internal node. These are also called as
non-terminal nodes.
Eg: - B and C
7. Sibling
Nodes which belong to same parent are called siblings.
E.g.: - D and E
8. Degree
The total number of children of a node is called degree of that node.
E.g.: -
Node Degree
A,B,C,D 2
E 1
H,I,J,F,G 0
9. Level
In a tree, each step from top to bottom is called as level. The level count starts with zero
from root node and incremented by 1 at each step.
E.g.: - A at level 0,
B,C at level 1
10. Height
Total number of edges from leaf node to a particular node in the largest path is called
height of node. The height of root node is considered as height of 3.
E.g.: - Height of node B is 2
11. Depth
Total number of edges from root node to a particular is called the depth of a node.

The depth of the root node is zero. Eg:- Depth of J is 3

12. Path
Path is the sequences of edges from one node to another node.
Eg: - The path between nodes A to D is sequences of edges AB and BD respectively.

BINARY TREE
It is a tree data structure where each node has almost two children called left child
and right child.

There are different types of binary tree.


1. Full Binary Tree
In this type of binary tree every node has exactly two children or not.
E.g.: -

2. Complete Binary Tree


A binary tree in which every internal node has exactly two children and all leaf nodes are
at same level is called as complete binary tree.

E.g:-

Properties of Binary Tree

1. Maximum number of nodes at level L of a binary tree is𝟐𝑳−𝟏, L > 0.


2. Maximum number of nodes in a binary tree with depth K is𝟐𝑲−𝟏, K > 0.

BINARY SEARCH TREE (BST)


It is a Binary Tree with following properties:

1. Left sub tree of a node have key values (Data part) less than its parent node.
2. Right sub tree of a node haver key values greater than its parent node.
E.g.: -

RECURSSION
The process in which the function calls itself directly or indirectly is called as
recursion and corresponding function is called as recursive function. Using recursive
function certain problems such as tree traversal can be solved easily. In a recursive function,
the solution to base case is provided and solution of bigger problem is expressed in terms of
smaller problems.

int fact (int n)


{
if(n<=1)
return 1;
else
return n*fact(n-1);
}
In this example the base case for n<=1 is defined and larger value of number can be solved
by converting it to smaller one till base case is reached.

TREE TRAVERSAL
The process of visiting nodes of a tree is called as tree traversal. There are three different
methods of tree traversal.

i) In-order Traversal
In this type of tree traversal left child of a node is first visited then root node is visited
and finally right node is visited. The algorithm follows recursive method represented as:
Step 1: - Recursively traverse left sub tree.
Step 2: - Visit root node.
Step 3: - Recursively traverse right sub tree.
E.g.: -

The output sequence will be 35 40 45 50 55 60 65

ii) Pre-order Traversal


In this type of tree traversal root node is first visited then left child is visited and finally
right child of node is visited.

Step 1: - Visit root node.


Step 2: - Recursively traverse left sub tree.
Step 3: - Recursively traverse right sub tree.
E.g.: -
The output sequence will be 50 40 35 45 60 55 65

iii) Post-order Traversal


In this type of tree traversal left child of the node is first visited then right child of the
node is visited and finally root node is visited.

Step 1: - Recursively traverse left sub tree.


Step 2: - Recursively traverse right sub tree.
Step 3: - Visit root node.
E.g.: -

The output sequence will be 35 45 40 55 65 60 50

Implement a binary search tree and write down the functions for tree
traversal
#include<iostream>
using namespace std;
class node
{
public:
int data;
node *left,*right;
};
class bst:public node
{
public:
node *root;
bst()
{
root=NULL;
}
void create();
void inorder(node*);
void preorder(node*);
void postorder(node*);
};

void bst::create()
{
node *temp;
temp=new node;
cout<<"Enter the Data"<<endl;
cin>>temp->data;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
root=temp;
else
{
node *p;
p=root;
while(1)
{
if(temp->data<p->data)
{
if(p->left==NULL)
{
p->left=temp;
break;
}
else if(p->left!=NULL)
p=p->left;
}
else if(temp->data>p->data)
{
if(p->right==NULL)
{
p->right=temp;
break;
}
else if(p->right!=NULL)
p=p->right;
}}}}

void bst::inorder(node *m)


{ if(m!
=NULL)
{
inorder(m->left);
cout<<m->data<<" ";
inorder(m->right);
}
}
void bst::preorder(node *m)
{ if(m!
=NULL)
{
cout<<m->data<<" ";
inorder(m->left);
inorder(m->right);
}}
void bst::postorder(node *m)
{ if(m!
=NULL)
{
inorder(m->left);
inorder(m->right);
cout<<m->data<<" ";
}}

int main()
{
bst B;
int y=1,ch;
while(y==1)
{
cout<<"Enter the Choice"<<endl;
cout<<"Enter 1.Create 2.Inorder 3.Preorder 4.Postorder
5.Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1: B.create();
break;
case 2: B.inorder(B.root);
break;
case 3: B.preorder(B.root);
break;
case 4: B.postorder(B.root);
break;
case 5: y=0;
break;
default: cout<<"Invalid Option"<<endl;
}}
return 0;
}

You might also like