bc230201818 0 Newbie Poster

The set of codes have been taken from Paul programming Tutorials of Bianary search tree while listening to the tutorial
and is not my effort . neither I have written this program. How ever converting the set of codes into animation has been done by me Enjoy watching and it is a Set of codes for learning implimentation of the BST

//#include "stdafx.h" if using any other program Than Visual C++ let this be diabled

#include<iostream>
#include<string>
#include<conio.h>
#include<stdlib.h>
#include<Windows.h>
//#include"BST.h"
using namespace std;
////////////////////////////////Header File/////////////////////////////////////////
//#pragma once
//#include<iostream>
//#include<string>
//#include<conio.h>
//#include<Windows.h>
//using namespace std;
class BST
{
public:
struct node{
int key;
node*left;
node*right;
};
node*root;
void adLeafPvt(int key , node*ptr);

void printInOrderPvt(node*ptr);

node*returnNodePvt(int key,node*ptr);

int findSmalestPvt(node*ptr);

void RemoveNodePvt(int key,node*parent);

BST();

struct node getKey();

void setKey(int key);

node*createLeaf(int key);

void adLeaf(int key);

void printInOrder();

node*returnNode(int key);

int returnRootKey();

void printCjildern(int key);

int findSmalest();
    //void RemoveNode(int key);
    //v//oid RemoveMatchNode();
    ~BST();
};

////////////////////////////////cpp File///////////////////////////////
//#include "stdafx.h"
//#include "BST.h"
//#include<iostream>
//#include<string>
//#include<conio.h>
//#include<stdlib.h>
//#include<Windows.h>
//using namespace std;



BST::BST( )
{
     root = NULL;

     node*ptr;


}


BST::node*BST::createLeaf(int key){

    node*n = new node;
    n->key = key;
    n->left = NULL;
    n->right = NULL;
    return n;}



void BST::adLeaf(int key){

    adLeafPvt(key, root);

}

void BST::adLeafPvt(int key, node*ptr){
if (root == NULL){ root = createLeaf(key); }
else if (key < ptr->key) {if (ptr->left != NULL){ adLeafPvt(key, ptr->left); }
else{ptr->left = createLeaf(key);}}
else if (key > ptr->key)    {
if (ptr->right != NULL){ adLeafPvt(key, ptr->right); }
else{ ptr->right = createLeaf(key); }}
else{ cout << "the key has already been added to the tree"; }}

void BST::printInOrder(){ printInOrderPvt(root); }

void BST::printInOrderPvt(node*ptr){                                      
    if (root != NULL){
        //printInOrderPvt(ptr->left);
        if (ptr->left != NULL){ printInOrderPvt(ptr->left); }//go left
        cout << " " << ptr->key << "  "; Sleep(500);
    }// print curr val
    if (root != NULL){
        //printInOrderPvt(ptr->right);
        if (ptr->right != NULL){ printInOrderPvt(ptr->right); }
    }
else{ cout << "\n    At the moment The Bianary Search Tree is empty :]\n"<<"....................................................................................."; }
}
BST::node*BST::returnNode(int key){return  returnNodePvt(key,root); }
BST::node*BST::returnNodePvt(int key,node*ptr){
if (ptr != NULL){
if (ptr->key == key){return ptr;    }
else{ if (key < ptr->key)
{ return returnNodePvt(key, ptr->left); }
else{ return returnNodePvt(key, ptr->right); }}}
    else{ return NULL; }}

int BST::returnRootKey(){
    if (root != NULL){
        return root->key;
    }
    else{
        return -1000;
    }
}
void BST::printCjildern(int key){
node*ptr = returnNode( key);
if (ptr != NULL){
cout << endl;
cout << "   Parent Node has a value ="<< ptr->key<< endl<<endl;
Sleep(500);
if (ptr->left == NULL)
{cout << "   Left child =  NULL";Sleep(500);}

else{ cout << "   Left child  is " << ptr->left->key; }
if (ptr->right == NULL)
{
    cout << "   Right child =   NULL\n        "; Sleep(500);
}
else{ cout << "   Right child is " << ptr->right->key << endl; }}
else{ cout << "key" << key << "   Does not exisit\n"; Sleep(500); }
}
int BST::findSmalest(){return  findSmalestPvt(root); }

int BST::findSmalestPvt(node*ptr){
if (root == NULL){
    cout << "   The tree is empty\n"; return-1000; Sleep(500);
}
else{
if (ptr->left != NULL){
return findSmalestPvt(ptr->left);}
else{ return ptr->key; }    }}
BST::~BST(){    }
//////////////////////////////////////main( )/////////////////////////////
int main(){ 
BST mytree;
int treekee[16]{50, 76, 21, 4, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, 87, 80};
mytree.printInOrder();
cout << "\n    Printing the tree in order  after adding tree elements \n";
cout << endl;
for (int i = 0; i < 16; i++){        
mytree.adLeaf(treekee[i]);}
cout << endl;
Sleep(500);
mytree.printInOrder();
cout << endl;

//mytree.printCjildern(mytree.returnRootKey());

for (int i = 0; i < 16; i++){    mytree.printCjildern(treekee[i]);Sleep(500);}
cout << endl; 


    cout << "  The smalest value in the tree is   [" << mytree.findSmalest()<<"]";
    cout << endl;

cout << endl;
system("pause");return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.