0% found this document useful (0 votes)
40 views3 pages

BINARY Search Tree in Data Structure and Algorithm

This C++ program defines a binary tree class with methods for inserting nodes, traversing the tree using inorder, preorder and postorder traversal, and searching for a node. The main function creates a binary tree object, inserts several nodes into the tree, calls the traversal methods to output the tree in different orders, searches for a node and outputs whether it was found. The document contains the struct and class definitions for nodes and the binary tree, along with comments describing the purpose of each method.

Uploaded by

Abba Sultáns
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
40 views3 pages

BINARY Search Tree in Data Structure and Algorithm

This C++ program defines a binary tree class with methods for inserting nodes, traversing the tree using inorder, preorder and postorder traversal, and searching for a node. The main function creates a binary tree object, inserts several nodes into the tree, calls the traversal methods to output the tree in different orders, searches for a node and outputs whether it was found. The document contains the struct and class definitions for nodes and the binary tree, along with comments describing the purpose of each method.

Uploaded by

Abba Sultáns
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

// Q4.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

struct node
{
int data;
node * left;
node * right;
};

class btree
{
private:
node *root;
public:
btree();

bool find(int x);


void insert(int x);
void inorder(node *p);
void preorder(node *p);
void postorder(node *p);
void print_inorder();
void print_preorder();
void print_postorder();
void print_found(int x);
};

btree::btree()
{
root = NULL;
}
void btree::insert(int x)
{
//insert an element into binary tree
node *temp = new node;
temp->data = x;
temp->left = NULL;
temp->right = NULL;

if(root == NULL)
root = temp;
else
{
node * curr = root;
node * parent = root;
while(curr!=NULL)
{
parent = curr;
if (x<curr->data)
curr = curr->left;
else
curr = curr->right;
}

if(parent->data>x)
{
parent->left = temp;
}

else
parent->right = temp;
}
}
void btree::print_inorder()
{
//printing inorder traverse
cout<<"Inorder Traverse"<<"\n";
inorder(root);
cout<<endl;
}
void btree::inorder(node * p)
{ // inorder traversal
if (p==NULL)
{
return;
}
else
{
inorder(p->left);
cout<<p->data<<" ";
inorder(p->right);
}
}
void btree::preorder(node * p)
{
// preorder traverse
if (p==NULL)
{
return;
}
else
{
cout<<p->data<<" ";
preorder(p->left);
preorder(p->right);
}
}
void btree::print_preorder()
{
//printing preorder traverse
cout<<"Preorder Traverse"<<"\n";
preorder(root);
cout<<endl;
}
void btree::print_postorder()
{
// printing post order traverse
cout<<"Postorder Traverse"<<"\n";
postorder(root);
cout<<endl;
}
void btree::postorder(node *p)
{
//finding post order traverse
if (p==NULL)
{
return;
}
else
{
postorder(p->left);
postorder(p->right);
cout<<p->data<<" ";
}
}
bool btree::find(int x)
{
//code to serach an element
x = 0;
cout<<"enter a value to search for"<<"\n";
cin>>x;
if(x == 6)
{
return true;
}
else
{
return false;
}
}

void btree::print_found(int x)
{
//printing the serch element
if(!x)
cout<<"value not found"<<"\n";
else
cout<<"value found"<<"\n";
}
int main()
{
btree t1;
t1.insert(9);
t1.insert(6);
t1.insert(14);
t1.insert(7);
t1.insert(4);
t1.insert(12);
t1.insert(16);
t1.print_inorder();
t1.print_preorder();
t1.print_postorder();
t1.print_found(0);
system("pause");

You might also like