I wanted to study c++ so i took up algorithms.I have implemented a Binary search tree with insert,delete,inorder and minimum element functions.I would like to have some feedback and some pointers to do things better.Anything is appreciated.
code:
#include <iostream>
using namespace std;
//This represents my tree node
struct node{
int value;
node *left,*right;
};
//minimum value of bst
node* minValue(node* tnode){
if(tnode->left!=NULL){
return minValue(tnode->left);
}
else{
return tnode;
}
}
//Inorder traversal
void inorder(node *tnode){
if(tnode->left!=NULL){
inorder(tnode->left);
}
cout<<tnode->value;
if(tnode->right!=NULL){
inorder(tnode->right);
}
}
//insert function
node* insert(int val,node *tnode){
node *temp=tnode;
if(temp==NULL){
temp= new node;
temp->value=val;
temp->left=NULL;
temp->right=NULL;
}
else if(val<temp->value){
temp->left=insert(val,temp->left);
}
else if(val>temp->value){
temp->right=insert(val,temp->right);
}
return temp;
}
//delete function
node* deleteNode(node* tnode,int val){
if(tnode==NULL)
return tnode;
if(tnode->value>val){
tnode->left=deleteNode(tnode->left,val);
}
else if(tnode->value<val){
tnode->right=deleteNode(tnode->right,val);
}
else{
if(tnode->left==NULL){
node* temp=tnode->right;
delete tnode;
return temp;
}
else if(tnode->right==NULL){
node* temp=tnode->left;
delete tnode;
return temp;
}
node* temp=minValue(tnode->right);
tnode->value=temp->value;
tnode->right=deleteNode(tnode->right,temp->value);
}
return tnode;
}
int main(){
cout<<"Binary Tree";
node *ROOT=NULL;
ROOT=insert(3,NULL);
insert(2,ROOT);
insert(5,ROOT);
insert(4,ROOT);
insert(1,ROOT);
insert(7,ROOT);
insert(8,ROOT);
deleteNode(ROOT,3);
inorder(ROOT);
return 0;
}