I am in CSC 330 and we have a big project to create a 234 or 2-3-4 tree. I am currently working on my insert method. I have my while loop which moves through the tree but am stuck with a simple problem. When I am breaking up a 4-node how do I access the parent node, to give it my middle element? I have a 234 node that is an array that takes in an element, my element takes in an integer and a String. I understand that the best way to access a parent would be to make a method in my node class, but it baffles me on how to move backwards in a linked list.
Asked
Active
Viewed 342 times
0
-
also, how do we keep the children – user2925914 Oct 27 '13 at 20:18
-
There isn't a way to access the parent in any standard (non-custom) tree data structure. Trees use the loop invariable that they're sorted before insertion - then you only need to figure out the place to put that element in the tree and you don't have to change the rest of the tree. I suggest you implement your tree the same way. – nstosic Oct 27 '13 at 20:22
-
I see what you are saying, but you are not exactly answering the question. In a sense, before I insert, I have a while loop thats only job is to make sure the 4-nodes are broken up (following the rules of the tree) if there is no way to go backwards, should I write a method that finds the parents, traversing the entire tree again to access that node, or is there a different way that would have a faster run time and is smoother. – user2925914 Oct 27 '13 at 20:29
-
Could you clarify what you mean by "4-nodes are broken up" please? You should compare the children of a node, not the node themselves, that way you know that you're at the right place when a child returns null and there is no need to go upwards. – nstosic Oct 27 '13 at 20:32
-
Well before inserting in a 2-3-4, when finding where to insert, if you come across a 4 node it must me broken up... This is the trouble Im running into, when breaking up the four node, the middle element goes to the parent, and two other elements become their own node. – user2925914 Oct 27 '13 at 20:44