It’s for homework for my Data Structures class, and it’s in Java.
I have to make this little game sort of thing; the world is made up of a graph, and some nodes hold items that, when found, will be added to the user’s inventory, which is a binary search tree. (It has to be a binary search tree, or else this would be much easier.) I have most of it figured out, but I need the user to be able to see and access the contents of the inventory tree thing. The binary tree node class I’m provided has an inorderPrint() method that uses recursion to print everything out, but that only helps for showing them the contents, not giving them an easy way to access them. I want to have a method that returns an array of binary search tree nodes so I can do something like this in main…
int i = 0;
int choice;
System.out.println("# - Item\tPoints"); //Will appear as # - Item Points
for (BTNode node : inventory.treeAsArray()) {
System.out.printf("%d - %s\t%d\n", i, node.getData().getName(),
node.getData().getPoints()); //example: 1 - Poo 100
i++;
}
System.out.println("Select the number of the item you want to remove: ");
choice = keyboard.nextInt();
And then I’d loop through the array again and remove the item that corresponds with the number the user inputs. I don’t know how I can write a method that returns the contents of a binary search tree in the form of an array, though. That’s my main problem. I can’t really find an algorithm for it in my textbook.
If I was to do this I would probably just pass an array to all of the node in order.
and then have
You would need to add proper checks but that is a basic outline of the code needed.