Hi,
Im currently working on an assignment where we are tasked with creating a Binary tree structure program, in which, we can illustrate a typical folder architecture.
i.e.:
1 - Root
1.1 - Folder 1 (Folder)
1.1.1 - MapOfTheCity (File)
1.1.2 - Document (File)
1.2 - Contract1 (File)
(I hope this is understandable)..
But this BST has to be able to store these attributes: Unique Node ID (int), highest node ID (int), Node Name (Folder, or Contract1) and Node Type (e.g. folder/file).
With the functions to add a new node by entering the required fields.
Delete a node based on the entered ID.
Display the whole BST
Im struggling with the implementation side of it, I thought I had it figured out but clearly Im doing something wrong.
Whenever I run my code, it doesnt print what I would expect it to - instead of the entered data, it prints a address and thats it.
Im not sure what Im doing wrong, and was hoping someone could point it out?
(Also, Im not even sure I set up the whole BST correctly).
Code so far:
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
|
#include <iostream>
using namespace std;
//Structure to create the nodes and links
struct Node {
int ID;
string Name;//Name of the node - i.e. Documents
string Type;//Node type e.g. Folder, File
Node* right;//Points to right side of node
Node* left;//Points to left side of node
Node(int ID, string Name, string Type) { //initial node (ROOT)
ID = ID;
Name = Name;
Type = Type;
left = NULL;
right = NULL;
}
};//end struct Node
//Method to COUNT ALL THE NODES
int countNodes(Node* root) {
//Count nodes in binary tree and return answer
if (root == NULL)
return 0;
else {
int count = 1; //Start by counting the root
count += countNodes(root->left); //add nodes on the left subtree
count += countNodes(root->right);//add nodes on the right subtree
return count; //return the total
}
}//end countNodes()
//Method to PRINT THE NODE (TREE)
void preorderPrint(Node* root) {
//Print all items in the tree - root is printed first - followed by left, then right subtree
if (root!= NULL) {
preorderPrint(root->left); //print items in left subtree
preorderPrint(root->right); //print items in right subtree
cout << root->ID << "-" << root->Name << "(" << root->Type << ")"; //print the root item
}
}//end of preorder print()
//Method to add data to structure
void treeInsert(Node*& root, int ID, string Name, string Type) {
// Add the item to the binary sort tree to which the parameter
// "root" refers. Note that root is passed by reference since
// its value can change in the case where the tree is empty.
if (root == NULL) {
// The tree is empty. Set root to point to a new node containing
// the new item. This becomes the only node in the tree.
root = new Node(ID, Name, Type);
// NOTE: The left and right subtrees of root
// are automatically set to NULL by the constructor.
// This is important!
return;
}
else if (ID < root->ID) {
treeInsert(root->left, ID, Name, Type);
}
else {
treeInsert(root->right, ID, Name, Type);
}
} // end treeInsert()
int main()
{
Node* root; //Pointer to the root node in the tree
root = NULL; //Start with an empty tree
int tempID;
string tempName, tempType;
cin >> tempID; //To enter the ID
cout << endl;
cin >> tempName; //to enter the name
cout << endl;
cin >> tempType; //to enter the type (later will be limited to folder/file)
cout << endl;
treeInsert(root, tempID, tempName, tempType);
preorderPrint(root); //I assume Im doing something wrong here, but I cant figure it out...
return 0;
}
|
Any help would be appreciated.
Thank you