1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
"(Header File)"
#include <iostream>
#include <string>
using namespace std;
class node{
public:
int ID;
string name;
class node *left, *right, *parent;
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 *n1=new node("Master",10);
class node * parent=NULL;
if(root==NULL)
root=n1;
else
{
node*temp=root;
while(temp)
{
parent=temp;
if(n1->ID > temp->ID)
{
temp=temp->right;
return true;
}
else
{
temp=temp->left;
return true;
}
if(n1->ID < parent->ID)
{
parent->left=n1;
return true;
}
else
{
parent->right=n1;
return true;
}
}
}
//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
|