answersLogoWhite

0


This program is to implement a binary search tree in c++


#include

using

namespace std;


struct

node{

int

data;

node *left, *right;

node( int

d = 0

, node *l = NULL

, node *r = NULL

): data(d), left(l), right(r){}

};


class

tree{

public:

node *root;

tree(){

root = NULL

;

}

void

inorder(){

cout<<

"The inorder traversal is :";

inorder(root);

cout<

}

void

inorder(node *temp){

if

(temp){

inorder(temp->left);

cout<data<<"\t"

;

inorder(temp->right);

}

}

void

preorder(){

cout<<

"The preorder traversal is :";

preorder(root);

cout<

}

void

preorder(node *temp){

if

( temp){

cout<data<<"\t"

;

preorder(temp->left);

preorder(temp->right);

}

}

void

postorder(){

cout<<

"The postorder traversal is :";

postorder(root);

cout<

}

void

postorder(node *temp){

if

( temp){

postorder(temp->left);

postorder(temp->right);

cout<data<<"\t"

;

}

}

void

insert( int

data ){

insert(data, root);

}

void

insert( int

data, node *&temp){

if

( !temp)

temp = new

node(data);

else

if

( temp->data < data )

insert(data, temp->right);

else

if

( temp->data > data)

insert(data, temp->left);

}

node *findmin(node *temp){

while

( temp->left != NULL

)

temp = temp->left;

return

temp;

}

void

remove(int

data){

remove(data, root);

}

void

remove( int

data, node *&temp){

if

( temp == NULL

)

cout<<"data not found\n"

;

else

if

( temp->data < data)

remove(data, temp->right);

else

if

(temp->data > data)

remove(data, temp->left);

else

if

( temp->right && temp->left){

node *min = findmin(temp->right);

temp->data = min->data;

remove(min->data, temp->right);

}else

{

node *curr = temp;

if

( !temp->left)

temp = temp->right;

else

temp = temp->left;

delete

curr;

}

}

};


int

main(){

int

choice, data;

tree obj;

do

{

cout<<

"Enter choice:\n1-Insert\n2-Delete\n3-Inorder Traversal\n4-Preorder Traversal\n5-Postorder Traversal\n6-Quit\t:";

cin>>choice;

switch

(choice){

case

1

:

cout<<

"Enter data for insert:\t";

cin>>data;

obj.insert(data);

break

;

case

2

:

cout<<

"Enter data for removal:\t";

cin>>data;

obj.remove(data);

break

;

case

3

:

obj.inorder();

break

;

case

4

:

obj.preorder();

break

;

case

5

:

obj.postorder();

break

;

case

6

: break

;

}

}while

( choice != 6

);

return

0

;

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

How do you implement insertion into AVL tree in C plus plus?

See related links for an example.


Can you implement the same using c?

Some function are not using in c


Write a program in c plus plus to implement macro processor?

Don't write, it is already written, google for 'cpp'.


What is stream operator in c plus plus?

There are two stream operators: &lt;&lt; (insert or put) and &gt;&gt; (extract or get). Output streams implement the insertion operator, input streams implement the extraction operator and input/output streams implement both operators.


How do you run graphics program in C?

pro c language to implement linear search using pointers


Can you use c in c plus plus without using class and object?

Sure.


To develop a taj mahal using c-language programming in turbo c plus plus?

turbo c


Why study c plus plus programming?

we are using c plus plus programming for developing object oriented programing software.


How do you implement a program to display the Indian flag with colors using c graphics?

ANSI/ISO C does not and never has done graphics.


Which statement in C plus plus is used to implement a decision structure in its simplest form-that of choosing between two alternatives?

if (condition) statement else statement;


How can I implement a tournament tree in C for efficient data structure manipulation during a competition or tournament?

To implement a tournament tree in C for efficient data structure manipulation during a competition or tournament, you can use a binary tree structure where each node represents a match between two participants. The winner of each match moves up the tree until a final winner is determined. This allows for quick access to match results and efficient updating of the tree during the tournament.


How should you design a scientific calculator using C plus plus?

The easiest way to implement a calculator is an RPN calculator (enter the numbers first, perform the operation last). You need a last-in-first-out stack (there's a "stack" class in C++, but you can also implement your own using an array or a linked list), and a set of functions that pop the last elements from the stack and push the result (e.g. Add() pops the last 2 values and pushes their addition).You'll need the math.h library for scientific operations.