• 普通二叉树操作


      所谓普通二叉树,也就是排序二叉树,对于任何一个节点,它的左子树比自己大,右子树比自己小,不保证平衡因子的范围,其实这是最基础的二叉树,你也可以

    增加他的搜索方法,就成了搜索二叉树。常见操作有构建排序二叉树、获取最大值、最小值、获取所有叶子节点、层级遍历、前序遍历、后序遍历、中序遍历、添加对象数组、添加元素、统计、叶子节点个数删除元素、插入、翻转、判断是不是二叉搜索树、求深度、判空等。

    package NewTree;
    
    import java.time.format.ResolverStyle;
    import java.util.LinkedList;
    import java.util.Queue;
    
    import javax.print.attribute.standard.NumberOfDocuments;
    
    public class Tree {
        Node<Integer> root;
        
        public Tree(int[] t) throws Exception {
            for(int i=0;i<t.length;i++) {
                add(t[i]);
            }
        }
        public Tree() {    }
        public boolean search(Node<Integer> node,int c) {
            if(c==node.data) {
                return true;
            }else if(c>node.data) {
                search(node.right, c);
            }else {
                search(node.left, c);
            }
            return false;
        }
    //    统计叶子节点的个数
        public  int leafNum(Node node) {
            if (node != null) {
                if (node.left == null && node.right == null) {
                    return 1;
                }
                return leafNum(node.left) + leafNum(node.right);
            }
            return 0;
        }
    
        
        public void add(int t) throws Exception {
            Node<Integer> current = root;
            Node<Integer> node = new Node<Integer>(t);
            if(current==null) {
                root = new Node<>(t);
                return ;
            }
            while(current!=null) {
                if(current.data>t) {
                    if(current.left==null) {
                        current.left = node;
                       return ;
                    }
                    else {
                        current = current.left;
                    }
                }else if(current.data<t){
                    if(current.right==null)
                    {
                        current.right = node;
                    return ;
                    }
                    else {
                        current = current.right;
                    }
                }else {
                    throw new Exception("不允许出现相同的元素");
                }
            }
            
        }
        public int getmax() {
            return getmax(root);
        }
        public int  getmax(Node<Integer> node) {
            if(node.right==null) {
                return node.data;
            }
            return  getmax(node.right);
        }
        
        public void insert(int t) {
            root = insert(root,t);
        }
        public Node<Integer> insert(Node<Integer> node,int t) {
            if(node==null) {
                return new Node<Integer>(t);
            }if(t<node.data) {
                node.right = insert(node.right, t);
            }else if(t>node.data) {
                node.left = insert(node.left, t);
            }
            return node;
        }
        public boolean isEmpey() {
            return root == null;
        }
    //    中序遍历
        public void inorder(Node<Integer> node) {
            if(node!=null) {
                inorder(node.left);
                System.out.println(node.data);
                inorder(node.right);
            }
        }
    
        public void beforeorder(Node<Integer> node) {
            if(node!=null) {
                System.out.println(node.data);
                beforeorder(node.left);
                beforeorder(node.right);
            }
        }
    //    后序遍历
        public void behindeorder(Node<Integer> node) {
            if(node!=null) {
                behindeorder(node.left);
                behindeorder(node.right);
                System.out.println(node.data);
            }
        }
        public void reverse(Node<Integer> node) {
            if(node==null) {
                return;
            }
            Node<Integer> current ;
            current  = node.left;
            node.left = node.right;
            node.right = current;
            reverse(node.left);
            reverse(node.right);
        }
    //    求二叉树的深度
        public static int getDepthRec(Node root) {
               if (root == null) {
                   return 0;
               }
               return Math.max(getDepthRec(root.left), getDepthRec(root.right)) + 1;
            }
    //    判断是不是二叉搜索树
        public static boolean isValidBST(Node<Integer> root, int pre){
            if (root == null) {
                return true;
            }
            boolean left = isValidBST(root.left, pre);
            if (!left) {
                return false;
            }
            if(root.data <= pre) {
                return false;
            }
            pre = root.data;
            boolean right = isValidBST(root.right, pre);
            if(!right) {
                return false;
            }
            return true;
        }
        public static void main(String[] args) throws Exception {
            int[] a = new int[]{18,4,5,3,90,9,56};
             Tree tree=  new Tree(a);
             System.out.println("输出叶子节点的个数:"+tree.leafNum(tree.root));
             System.out.println("输出二叉树的深度:"+tree.leafNum(tree.root));
             System.out.println("最大值: "+tree.getmax());
    //         System.out.println("中序遍历");
    //        tree.inorder(tree.root);
    //        System.out.println("前序遍历");
    //        tree.beforeorder(tree.root);
    //        System.out.println("后序遍历");
    //        tree.behindeorder(tree.root);
    //        System.out.println("中序遍历翻转后的二叉树");
    //        tree.reverse(tree.root);
    //       tree.inorder(tree.root);
           System.out.println("是平衡树吗:"+tree.isValidBST(tree.root, tree.root.left.data));
           
           System.out.println("层序遍历:");
           Queue<Node<Integer>> queue = new LinkedList<>();
           queue.add(tree.root);
           while(!queue.isEmpty()) {
               Node<Integer> temp = queue.poll();
               System.out.printf(temp.data + "  ");
               if(temp.left!=null) {
                   queue.add(temp.left);
               }
               if(temp.right!=null) {
                   queue.add(temp.right);
               }
           }
        }
        
    }

        今天先立个flag,下一篇,再写个平衡树的API,全面探究下平衡树的操作。

    好好生活,天天向上
  • 相关阅读:
    Ubuntu16安装chrome
    Ubuntu桌面消失
    Keras读取保存的模型时, 产生错误[ValueError: Unknown activation function:relu6]
    MATLAB字符串分解, 合并
    Pycharm+任务栏悬浮+docked mode
    tfrecords转np.array
    TensorFlow+restore读取模型
    tfrecords转图片存储
    Lenet车牌号字符识别+保存模型
    spring boot日志配置
  • 原文地址:https://www.cnblogs.com/linchongatfirst/p/9527248.html
Copyright © 2020-2023  润新知