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

dsa 8

The document outlines an experiment on implementing binary search tree traversals in C++, including PreOrder, InOrder, and PostOrder traversals. Each section provides the code for creating a binary tree and the corresponding traversal method. The driver code demonstrates how to build the tree and call the traversal functions, displaying the output for each traversal method.
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)
0 views

dsa 8

The document outlines an experiment on implementing binary search tree traversals in C++, including PreOrder, InOrder, and PostOrder traversals. Each section provides the code for creating a binary tree and the corresponding traversal method. The driver code demonstrates how to build the tree and call the traversal functions, displaying the output for each traversal method.
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/ 8

Experiment no: 8

Name: Pranit Dilip


Lavangare Roll no: 140
Date: 17/02/2025
Title: PROGRAM ON BINARY SEARCH TREE.
1) Implementation of PreOrder Traversal.
// C++ code to implement the approach

#include <iostream.h>

#include<conio.h>

// Class describing a node of tree

class Node {

public:

int data;

Node* left;

Node* right;

Node(int v)

this->data = v;

this->left = this->right = NULL;


}

};

// Preorder Traversal

void printPreOrder(Node* node)

if (node == NULL)

return;

// Visit Node

cout << node->data << " ";

// Traverse left subtree

printPreOrder(node->left);

// Traverse right subtree

printPreOrder(node->right);

// Driver code

void main()

// Build the tree


Node* root = new Node(100);

root->left = new Node(20);

root->right = new Node(200);

root->left->left = new Node(10);

root->left->right = new Node(30);

root->right->left = new Node(150);

root->right->right = new Node(300);

// Function call

cout << "Preorder Traversal: ";

printPreOrder(root);

getch();

Output :

2) Implementation of InOrder Traversal.


// C++ code to implement the approach

#include <iostream.h>
#include<conio.h>

// Class describing a node of tree

class Node {

public:

int data;

Node* left;

Node* right;

Node(int v)

this->data = v;

this->left = this->right = NULL;

};

// Inorder Traversal

void printInorder(Node* node)

if (node == NULL)

return;

// Traverse left subtree

printInorder(node->left);
// Visit node

cout << node->data << " ";

// Traverse right subtree

printInorder(node->right);

// Driver code

void main()

// Build the tree

Node* root = new Node(100);

root->left = new Node(20);

root->right = new Node(200);

root->left->left = new Node(10);

root->left->right = new Node(30);

root->right->left = new Node(150);

root->right->right = new Node(300);

// Function call

cout << "Inorder Traversal: ";

printInorder(root);
getch();

Output :

3) Implementation of POST-Order Traversal.


// C++ code to implement the approach

#include <iostream.h>

#include<conio.h>

// Class to define structure of a node

class Node {

public:

int data;

Node* left;

Node* right;

Node(int v)

this->data = v;

this->left = this->right = NULL;

}
};

// PostOrder Traversal

void printPostOrder(Node* node)

if (node == NULL)

return;

// Traverse left subtree

printPostOrder(node->left);

// Traverse right subtree

printPostOrder(node->right);

// Visit node

cout << node->data << " ";

// Driver code

void main()

Node* root = new Node(100);

root->left = new Node(20);


root->right = new Node(200);

root->left->left = new Node(10);

root->left->right = new Node(30);

root->right->left = new Node(150);

root->right->right = new Node(300);

// Function call

cout << "PostOrder Traversal: ";

printPostOrder(root);

cout << "\n";

getch();

Output:

You might also like