Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3027

Binary Search tree

$
0
0
I am unable to implement the insert function properly,every time i run the program i just get the first value and name,i am not getting other Id's and name.Please help!Thanks
Code:

"(Header File)"

#include <iostream>
#include <string>
using namespace std;


class node
{
  public:
  int ID;
  node (string StudentName, int IDNumber) {
  name = StudentName;
  ID = IDNumber; // key value
  left = NULL;
  right = NULL;
  parent = NULL;
}
}; // end class node

class bst {
                class node *root; // root of the tree

              // print in ascending order of IDs

void printInOrder(class node *n)
 {
  if (n == NULL){
          //nothing to print
            return;}
           
  // recursively print left sub-tree

              printInOrder(n->left);

              // print this node;

              cout << "<" << n->ID << ", " << n->name << ">" << endl;

              // recursively print right subtree

              printInOrder(n->right);
}

public:
        bst() {
                  root = NULL;}

void print() {
                  printInOrder(root);
}

bool insert(string StudentName, int IDNumber);

      // returns true if successfully inserted
      // otherwise returns false (if matching ID exists in BST)

};



"(Main file)"


#include "bst.h"

int main() {
             
class bst *tree = new bst();

while (1) {
  int choice;

  // 1 to insert a record, 2 to remove a record, 3 to print records in ascending order of IDs, 0 to exit

  int StudentID;

  string StudentName;

  cout << endl << "Enter choice (1 to insert a record, 2 to remove a record, 3 to print records, 0 to exit): ";

  cin >> choice;

if (choice == 0) {
break;
}

else if (choice == 1) {
        cout << endl << "Enter new student ID: ";
        cin >> StudentID
        cout<< "Enter new student name: ";
        cin >> StudentName;
    if (tree->insert(StudentName, StudentID))
    cout << "New student record inserted in BST" << endl;
else
cout << "Failed to add new student record in BST - matching ID exists" << endl;
}
if (choice == 3) {
cout << "Printing student records in ascending order of IDs" << endl;
tree->print();
}
}
system("pause");
}

"BST File(Implementation file)"

#include "bst.h"

using namespace std;

bool bst::insert(string StudentName, int IDNumber) {


        class node * n= new node(StudentName,IDNumber);
        class node * parent = NULL;


        if(root == NULL)
                root = n;
        else
        {
               
               

                      // Need help here




//implement the function here
// you will need to create a new node with given student name and ID
// then traverse the tree (starting at the root) to find the right
// place to insert the node

// if a node with matching ID is found, return false to indicate failure

// if given ID is less than the ID of the node you are inspecting, move
// to the left subtree. If the given ID is greater, move to the right subtree

// end insert


Viewing all articles
Browse latest Browse all 3027