原谅我,如果它之前问过,但我找不到合适的答案。在将使用递归方法编写的二进制搜索树遍历转换为非递归方法时,我遇到了一个主要问题。请帮助我。这是在java中使用递归方法的代码。
// BinarySt类
public interface BinaryST {
public void insert(Integer nData);
public Integer remove(Integer nData);
public boolean isExist(Integer nData);
public String preOrder();
public String inOrder();
public String postOrder();
}
// BinaryTreeNode类
public class BinaryTreeNode {
Integer element;
BinaryTreeNode LChild;
BinaryTreeNode RChild;
BinaryTreeNode() {}
}
// BinarySearchTree实现了BinaryST类
public class BinarySearchTree implements BinaryST{
protected StringBuffer s;
protected BinaryTreeNode root;
public BinarySearchTree() {}
public BinarySearchTree(Integer nData)
{
root = new BinaryTreeNode();
root.element = nData;
}
public void insert(Integer nData)
{
if( root==null ) {
root = new BinaryTreeNode();
root.element = nData;
return;
}
boolean flag=true;
BinaryTreeNode c=root;
while(flag) {
if( nData>=c.element ) {
if( c.RChild==null ) {
c.RChild = new BinaryTreeNode();
c.RChild.element = nData;
flag = false;
}
c = c.RChild;
}
else {
if( c.LChild==null ) {
c.LChild = new BinaryTreeNode();
c.LChild.element = nData;
flag = false;
}
c = c.LChild;
}
}
}
public Integer remove(Integer nData)
{
return new Integer(5);
}
public boolean isExist(Integer nData)
{
return true;
}
public String preOrder()
{
if( root==null ) {
return new String( "null" );
}
s = new StringBuffer("");
prOrder( root );
return new String(s);
}
public String inOrder()
{
if( root==null ) {
return new String( "null" );
}
s = new StringBuffer("");
iOrder( root );
return new String(s);
}
public String postOrder()
{
if( root==null ) {
return new String( "null" );
}
s = new StringBuffer("");
poOrder( root );
return new String(s);
}
private void prOrder(BinaryTreeNode t) {
if( t!=null ) {
s.append( t.element.toString() + " " );
prOrder( t.LChild );
prOrder( t.RChild );
}
}
private void iOrder(BinaryTreeNode t) {
if( t!=null ) {
iOrder( t.LChild );
s.append( t.element.toString() + " " );
iOrder( t.RChild );
}
}
private void poOrder(BinaryTreeNode t) {
if( t!=null ) {
poOrder( t.LChild );
poOrder( t.RChild );
s.append( t.element.toString() + " " );
}
}
}
// Runner class
public class Runner {
public static void main(String[] args) {
BinarySearchTree a = new BinarySearchTree();
System.out.println( "Pre-Order : "+a.preOrder() );
System.out.println( "In-Order : "+a.inOrder() );
System.out.println( "Post-Order: "+a.postOrder() );
a.insert( (Integer) 100 );
a.insert( (Integer) 50 );
a.insert( (Integer) 150 );
a.insert( (Integer) 25 );
a.insert( (Integer) 75 );
a.insert( (Integer) 125 );
a.insert( (Integer) 175 );
a.insert( (Integer) 60 );
a.insert( (Integer) 160 );
a.insert( (Integer) 200 );
a.insert( (Integer) 155 );
System.out.println( "Pre-Order : "+a.preOrder() );
System.out.println( "In-Order : "+a.inOrder() );
System.out.println( "Post-Order: "+a.postOrder() );
}
}
答案 0 :(得分:0)
这是预订的代码。使用此并转换为后序和顺序
private void prOrder(BinaryTreeNode t) {
if( t!=null ) {
Stack<BinaryTreeNode> rootStack=new Stack<BinaryTreeNode>();
BinaryTreeNode current=t;
do{
s.append( current.element.toString() + " " );
if(current.RChild!=null)
rootStack.add(current.RChild);
if(current.LChild!=null)
rootStack.add(current.LChild);
if(rootStack.isEmpty())
current=null;
else
current=rootStack.pop();
}while(current!=null);
}
}
答案 1 :(得分:0)
让我们考虑二叉树节点:
public class BinaryTreeNode {
private int data;
private BinaryTreeNode left;
private BinaryTreeNode right;
public BinaryTreeNode() {
}
public BinaryTreeNode(int data) {
this.data=data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}
}
非重复邮寄订单:
public static void nonRecursivePostOrder(BinaryTreeNode root){
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
List<BinaryTreeNode> returnNodes = new LinkedList<BinaryTreeNode>();
while (true) {
if (root != null) {
if (returnNodes.contains(root)) {
returnNodes.add(stack.pop());
root = null;
} else {
stack.push(root);
root = root.getLeft();
}
} else {
if (stack.isEmpty()) {
break;
} else if (stack.peek().getRight() == null) {
root = stack.pop();
returnNodes.add(root);
if (root == stack.peek().getRight()) {
returnNodes.add(stack.pop());
}
}
if (!stack.isEmpty()) {
root = stack.peek().getRight();
} else {
root = null;
}
}
}
for(BinaryTreeNode node : returnNodes)
System.out.print(node.getData()+" ");
}
Non Reusive inorder Travesal:
public static void nonRecursiveInOrder(BinaryTreeNode root){
if(root == null)return;
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
while (true) {
if(root.getLeft()!=null){
stack.push(root);
root = root.getLeft();
}else if(root.getRight()!=null){
System.out.print(root.getData()+" ");
root = root.getRight();
}else{
System.out.print(root.getData()+" ");
if(stack.isEmpty())
break;
root = stack.pop();
System.out.print(root.getData()+" ");
root = root.getRight();
}
}
}
非递归PreOrder遍历:
public static void nonRecursivePreOrder(BinaryTreeNode root){
if(root == null)return;
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
while (true) {
if(root.getLeft()!=null){
stack.push(root);
System.out.print(root.getData()+" ");
root = root.getLeft();
}else if(root.getRight()!=null){
System.out.print(root.getData()+" ");
root = root.getRight();
}else{
System.out.print(root.getData()+" ");
if(stack.isEmpty())
break;
root = stack.pop();
root = root.getRight();
}
}
}