当前位置:   article > 正文

如何建立一颗树_jdk.nashorn.internal.objects.annotations.setter;

jdk.nashorn.internal.objects.annotations.setter;

package com.example.demo;

import jdk.nashorn.internal.objects.annotations.Setter;

import java.util.*;
import java.util.stream.Stream;

public class BinaryTree {

private Node root ;

public Node getRoot() {
    return root;
}

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

private class Node {
    //左节点
    private Node left;
    //数据域
    private int data;
    //右节点
    private Node right;

    public Node(int data) {
        this.data = data;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }
}

private void buildTree(Node node, int data) {
    if (root==null) {
        root = new Node(data);
    } else {
        if (data < node.getData()) {
           if (node.getLeft()==null) {
               node.setLeft(new Node(data));
           } else {
               buildTree(node.getLeft(),data);
           }
        } else {
            if (node.getRight() == null){
                node.setRight(new Node(data));
            } else {
                buildTree(node.getRight(), data);
            }
        }
    }
}

public BinaryTree creatBitryTree(int[] datas) {
    BinaryTree binaryTree = new BinaryTree();
    for (int data: datas) {
        binaryTree.buildTree(binaryTree.getRoot(),data);
    }
    return binaryTree;

}

/**
 * 前序
 * @param node
 */
public void preTraversal(Node node) {
    if (node!=null) {
       System.out.println(node.getData());
       preTraversal(node.getLeft());
       preTraversal(node.getRight());
   }
}

/**
 * 广度便利
 */
public void levalBitrayTree(Node node) {

    if (node==null) {
        return;
    }
    Queue<Node> queue = new LinkedList<>();
    queue.add(node);
    while (!queue.isEmpty()) {
        Node poll = queue.poll();
        System.out.println(poll.getData());
        if (poll.getLeft()!=null) {
            queue.add(poll.getLeft());
        }
        if (poll.getRight()!=null) {
            queue.add(poll.getRight());
        }
    }

}

public static void main(String[] args) {
    BinaryTree binaryTree = new BinaryTree();
    BinaryTree binaryTree1 = binaryTree.creatBitryTree(new int[]{6, 3, 7, 4, 8, 5});
    //binaryTree1.preTraversal(binaryTree1.getRoot());
    binaryTree1.levalBitrayTree(binaryTree1.getRoot());
    List<Integer> list = new ArrayList<>();

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/226929
推荐阅读
相关标签
  

闽ICP备14008679号