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

2.Binary search using traversal tree

The document contains a C++ implementation of a binary search tree with functionalities to insert nodes and display them in preorder, inorder, and postorder traversals. It defines a structure for tree nodes and a class for managing the binary tree operations. The main function provides a menu-driven interface for user interaction to insert nodes and display the tree structure.

Uploaded by

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

2.Binary search using traversal tree

The document contains a C++ implementation of a binary search tree with functionalities to insert nodes and display them in preorder, inorder, and postorder traversals. It defines a structure for tree nodes and a class for managing the binary tree operations. The main function provides a menu-driven interface for user interaction to insert nodes and display the tree structure.

Uploaded by

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

2.

Binary search using traversal tree

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

struct node

int info;

struct node*left;

struct node*right;

};

typedef struct node tree;

tree*root =NULL;

class binary

int num;

tree *p, *prev,*temp;

public:

void insert();

void inorder(tree*);

void postorder(tree*);

void preorder(tree*);

void display();

};

void binary::insert()
{

p=new(tree);

cout<<"\n Enter a number:";

cin>>num;

p->info=num;

p->left=p->right=NULL;

if (root==NULL)

root=p;

return;

temp=root;

while(temp!=NULL)

if(num>=temp->info)

prev=temp;

temp=temp->right;

else

prev=temp;

temp=temp->left;

if (num>=prev->info)
prev->right=p;

else

prev->left=p;

void binary::preorder(tree*temp)

if(temp!=NULL)

cout<<" "<<temp->info;

preorder(temp->left);

postorder(temp->right);

void binary :: inorder(tree*temp)

if (temp!=NULL)

inorder(temp->left);

cout<<" "<<temp->info;

inorder(temp->right);

void binary :: postorder(tree*temp)

if (temp!=NULL)

postorder(temp->left);
postorder(temp->right);

cout<<" "<<temp->info;

void binary :: display()

if (root==NULL)

cout<<"\n***Empty tree***";

return;

cout<<"\n\t The preorder display is :";

preorder(root);

cout<<"\n\t The inorder display is :";

inorder(root);

cout<<"\n\t The postorder display is :";

postorder(root);

void main()

binary b;

int ch=1,count=0;

clrscr();

while(ch)

cout<<"\n MENU ";

cout<<"\n 1.Insert \n 2.Display \n 3.Quit \n";


cout<<"Enter your choice:";

cin>>ch;

switch(ch)

case 1:

clrscr();

count++;

b.insert();

break;

case 2:

clrscr();

cout<<"\n The number of nodes the bit is :"<<count;

b.display();

break;

case 3:

exit(0);

getch();

You might also like