C++ BST using nodes updated

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
Last edited on
Change your node constructor to either
1
2
3
4
5
6
7
	Node(int I, string N, string T) {
		ID = I;
		Name = N;
		Type = T;
		left = NULL;
		right = NULL;
	}

or
1
2
3
4
	Node(int ID, string Name, string Type) : ID(ID), Name(Name), Type(Type) {
		left = NULL;
		right = NULL;
	}

or change the whole Node struct to
1
2
3
4
5
6
7
8
9
struct Node {
	int ID;
	string Name;
	string Type;
	Node* right=NULL;
	Node* left=NULL;

	Node(int ID, string Name, string Type) : ID(ID), Name(Name), Type(Type) {}
};


I don't know which is to be preferred (although I would probably have gone with the last).

Any of these give:
123

ABCD

EFGH

123-ABCD(EFGH) 




For further consideration:-
(a) prompt the user for the required input: I had to read your code to see what to enter;
(b) use getline() rather than cin>> if any of your strings have spaces in;
(c) should this be a BINARY tree? It limits your number of subsections.
Last edited on
@lastchange Thank you!! couldnt figure it out for the life of me..
(a) yep, was planning on doing that, just wanted to make sure everything works prior to that
(b) thats a good point, I think I will.
(c) The task essentially: Explore tree data structure. Develop an example of a simplified file system demonstration. I think this is more of an excercise in understanding how the tree works, rather than full scale implementation of it, so Im not too worried about any subsections.

Thank you again!
How would I go about searching the structure (once data is added) for one specific instance based on its 'ID' and then printing its data, and any nodes data that are after it?

As well as removing a node based on its ID and any subsequent nodes that are linked to it?
The task essentially: Explore tree data structure. Develop an example of a simplified file system demonstration.


I'm not aware of any filesystem that acts like a binary tree (where a directory can have only two entries). For a filesystem, I think you want something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class FileSystemEntry {
    string name;
    FileSystem::Type type;
    vector<FileEntry> children;   // contents if it's a directory
};

class FileSystem {
public:
    enum Type {File, Dir};

    // create a file or directory named "name" in the parent directory "parentPath"
    // "type" tells whether the entry is a file or a directory
    create(const string &parentPath, const string &name, Type type);

    // list the contents of the directory "path".  Print "not a directory" if it's a file
    ls(ostream &os, const string &path);

    / remove the file or directory at path. Only remove directories if they are empty
    rm(const string &path);
};


Topic archived. No new replies allowed.