0% found this document useful (0 votes)
17 views2 pages

Practical-8: AIM:-Program To Illustrate The Implementation of Different Operations On A Binary Search Tree

This C++ program demonstrates different traversal methods (preorder, inorder, and postorder) on a sample binary search tree. It defines a Node structure to represent nodes, with data and left/right child pointers. Functions are created to print the traversals by recursively visiting nodes. A main function builds a sample tree and calls the print functions to output the traversals.
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)
17 views2 pages

Practical-8: AIM:-Program To Illustrate The Implementation of Different Operations On A Binary Search Tree

This C++ program demonstrates different traversal methods (preorder, inorder, and postorder) on a sample binary search tree. It defines a Node structure to represent nodes, with data and left/right child pointers. Functions are created to print the traversals by recursively visiting nodes. A main function builds a sample tree and calls the print functions to output the traversals.
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/ 2

PRACTICAL-8

 AIM :- Program to illustrate the implementation of different operations on a binary


search tree.

#include <iostream>
using namespace std;

struct Node
{
int data;
struct Node* left, *right;
Node(int data)
{
this->data = data;
left = right = NULL;
}
};

void printPostorder(struct Node* node)


{
if (node == NULL)
return;

printPostorder(node->left);

printPostorder(node->right);

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


}

void printInorder(struct Node* node)


{
if (node == NULL)
return;

printInorder(node->left);

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

printInorder(node->right);
}

void printPreorder(struct Node* node)


{

SHREEPRIYA WALI UID-19BCS8012


if (node == NULL)
return;

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

printPreorder(node->left);

printPreorder(node->right);
}

int main( )
{
struct Node *root = new Node(60);
root->left = new Node(45);
root->right = new Node(73);
root->left->left = new Node(12);

cout << "\nPreorder traversal of binary tree is \n";


printPreorder(root);

cout << "\nInorder traversal of binary tree is \n";


printInorder(root);

cout << "\nPostorder traversal of binary tree is \n";


printPostorder(root);

return 0;
}

OUTPUT

SHREEPRIYA WALI UID-19BCS8012

You might also like