Home > Back-end >  Recursion is transferred in a JAVA reference problem
Recursion is transferred in a JAVA reference problem

Time:11-23

I's main function is as follows:
 public static void main (String [] args) {
Search_BTree tree2=null, pre=new Search_BTree ();
Tree2=Search_BTree. AutoCreate (3);
Tree2. CreateSearchBTreeEn (tree2, pre);
}



Search_BTree class is like this:
 public class Search_BTree {
Public Search_BTree Left;
Public Search_BTree Right;
Public String STR.
Public int ltag;//1 said toward the precursor or subsequent 0 means to children
Public int rtag;

//a clue binary
Public void createSearchBTreeEn (Search_BTree tree, Search_BTree pre) {
If (null==tree)
return ;
CreateSearchBTreeEn (tree) Left, pre);
If (null==tree. Left) {
Tree. Ltag=1;
Tree. Left=pre;
}
If (null==pre. Right) {
The pre. Rtag=1;
The pre. Right=tree;
}
The pre=tree;

CreateSearchBTreeEn (tree. Right, the pre);
}

//before sequence creates a binary tree traversal
Public static Search_BTree createSearchBTree (Search_BTree tree) {
String STR=new Scanner (System.) in the next ();
if(! STR. Equals (" # ")) {
The tree=new Search_BTree (STR);
Tree. Left=createSearchBTree (tree. Left);
Tree. Right=createSearchBTree (tree. Right);
Return the tree;
}
return null;
}

//to automatically create full binary tree
Public static Search_BTree autoCreate (int k) {
Int sum=(int) Math. Pow (2 k) - 1;
Int the hasChildren=(int) Math. Pow (2 k - 1) - 1;
Search_BTree list []=new Search_BTree [sum + 1];
for(int i=1; i<=sum; I++) {
The list [I]=new Search_BTree (String. The valueOf (I));//all assignment 1, 2, 3, 4...
}
for(int i=1; i<=the hasChildren; I++) {
The list [I] Left=list (2 * I];
The list [I] Right=list (2 * I + 1];
}
Return the list [1].
}

//level traversal
Public void Hierarchy (tree) Search_BTree {
Int current=1, the position=0;
ListIf (tree!=null) {
List. The add (tree);
While (current!={
the position)Current++;
If (list. Get (current). Left!=null) {
List. The add (list. Get (current). Left);
Position++;
}
If (list. Get (current). Right!=null) {
List. The add (list. Get (current). Right);
Position++;
}
}
For (Search_BTree node: list) {
System. The out. Println (node. STR);
}
}
}

//level traversal recursive
Public void HierarchyDG (Search_BTree tree, int k, List the List) {
If (tree!=null) {
If (k==1) {
List. The add (tree);
}
If (tree Left!=null) {
List. The add (tree. Left);
}
If (tree. Right!=null) {
List. The add (tree. Right);
}
If (tree Left!=null) {
HierarchyDG (tree. Left, k + 1, list);
}
If (tree. Right!=null) {
HierarchyDG (tree. Right, k + 1, list);
}
}
}


Public Search_BTree () {
}

Public Search_BTree (String STR) {
Enclosing the STR=STR;
}
}



The problem is that Search_BTree and createSearchBTreeEn method have a pre=tree; Can I inside the automatically created full binary tree, one step after debugging, found this sentence does not transfer reference, after the jump, the pre and turned back to the original value, the pre=tree here before changed the pointer pointing to useless,!!!!! ? This is why, for a great god help!!

CodePudding user response:

The above problem has been solved, I use a static variable set pre into a global static static variables,

Change a problem, I here two level traversal, non recursive method is right, can the level of the recursive traversal has a problem, I also know that there are problems, but I can't figure out how to write level recursive traversal,

CodePudding user response:

 
Import the Java. Util. ArrayList;
Import the Java. Util. List;
import java.util.Scanner;

Public class Search_BTree {
Public Search_BTree Left;
Public Search_BTree Right;
Public String STR.
Public int ltag;//1 said toward the precursor or subsequent 0 means to children
Public int rtag;

//a clue binary
Public void createSearchBTreeEn (Search_BTree tree, Search_BTree pre) {
If (null==tree)
return ;
CreateSearchBTreeEn (tree) Left, pre);
If (null==tree. Left) {
Tree. Ltag=1;
Tree. Left=pre;
}
If (null==pre. Right) {
The pre. Rtag=1;
The pre. Right=tree;
}
The pre=tree;

CreateSearchBTreeEn (tree. Right, the pre);
}

//before sequence creates a binary tree traversal
Public static Search_BTree createSearchBTree (Search_BTree tree) {
String STR=new Scanner (System.) in the next ();
if(! STR. Equals (" # ")) {
The tree=new Search_BTree (STR);
Tree. Left=createSearchBTree (tree. Left);
Tree. Right=createSearchBTree (tree. Right);
Return the tree;
}
return null;
}

//to automatically create full binary tree
Public static Search_BTree autoCreate (int k) {
Int sum=(int) Math. Pow (2 k) - 1;
Int the hasChildren=(int) Math. Pow (2 k - 1) - 1;
Search_BTree list []=new Search_BTree [sum + 1];
for(int i=1; i<=sum; I++) {
The list [I]=new Search_BTree (String. The valueOf (I));//all assignment 1, 2, 3, 4...
}
for(int i=1; i<=the hasChildren; I++) {
The list [I] Left=list (2 * I];
The list [I] Right=list (2 * I + 1];
}
Return the list [1].
}

//level traversal
Public void Hierarchy (tree) Search_BTree {
Int current=1, the position=0;
ListIf (tree!=null) {
List. The add (tree);
While (current!={
the position)Current++;
If (list. Get (current). Left!=null) {
List. The add (list. Get (current). Left);
Position++;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related