问题描述
您好,我正在尝试编写一种方法,该方法将访问二叉树中的所有节点,并在该节点处添加项目名称,以及项目的出现次数[此数据已正确入队]。这就是二叉树中的数据的样子...
当我运行打印命令时(在单独的主程序中……调用此命令……),我得到以下信息……
blow: 2
mm: 1
blow: 2
我一直在努力获得预期的结果,应该是...
blow:2
cc:1
ee:2
mm:1
nn:1
pp:2
oo:1
[因为oo是pp的孩子,所以我把它放在最后了]
这是我的代码...
//=========================================================================
/*
* This method returns a string of the items of the binary tree on a new line
*/
@Override
public String print()
{
// Placeholder,please update.
BT_node current=root;
// Placeholder,please update.
String result="";
// this is a string to hold the concatenation of the array elements
//go through the left nodes
while (current.leftChild != null)
{
if(current.leftChild!=null)
{
current = current.leftChild;
result= result + "\n"+current.getitem()+": "+current.getnumber() ;
if(current.leftChild==null)
{
current = current.rightChild ;
}//close if(current.leftChild==null)
}//close if(current.leftChild!=null)
else if (current.rightChild != null)
{
current = current.rightChild;
result= result + "\n"+current.getitem()+": "+current.getnumber() ;
if(current.rightChild==null)
{
break;
}//close if(current.leftChild==null)
}//close (current.rightChild != null)
}// close while (current.leftChild != null)
//System.out.println(result);
current=root;
result= result + "\n"+current.getitem()+": "+current.getnumber() ;
//current=root;
// go through the right nodes
while (current.rightChild != null)
{
if(current.leftChild!=null)
{
current = current.leftChild;
result= result + "\n"+current.getitem()+": "+current.getnumber() ;
if(current.leftChild==null)
{
break;
}//close if(current.leftChild==null)
}//close if(current.leftChild!=null)
else if (current.rightChild != null)
{
current = current.rightChild;
result= result + "\n"+current.getitem()+": "+current.getnumber() ;
if(current.rightChild==null)
{
break;
}//close if(current.leftChild==null)
}//close (current.rightChild != null)
}// close
//System.out.println(result);
//return new String();
return result;
} // end of OrderedPrint
这是二叉树节点类的代码。
public class BT_node
{
// define class variables
BT_node leftChild;
// these nodes hold values less that
BT_node rightChild;
String item;
// this is the name of the item being stored.
// It is the element
int number;
//This stores the number of occurences of the item
// this is the itemCount
//==========================================================================
/*
* class constructor
*/
public BT_node(String item,int number)
// this is the constructor used by BSTNode
{
this.item = item;
this.number = number;
this.leftChild = null;
// This stores values less than node value
this.rightChild = null;
// This stores valies greater than the node value
}//close public BTNode(String item,int number)
//==========================================================================
/*
* this returns the itement as a string
*/
public String toString()
{
return item.toString();
}// close public String toString()
//==========================================================================
/*
* this returns the itement
*/
public String getitem()
{
return item;
}// close public String getitem()
//==========================================================================
/*
* this sets the itement
*/
public void setitem(String item)
{
this.item = item;
}//close public void setitem(String item)
//==========================================================================
/*
* this gets the number of occurences
*/
public int getnumber()
{
return number;
}// close public int getnumber()
//==========================================================================
/*
* this this sets the number of occurnaces for the itemn
*/
public void setnumber(int number)
{
this.number = number;
}// close public void setnumber(int number)
//==========================================================================
/*
* this gets the leftChild
*/
public BT_node getLeftChild()
{
return leftChild;
}// close public BTNode getLeftChild()
//==========================================================================
/*
* this sets the leftChild
*/
public void setLeftChild(BT_node leftChild)
{
this.leftChild = leftChild;
}//close public void setLeftChild(BTNode leftChild)
//==========================================================================
/*
* this gets the rightChild
*/
public BT_node getRightChild()
{
return rightChild;
}//close public BTNode getRightChild()
//==========================================================================
/*
* this sets the rightChild
*/
public void setRightChild(BT_node rightChild)
{
this.rightChild = rightChild;
}//close public void setRightChild(BTNode rightChild)
}// close BT_node
我必须承认,我在递归方法上有点虚弱,如果我不必将字符串输出回调用程序,然后将其打印到控制台,则这样做会更容易...
我一直在努力使其一直沿一个节点递归,并将leftChild和rightChild节点项添加到“结果”字符串中...
任何对我的代码更改的建议都将不胜感激。
真诚的
解决方法
那么这可能更容易以DFS方式访问所有节点。伪代码如下所示:
HM: HashMap<String,Integer>(); // String -> name of the item,Integer -> occ. of item.
Function getNameAndOcc(root,HM):
IF (!root): /* when the root is empty,we will get back */
return HM;
ELSE:
String item = root.getItem(); /* extract item from node */
IF (HM->containsKey(item)): /* is item already tracked before */
HM->PUT(item,HM->GET(item)+1);
ELSE:
HM->PUT(item,1); /* the item first time tracking */
getNameAndOcc(root->left,HM); // go all nodes in left side
getNameAndOcc(root->right,HM); // go all nodes in right side
return HM; // we eventually visits all nodes and we will return the result.
想法紧随其后,我们将使用HashMap来跟踪每个项目及其发生。当我们处理一个空节点(空节点)时,通常会从遍历中返回。
我们将访问树的每个节点,并以DepthFirstSearch方式继续计数它的出现。