-1

I have an ordered binary search tree by name, but im trying to delete all students who have a score less than 50. Im having difficulty because the tree is not ordered by scores, rather its ordered by name, but i need to delete by scores. I cant for the life of god seem to be able to incorporate a traversal + node deletion without getting errors or it just plainly not working. Could someone set me in the correct direction? I couldnt find any threads where they were deleting by the variable that the tree is not ordered in and couldnt find any online.

Thank you.

  • May be, you should separate the task into sub-tasks and solve them one by one. 1st.: traverse tree to pass every node, 2nd.: inspect the current node, 3rd.: remove the node if intended. If would use `std::map` (which uses a tree internally). To solve 1st, just iterate from `std::map::begin()` to `std::map::end`. To solve 2nd, check `second` of the iterator. To solve 3rd, use `std::map::erase()`. Erasing an element from map invalidates the iterator but `erase()` returns the succeeder which can be used to continue iteration. If you may not use `std::map` you can at least get the idea from it. – Scheff's Cat Nov 01 '17 at 11:52

1 Answers1

0

This is probably best done in two passes. In the first pass, initialize an empty list of nodes. Then, do an inorder traversal of the tree and store in the list a reference to each node that needs to be deleted.

In the second pass, go through the list that you created in the first pass, and delete each node, in turn.

You can also do this in a single pass by creating a new tree. Then, do an inorder traversal of the original tree, copying all of the nodes that have a score greater than or equal to 50 to the new tree. When you're done, delete the old tree.

In general, the first method will be faster if there are few students with grades less than 50. However, if more than half of the students have scores less than 50, then the second method could very well be faster.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Hi, thanks for the comment. I like your first suggestion as the trees have to maintain the same format. During the inorder traversal, how would i be able to make it so that each node is stored. I was thinking of a global variable array of nodes thats been initialised to a huge size (500) and i would just have a function inside the inorder traversal that adds the pointer every time it is less than 50. Whats your thoughts on this? – user421546 Nov 01 '17 at 13:33
  • @user421546: Two options come to mind. The first is a `std::vector`, although perhaps you haven't covered that one yet. The second is a linked list. You can just add each item to the head of the list as you find it. Your fixed array could work, as long as you're certain that it will be large enough. If you wanted to be sure, you could do a pass to count how many records will be deleted, then allocate the array and do another pass of the tree to copy those records to the array. – Jim Mischel Nov 01 '17 at 13:47