The logic is simple - after the end of the post order, rotate the root and then the null nodes. Below is a code that I have written to remove all the nodes of a tree (i.e., removing binary trees).
Problem: The actual tree has not been destroyed. I mean that the DeleteTree (BTNode Route) function is not just all the values of the blank ref but the head. Tree.deleteTree (); Tree.preorder (); - It still prints all the values of a tree
.deleteTree (even after tree), it prints all the nodes in a tree.
Can anyone help me with the code in the code?
Note : There is no error in the insert and preorder work, so you can only focus on deleteTree () code
package com. Import java.util.LinkedList; Public Binary Binaries {Private BTnode Head; Enter public zeros (int data) {btnode n = new btnode (data); Btnode temp = null; If (head == empty) {head = n; Return; } And (linked list) q = new linked list (); Q.addLast (head); // ANK while (! Q.isEmpty ()) {temp = (BTNode) q.removeFirst (); If (temp.getLeft () == faucet) {temp.setLeft (n); Return; } Else {// enque q.addLast (temp.getLeft ()); } If (temp.getRight () == faucet) {temp.setRight (n); Return; } Else {// enque q.addlast (temp.getRight ()); }} // Where the loop ends here}} // is inserted, ends here Public void preorder () {preorder (head); } Private Worth Preder (BTnode Head) {if (head == null) {return; } And {System.out.println (head.getData ()); \ Preorder (head.getLeft ()); Preorder (head.getRight ()); }} Public Zero Deletion () {deleteTreeInternal (this.head); } Private zero removal (BTND root) {if (root == null) {return; } And {deleteTreeInternal (root.getLeft ()); DeleteTreeInternal (root.getRight ()); Root = null; }}} // class ends here
package com Public Sector BTND {Private BTNode left; Private BTnode Correct; Private int data; Public BTnode () {} Public BTnode (Int Data) {this.data = data; This.left = Null; This.right = Null; } Public BTNode getLeft () {return left; } Public Zero Set Lift (BTNode Left) {this.left = left; } Public BTnode getRight () {Return correct; } Public Zero Set Wright (BTnode Correct) {this.right = right; } Public int getData () {return data; } Public Zero Set Data (Int Data) {this.data = data; }}
simply root = zero;
C ++ does not accumulate garbage, but Java is in Java, once the object no longer has a reference, it will be automatically removed from memory. All referenced objects in the garbage collected objects will also be removed, if they have no other reference for them. That bit addresses your question: If the nodes under the root have no external reference, then they will automatically be removed after the root.
In your deleteTree ()
function, code> head = zero; // nothing else
Remove the other DeleteAttri (BTnode Route)
function, no use of it.
No comments:
Post a Comment