• 二叉树(前序,中序,后序遍历)查找


    二叉树(前序,中序,后序遍历)查找

    树的结点

    /**
     * 树的结点
     */
    public class TreeNode {
        private Object obj;
        private TreeNode leftNode;
        private TreeNode rightNode;
    
        public TreeNode(Object obj) {
            this.obj = obj;
            this.leftNode=null;
            this.rightNode=null;
        }
    
        public Object getObj() {
            return obj;
        }
    
        public void setObj(Object obj) {
            this.obj = obj;
        }
    
        public TreeNode getLeftNode() {
            return leftNode;
        }
    
        public void setLeftNode(TreeNode leftNode) {
            this.leftNode = leftNode;
        }
    
        public TreeNode getRightNode() {
            return rightNode;
        }
    
        public void setRightNode(TreeNode rightNode) {
            this.rightNode = rightNode;
        }
    }
    

    二叉树

    public class BinaryTree {
        private TreeNode root;
        private TreeNode searchNode;
        Scanner scanner = new Scanner(System.in);
    
    
        public BinaryTree() {
            System.out.println("输入!~结束输入");
            this.inputTree(null);
        }
    
        public TreeNode getRoot() {
            return root;
        }
    
        public void setRoot(TreeNode root) {
            this.root = root;
        }
    
        public void inputTree(TreeNode node) {
            if (this.root == null) {
                System.out.print("请输入根节点:");
                String next = scanner.next();
                this.root = new TreeNode(next);
                inputTree(this.root);
                return;
            }
            System.out.print("请输入" + node.getObj() + "的左孩子节点:");
            String nextL = scanner.next();
            if (!"!~".equals(nextL)) {
                node.setLeftNode(new TreeNode(nextL));
                inputTree(node.getLeftNode());
            }
    
    
            System.out.print("请输入" + node.getObj() + "的右孩子节点:");
            String nextR = scanner.next();
            if (!"!~".equals(nextR)) {
                node.setRightNode(new TreeNode(nextR));
                inputTree(node.getRightNode());
    
            }
    
        }
    
    
        /**
         * 前序遍历
         */
        public void preorderTraversal(TreeNode node) {
            if (node == null) return;
            else {
                System.out.print(node.getObj() + " ");
                preorderTraversal(node.getLeftNode());
                preorderTraversal(node.getRightNode());
            }
        }
    
        /**
         * 中序遍历
         */
        public void inOrderTraversal(TreeNode node) {
            if (node == null) return;
            else {
                inOrderTraversal(node.getLeftNode());
                System.out.print(node.getObj() + " ");
                inOrderTraversal(node.getRightNode());
            }
        }
    
        /**
         * 后序遍历
         */
        public void postOrderTraversal(TreeNode node) {
            if (node == null) return;
            else {
    
                postOrderTraversal(node.getLeftNode());
                postOrderTraversal(node.getRightNode());
                System.out.print(node.getObj() + " ");
            }
        }
    
         /**
         * 查找
         */
        public TreeNode searchNode(Object object){
            preorderSearch(this.root,object);
            return searchNode;
        }
    
         /**
         * 用于查找遍历
         */
        public void preorderSearch(TreeNode node,Object object){
          if (searchNode!=null||node==null) return ;
          else  {
              if (node.getObj().equals(object)) this.searchNode=node;
              preorderSearch(node.getLeftNode(),object);
              preorderSearch(node.getRightNode(),object);
    
          }
    
    
        }
    }
    
    

  • 相关阅读:
    报表开发之扩展GROUP BY
    Leetcode--easy系列9
    datatable 前台和后台数据格式
    C实现头插法和尾插法来构建单链表(带头结点)
    HDOJ 题目1520 Anniversary party(树形dp)
    windows环境利用apache 配置虚拟主机
    POJ--1966--Cable TV Network【无向图顶点连通度】
    Linux Shell脚本编程学习笔记和实战
    win10 bcdedit加入vhdx启动
    设计模式之Mediator模式(笔记)
  • 原文地址:https://www.cnblogs.com/huangshen/p/13362293.html
Copyright © 2020-2023  润新知