0% found this document useful (0 votes)
64 views5 pages

Binary Search Tree Lab Exercises

The document discusses binary search trees and provides code for a C++ class that implements a binary search tree with methods for insertion, searching, and traversing nodes in inorder, preorder, and postorder. The code includes a main function that demonstrates inserting nodes into a binary search tree and using the traversal methods to output the tree. Exercises are provided at the end to count nodes, print root-to-leaf paths, implement node deletion, and mirror the tree.

Uploaded by

xz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views5 pages

Binary Search Tree Lab Exercises

The document discusses binary search trees and provides code for a C++ class that implements a binary search tree with methods for insertion, searching, and traversing nodes in inorder, preorder, and postorder. The code includes a main function that demonstrates inserting nodes into a binary search tree and using the traversal methods to output the tree. Exercises are provided at the end to count nodes, print root-to-leaf paths, implement node deletion, and mirror the tree.

Uploaded by

xz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Structure

LABORATORY

Lab. Sheet six

Binary Search Tree

Computer engineering department


Second class
Binary Search Tree:
#include "stdafx.h"
#include "stdafx.h"
#include<iostream>

using namespace std;


typedef int type ;
class Tree;
//---------------------------------------------------------------------------
class node
{
node *left;
type data;
node*right;
public:
node( type x)
{
this->left = 0 ;
this->right = 0 ;
this-> data = x ;
}
friend class Tree ;
};
//---------------------------------------------------------------------------
class Tree
{

void insert(node *&p,type y)


{
if(p==0)
p=new node(y);
else
{
if(y<p->data)
insert(p->left ,y);
else
insert(p->right,y);
}
}
public :
node *root;
//------------------------------------constructor----------------------
Tree( )
{
root = 0 ;
}
//------------------------------------insert---------------------------
void insert( type x)
{
insert(root,x);
}
//-------------------------------------search--------------------------

void Tree::search(int key_value, node *leaf)


{
if(leaf!=NULL)
{
if(key_value==leaf->data)
{
cout <<"Node is found ^_^"<<endl;
return;
}

if(key_value<leaf->data)
search(key_value, leaf->left);
else
search(key_value, leaf->right);
}
else
cout <<"Node not found..."<<endl;
}

//-------------------------------------inOrder-------------------------
void Tree::inOrder(node* n)
{
if ( n ) {
inOrder(n->left);
cout << n->data << " ";
inOrder(n->right);
}
}
//-------------------------------------PreOrder------------------------
void Tree::PreOrder(node* n)
{
if ( n ) {
cout << n->data << " ";
PreOrder(n->left);
PreOrder(n->right);
}
}
//-------------------------------------PostOrder-----------------------
void Tree::PostOrder(node* n)
{
if ( n ) {
PostOrder(n->left);
PostOrder(n->right);
cout << n->data << " ";
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Tree t;
t.insert(10);
t.insert(2);
t.insert(1);
t.insert(3);
t.insert(7);
t.insert(10);
t.insert(2);
t.insert(5);
t.insert(10);
t.insert(6);
t.insert(4);

cout << "search for value:..((enter N#))" ;


int n;
cin>>n;
t.search(n,t.root);
cout << endl;

cout << "In order traversal" << endl;


t.inOrder(t.root);
cout << endl;

cout << "Pre order traversal" << endl;


t.PreOrder(t.root);
cout << endl;

cout << "Post order traversal" << endl;


t.PostOrder(t.root);
cout << endl;
return 0;
}
Exercises:
1. Returns the number of nodes in binary search tree.
2. Given a binary search tree, prints out all of its root-to-leaf paths.
3. Implement function that can delete a spicific node in tree.
4. Changes the binary search tree into its mirror image.

You might also like