简体   繁体   中英

Inserting array to BST in-order traverse

I want to traverse over a given tree using in-order traversal. inserting the sorted array into the BST (keeping it the same shape)

This is my go:

public static BinTreeNode<Integer> ARR_TO_BST(BinTreeNode<Integer> root, int[] arr, int ind){

    
    if (root.GetLeft() != null) 
        ARR_TO_BST(root.GetLeft(), arr, ind);

    root.SetInfo(arr[ind]);
    ind+=1;
    
    if (root.GetRight() != null)
        ARR_TO_BST(root.GetRight(), arr, ind);
    
    return root;

The problem is that if the array is arr = {10,20,30,40,50,60} and the tree is:

The return output is a tree that if I do an in-order traversal over it is: 10 20 10 20 10 20... and not 10 20 30 40 50 60

I need the output to be the same shape of the picture but with the values of arr the algorithm is to traverse -in order on the tree: left subtree - vertex - right subtree
but instead of reading the values we insert the values from arr to root

I would appreciate help! thank you

You never change ind . After the first call to ARR_TO_BST , it remains what it was before the call. Make ARR_TO_BST return the number of elements it placed:

    if (root.GetLeft() != null)
        ind = ARR_TO_BST(root.GetLeft(), arr, ind);
    root.SetInfo(arr[ind]);
    if (root.GetLeft() != null)
        ind = ARR_TO_BST(root.GetLeft(), arr, ind + 1);
    return ind;

and you should be all right.

You can use accomplish by keep tracking the index of each element to add it back to the result array

  static class BST {

        private class Node {
            private Integer key;
            private Node left, right;
            private int index;

            public Node(Integer key, int index) {
                this.key = key;
                this.index = index;
            }
        }

        private Node root;
        private int size = 0;

        public void put(int[] arr) {
            if (size >= arr.length)
                return;
            root = put(root, arr[size], size);
            size++;
            put(arr);
        }

        private Node put(Node node, int key, int index) {
            if (node == null)
                return new Node(key, index);
            int cmp = Integer.valueOf(key).compareTo(node.key);
            if (cmp < 0)
                node.left = put(node.left, key, index);
            else if (cmp > 0)
                node.right = put(node.right, key, index);
            else
                node.key = key;
            return node;
        }

        public int size() {
            return size;
        }

        public int[] keys() {
            int[] result = new int[size];
            get(root, result, 0);
            return result;
        }

        public void get(Node node, int[] result, int i) {
            if (i >= result.length || node == null)
                return;
            result[node.index] = node.key;
            get(node.left, result, ++i);
            get(node.right, result, ++i);
        }
    }

, main

    public static void main(String[] args) {
        BST bst = new BST();
        bst.put(new int[] { 10, 20, 5, 40, 1, 60, -10, 0 });

        for (int num : bst.keys()) {
            System.out.print(num + " ");
        }
    }

, output

10 20 5 40 1 60

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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