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<
;
inorder(temp->right);
}
}
void
preorder(){
cout<<
"The preorder traversal is :";
preorder(root);
cout<
}
void
preorder(node *temp){
if
( temp){
cout<
;
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<
;
}
}
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
;
}
See related links for an example.
Some function are not using in c
Don't write, it is already written, google for 'cpp'.
There are two stream operators: << (insert or put) and >> (extract or get). Output streams implement the insertion operator, input streams implement the extraction operator and input/output streams implement both operators.
pro c language to implement linear search using pointers
Sure.
turbo c
we are using c plus plus programming for developing object oriented programing software.
ANSI/ISO C does not and never has done graphics.
if (condition) statement else statement;
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.
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.