zoukankan      html  css  js  c++  java
  • 二叉树中序遍历java代码实现

    package binarytreesort;
    
    public class BinaryTree {
        private int value;
        private BinaryTree leftNode;
        private BinaryTree rightNode;
        
        public BinaryTree() {
            // TODO Auto-generated constructor stub
        }
        
        public BinaryTree(int value,BinaryTree leftNode,BinaryTree rightNode) {
            this.value = value;
            this.leftNode = leftNode;
            this.rightNode = rightNode;
        }
    
        public int getValue() {
            return value;
        }
    
        public void setValue(int value) {
            this.value = value;
        }
    
        public BinaryTree getLeftNode() {
            return leftNode;
        }
    
        public void setLeftNode(BinaryTree leftNode) {
            this.leftNode = leftNode;
        }
    
        public BinaryTree getRightNode() {
            return rightNode;
        }
    
        public void setRightNode(BinaryTree rightNode) {
            this.rightNode = rightNode;
        }
            
    }
    /**
     * 
     */
    package binarytreesort;
    
    /**
     * @author Administrator
     * 
     */
    public class BinaryTreeSort {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            BinaryTree binaryTree = new BinaryTree(10, new BinaryTree(8,
                    new BinaryTree(5, new BinaryTree(2, null, null),
                            new BinaryTree(6, null, null)), new BinaryTree(9, null,
                            null)), new BinaryTree(15, null, null));
            preOderTraversal(binaryTree," ");
            System.out.println("\n");
            inOrderTraversal(binaryTree);
        }
    
        public static void inOrderTraversal(BinaryTree binaryTree){
                if(binaryTree.getLeftNode()!=null){
                    inOrderTraversal(binaryTree.getLeftNode());
                }
                System.out.println(binaryTree.getValue());
                if(binaryTree.getRightNode()!=null){
                    inOrderTraversal(binaryTree.getRightNode());
                }
    
            }
        
        public static void preOderTraversal(BinaryTree binaryTree,String blankSpace){
            System.out.println(blankSpace+binaryTree.getValue());
            blankSpace +=" "+blankSpace;
            if(binaryTree.getLeftNode()!=null){
                preOderTraversal(binaryTree.getLeftNode(),blankSpace);
            }
            if (binaryTree.getRightNode()!=null){
                preOderTraversal(binaryTree.getRightNode(),blankSpace);
            }
        }
    }
  • 相关阅读:
    2016年3月3日
    性能测试之我解
    Vim命令合集
    vi-vim常用命令
    架构的本质是
    网站三层架构学习之一 分层式结构
    Spring 4 + Quartz 2.2.1 Scheduler Integration Example
    “城市民族工作条例”详解--建议废止
    字符串匹配处理
    LogBack简易教程
  • 原文地址:https://www.cnblogs.com/usual2013blog/p/3099784.html
Copyright © 2011-2022 走看看