美文网首页
Java 二叉树

Java 二叉树

作者: 咪啊p | 来源:发表于2019-11-12 13:52 被阅读0次

创建一个二叉树对象

 private static class Node{
        int data;
        Node leftNode;
        Node righNode;
        public Node(int data, Node leftNode, Node righNode) {
            super();
            this.data = data;
            this.leftNode = leftNode;
            this.righNode = righNode;
        }
        public int getData() {
            return data;
        }
        public void setData(int data) {
            this.data = data;
        }
        public Node getLeftNode() {
            return leftNode;
        }
        public void setLeftNode(Node leftNode) {
            this.leftNode = leftNode;
        }
        public Node getRighNode() {
            return righNode;
        }
        public void setRighNode(Node righNode) {
            this.righNode = righNode;
        }
        
    }

build 一个二叉树

private static Node rootNode;
public static void buildTree(int[] A){
    for(int i:A){
        rootNode = insertData(rootNode,i);
    }
}

public static void insertData(Node tree, int a){
    if (tree == null){
          return new Node(a, 0, 0);
    }
  
  if (a>tree.getData()){
      return tree.setRightNode(insertData(tree.getRightNode(), a));
  } else {
      return tree.setLeftNode(insertData(tree.getLeftNode(),a));
  }
}

遍历

先序遍历

public static void preOrder(Node node){
    if (node!=null){
        System.out.println(node.getData());
        preOrder(node.getLeftNode());
        preOrder(node.getRightNode());
    }
}

后序遍历

public static void postOrder(Node node){
    if (node != null ){
        postOrder(node.getLeftNode());
        postOrder(node.getRightNode());
       System.out.println(node.getData());
    }
}

中序遍历

public static void inOrder(Node node){
    if (node!=null){
          inOrder(node.getLeftNode());
          System.out.println(node.getData());
          inOrder(node.getRightNode());
    }
}
image.png

先序遍历的结果为:0 1 3 7 4 2 5 6

中序遍历的结果为:7 3 1 4 0 5 2 6

后序遍历的结果为:7 3 4 1 5 6 2 0

计算深度

计算深度-递归, 依次计算其左右子树的深度,选其大者

 public static int deep(Node node) {
    if (node !=null){
      int leftDeep= 1, rightDeep=1;
      if (node.getLeftNode()!=null)
          leftDeep=leftDeep+deep(node.getLeftNode());

     if (node.getRightNode() !=null)
        rightDeep=rightDeep+deep(node.getRightNode());

      return Math.max(leftDeep, rightDeep);

    }
}

插入

   public Node insert(Node root, int key){

        Node newNode =new Node(key,null,null);

        Node current = root;

        Node parent = null;

        
        if (current == null){ 
            root = newNode;
            return newNode;
        }

        while (true)
        {
            parent = current;

            if (key < current.getData()){

                current = current.getLeftNode();

                if (current == null)

                {               
                    parent.setLeftNode(newNode);

                    return newNode;

                }

            }else {

                current = current.getRighNode();

                if (current ==null){

                    parent.setRighNode( newNode);

                    return newNode;

                }

            }

        }

    }

删除

相关文章

  • JavaScript和树(二叉树)

    二叉树的概念:https://segmentfault.com/a/1190000000740261java二叉树...

  • 树 - 实现二叉排序树(Java)

    链表实现二叉树的类型声明(Java): 二叉树的构建 调用(Kotlin写法): 二叉树构建过程分解:

  • Java针对二叉树的几种遍历方式

    Java针对二叉树的几种遍历方式

  • 力扣算法 - 翻转二叉树

    翻转二叉树 翻转一棵二叉树。 示例: 输入: 输出: 思路: Java实现 Swift实现

  • 二叉树BinaryTree

    Java 实现二叉树的构造以及遍历过程 二叉树遍历(先序、中序、后序)

  • Java自学-集合框架 二叉树

    Java集合框架 二叉树 示例 1 : 二叉树概念 二叉树由各种节点组成二叉树特点:每个节点都可以有左子节点,右子...

  • 18二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输出描述 输出描述 Java实现

  • Java二叉树的遍历

    Java二叉树的遍历 利用递归和非递归实现二叉树的先序,中序,后序遍历以及使用队列实现二叉树的层次遍历

  • 二叉树

    二叉树每个节点最多有两个子树,并且子树有左右之分,其次序不能任意颠倒 java 实现二叉树 遍历二叉树 前序遍历 ...

  • 632. 二叉树的最大节点

    在二叉树中寻找值最大的节点并返回。样例给出如下一棵二叉树: Java 代码

网友评论

      本文标题:Java 二叉树

      本文链接:https://www.haomeiwen.com/subject/luuibctx.html