简体   繁体   English

二叉树:按升序打印数据

[英]binary Tree: printing the data in ascending order

I am a beginner in java .. I have just started the data structure online.我是java初学者..我刚刚开始在线学习数据结构。 I want to print the values i have added to the binary tree in ascending order.我想按升序打印我添加到二叉树的值。

I have created a method print and tried it with these values:我创建了一个方法 print 并尝试使用以下值:

9,5,2,8,3 9、5、2、8、3

It printed this output and stopped它打印了这个输出并停止了

2 , 3 ,8 2 , 3 ,8

the Nodes have to constructors :节点必须构造函数:

Constructor 1构造函数 1

public Node(int value){
    this.Value=value;
    isEmpty=false;
    this.left=new Node();
    this.right=new Node();
}

Constructor 2构造函数 2

public Node(){
   isEmpty=true; 
}

The Adding method :添加方法:

public void add(int value) {

    if (Objects.isNull(root)) {
        root = new Node(value);
        root.isEmpty = false;
    }
    Node current = root;
    while (true) {

        if (value < current.Value) {
            if (current.left.isEmpty) {
                current.left.prev = current;
                current = current.left;
                current.Value = value;
                current.isEmpty = false;
                current.left = new Node();
                current.right = new Node();
                break;
            } else {
                current = current.left;

            }
        } else {
            if (current.right.isEmpty) {
                current.right.prev = current;
                current = current.right;
                current.Value = value;
                current.isEmpty = false;
                current.left = new Node();
                current.right = new Node();
                break;
            } else {
                current = current.right;

            }
        }
    }
}

The Print method打印方法

ArrayList<Node> list = new ArrayList();
Node current = root;while(true){
 if(!current.left.isEmpty ){
     if(!list.contains(current.left)){
     current=current.left;
     continue;
     }

 } else {
     System.out.println(current.Value);
     list.add(current);
     if(!current.right.isEmpty && !list.contains(current.right)){
       current=current.right;
       continue;
     }

     current=current.prev.prev;
 } 

To print data from BST you need to do inorder traversal.要从 BST 打印数据,您需要进行中序遍历。 In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order.在二叉搜索树 (BST) 的情况下,中序遍历以非递减顺序给出节点。 To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder traversal s reversed can be used.要以非递增顺序获取 BST 的节点,可以使用 Inorder traversal 的变体,其中 Inorder traversal s reversed。

Algorithm Inorder(tree) 1. Traverse the left subtree, ie, call Inorder(left-subtree) 2. Visit the root.算法 Inorder(tree) 1. 遍历左子树,即调用 Inorder(left-subtree) 2. 访问根。 3. Traverse the right subtree, ie, call Inorder(right-subtree) 3.遍历右子树,即调用Inorder(right-subtree)

/* Given a binary tree, print its nodes in inorder*/
void printInorder(Node node) 
{ 
    if (node == null) 
        return; 

    /* first recur on left child */
    printInorder(node.left); 

    /* then print the data of node */
    if(!node.isEmpty){
        System.out.print(node.value+ " "); 
    }

    /* now recur on right child */
    printInorder(node.right); 
} 

Time Complexity: O(n)时间复杂度:O(n)



If the tree is not BST.如果树不是BST。 You can create List and write the tree's values into the list and sort the list in ascending order.您可以创建 List 并将树的值写入列表并按升序对列表进行排序。

List<Integer> treeValues = new ArrayList<Integer>();

List<Integer> treeToList(Node node){
    if (node == null) 
        return; 
    printInorder(node.left); 
    if(!node.isEmpty){
        treeValues.add(node.value); 
    }
    printInorder(node.right); 
}

void sortedTree(Node node){
    List<Integer> treeData = treeToList(node);
    Collections.sort(treeData);
    for(int i=0; i<treeData.size();i++ ){
        System.out.println(treeData.get(i));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM