java之二叉树的创建和递归遍历

每一个节点

package tree; 
/** 
 * @author wangpei 
 * @version 
 *创建时间:2017年3月5日 上午11:10:34 
 * 树节点
 */
public class TreeNode {
    protected char data;
    protected TreeNode Lchild;
    protected TreeNode Rchild;
    public TreeNode(char data){
        this.data=data;

    }


    public char getData() {
        return data;
    }
    public void setData(char data) {
        this.data = data;
    }
    public TreeNode getLchild() {
        return Lchild;
    }
    public void setLchild(TreeNode lchild) {
        Lchild = lchild;
    }
    public TreeNode getRchild() {
        return Rchild;
    }
    public void setRchild(TreeNode rchild) {
        Rchild = rchild;
    }



}

树节点:

package tree; 
/** 
 * @author wangpei 
 * @version 
 *创建时间:2017年3月5日 上午11:11:54 
 * 递归树节点遍历(先,中,后)
 */
public class TreeExercise {
    protected TreeNode root; // 树的根节点
    protected int size; // 树长度
    protected char[] data; // 树的数据
    public TreeExercise(char[] data) {
        this.data = data;
        size = data.length;
        root = createTree(0);
    }

    public TreeNode createTree(int index) { // 采用递归生成二叉树
        if (index >= size)
            return null;
        if (data[index] == '0')
            return null;
        TreeNode node = new TreeNode(data[index]);
        node.setLchild(createTree(2 * index + 1));
        node.setRchild(createTree(2 * index + 2));
    return node;
    }
    /*先序递归遍历二叉树*/
    public void PrintOrder(TreeNode root){
        if(root!=null){
        System.out.print(root.getData()+"..");
         PrintOrder(root.Lchild);
         PrintOrder(root.Rchild);
        }
    }
    /*中序遍历二叉树*/
    public void InOrder(TreeNode root){
        if(root!=null){

         InOrder(root.Lchild);
         System.out.print(root.getData()+"..");
         InOrder(root.Rchild);
        }
    }
    /*后续遍历二叉树*/
    public void PostOrder(TreeNode root){
        if(root!=null){

         PostOrder(root.Lchild);
         PostOrder(root.Rchild);
         System.out.print(root.data+"..");

        }
    }


    /*输出二叉树的值*/
    public static void print(TreeNode root){
        System.out.println(root.data);

    }
    public static void main(String[] args) {
         char[] chars = new char[] {'A', 'B', 'C', '0', 'D', 'E', 'F','0','0','G','0','0','0','H','0'};
        TreeExercise ec=new TreeExercise(chars);

        ec.PostOrder(ec.getRoot());
    //  System.out.println(ec.getRoot().data);
    //  System.out.println(root.Lchild.data);



    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 我可以提供一些参考代码,但是最终的实现取决于你的应用场景。首先,需要为二叉树定义一个类,然后实现一个非递归遍历方法: public class BinaryTree { Node root; // 非递归遍历 public void traverseWithoutRecursion(){ Stack<Node> stack = new Stack<>(); Node currentNode = root; while(currentNode != null || !stack.isEmpty()){ // 向左遍历 while(currentNode != null){ stack.push(currentNode); currentNode = currentNode.left; } // 处理右子树 if(!stack.isEmpty()){ currentNode = stack.pop(); // 处理当前节点,如打印等 System.out.print(currentNode.data + " "); currentNode = currentNode.right; } } } } ### 回答2: 要用Java写一个二叉树的非递归遍历,我们可以使用栈来辅助实现。 首先,需要先定义一个二叉树的节点类,包含节点值、左子节点和右子节点。代码如下: ```java class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } } ``` 然后,我们可以使用迭代的方式来实现二叉树的非递归遍历。这里以中序遍历为例: ```java import java.util.Stack; public class BinaryTreeTraversal { public static void inorderTraversal(TreeNode root) { if (root == null) return; Stack<TreeNode> stack = new Stack<>(); TreeNode current = root; while (current != null || !stack.isEmpty()) { while (current != null) { stack.push(current); current = current.left; } current = stack.pop(); System.out.print(current.val + " "); current = current.right; } } public static void main(String[] args) { /* * 构建二叉树: * 1 * / \ * 2 3 * / \ * 4 5 */ TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); System.out.println("中序遍历结果:"); inorderTraversal(root); } } ``` 上述代码中,我们使用一个while循环来依次将左子节点压入栈中,直到左节点为空。然后出栈一个节点,并打印其值,再访问右子节点。重复这个过程,直到遍历完所有节点。 测试代码中先构建了一个二叉树。然后调用inorderTraversal方法进行中序遍历,并输出结果。 以上就是用Java写一个二叉树的非递归遍历的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值