binary search tree - Getting an object from a recursive deletion Java -
I have a binary search tree that contains nodes. Each node has a significant value and data value, and the nodes According to the key, I am trying to write a method to remove an object from my BST. Here's the code:
Remove the public object (comparative theKey) {Remove the return (theKey, rootPtr) .data; } Removing the public node (comparative key, node node) {Object o; If (node == zero) returns node; If (theKey.compareTo (node.key) & lt; 0) {// go to the left sub-section node. LeftChild = remove (theKey, node.leftChild); } And if (theKey.compareTo (node.key)> 0) {// Go to the correct sub-node node. RightChild = remove (theKey, node.rightChild); } And if (theKey.compareTo (node.key) == 0) {if (node.bumbled! = Null & node. Writebill! = Null) {node foundNode = findMin (node.rightChild); Node.key = foundNode.key; Node.data = foundNode.data; Node.rightChild = remove (node.key, node.rightChild); } Else {if (node.leftChild! = Null) {node = node.leftChild; } And {node = node. Wright Shield; }}} NumNodes--; Return node; }
I want to return data values related to the DELETED node. The issue I have is that: In removing public objects () method, is the return value always the data value of the root node? I believe this will happen because the last method will be referred by another method to the root pTR (the root pointers). If this is the case, how can I save data from a deleted node?
The simple solution seems to add an output parameter back to the result:
< Pre> remove public objects (comparative theKey) {object [] result = new object [1]; RootPtr = Remove (theKey, rootPtr, results); // Decision to remove one-node tree return result [0]; } Remove the public node (comparative pinnacle, node node, object [] result) {if (node == faucet) {return node; } Int diff = theKey.compareTo (node.key); If (difference <0) {node.leftChild = Remove (theKey, node.leftChild, results); } And if (diff> 0) {node.rightChild = remove (theKey, node.rightChild, results); } And {result [0] = node.kee; If (node.rightChild == null) {node = node.leftChild; } And if (node. Left = Chald ==) {node = node. Wright Shield; } Else {node foundNode = findMin (node.rightChild); Node.key = foundNode.key; Node.data = foundNode.data; Node.rightChild = remove (node, node, right shield, new object [1]); } NumNodes--; } Return node; } Returning the found node does not work without significant changes because the return parameter is used to change the nodes as needed, and in that case where the node has two children, you can It is necessary to make a copy or insert a new node to handle the case where the root node is removed, it will also be an issue. ps I'm assuming this is not a "trick question" and you can not just return - which is equal to the key found after all :)
Comments
Post a Comment