0% found this document useful (0 votes)
8 views

Exp 2

Uploaded by

papu varsha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Exp 2

Uploaded by

papu varsha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DATE EX.

NO: 2: Implementation of iteration function for tree


traversal and Fibonacci

2A: Tree Traversal with Iteration

AIM:
To write a program to implement of Iteration function for tree traversal.

ALGORITHM:

1) Create an empty stack S.


2) Initialize current node as root
3) Push the current node to S and set current = current->left until current is NULL
4) If current is NULL and stack is not empty then
a) Pop the top item from stack.
b) Print the popped item, set current = popped_item->right
c) Go to step 3.
5) If current is NULL and stack is empty then we are done.
PROGRAM :
#include <iostream>
#include <stack>
using namespace std;
// Data structure to store a binary tree node
struct Node
{
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = this->right = nullptr;
}
};
// Iterative function to perform preorder traversal on the tree
void preorderIterative(Node* root)
{
// return if the tree is empty
if (root == nullptr)
return;
// create an empty stack and push the root node
stack<Node*> stack;
stack.push(root);
// loop till stack is empty
while (!stack.empty())
{
// pop a node from the stack and print it
Node* curr = stack.top();8
stack.pop();
cout << curr->data << " ";
// push the right child of the popped node into the stack
if (curr->right)
{
stack.push(curr->right);
}
// push the left child of the popped node into the stack
if (curr->left)
{
stack.push(curr->left);
}
// the right child must be pushed first so that the left child
// is processed first (LIFO order)
}
}

int main()
{
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->right = new Node(6);
root->right->left->left = new Node(7);
root->right->left->right = new Node(8);
preorderIterative(root);
return 0;
}

OUTPUT:

12435786
2B: Fibonacci with Iteration

AIM:
To write a program to implement of Iteration function for Fibonacci.

ALGORITHM:
1: Start

2: Read n value for computing nth term in Fibonacci series

3: call Fibonacci (n)

4: Print the nth

5: End Fibonacci(n)

6: If n = 0 then go to step2 else go to step3

7: return 0

8: If n = 1 then go to step4 else go to step5

9: return 1

10: return(Fibonacci (n-1) + Fibonacci (n-2))


PROGRAM :
#include <iostream>
using namespace std;
int main()
{
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" "; //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
return 0;
}

OUTPUT :
Enter the number of elements: 9
0 1 1 2 3 5 8 13 21

RESULT:
Thus the C++ program to implement iteration function for tree traversal and
Fibonacci was written, executed and verified successfully .

You might also like