【问题标题】:Inserting array to BST in-order traverse将数组插入 BST 中序遍历
【发布时间】:2020-05-10 16:29:43
【问题描述】:

我想使用中序遍历遍历给定的树。将排序后的数组插入 BST(保持相同的形状)

这是我的目标:

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;

问题是如果数组是arr = {10,20,30,40,50,60} 树是:

返回输出是一棵树,如果我按顺序遍历它是:10 20 10 20 10 20 ... 而不是 10 20 30 40 50 60

我需要输出与图片的形状相同但具有arr 的值,该算法是在树上按顺序遍历:左子树 - 顶点 - 右子树
但我们不是读取值,而是将值从arr 插入到root

我将不胜感激!谢谢

【问题讨论】:

  • 所以你已经以某种方式定义了树和一个数组,你想创建完全相同的树,但由数组中的项目组成,以便中序遍历与通过给定数组的项目迭代相同?
  • @Ecto 我在我的主函数上定义了树,我想创建一个具有相同形状但具有数组值(我选择值)的新树,同时保持树 BST -是,按顺序遍历它并放入数组中的值:左子树 - 顶点 - 右子树。简而言之:对你的问题是肯定的,按照 BST = 有序数组的顺序排列
  • 我有post解决方案,你能检查一下吗
  • @sc0der 嘿,谢谢你的回答,我们可以只使用数组来完成这个吗?还是不可能?
  • @sc0der 我需要保持树的形状和 BST 的属性(左

标签: java algorithm binary-tree binary-search-tree


【解决方案1】:

你永远不会改变ind。在第一次调用ARR_TO_BST 之后,它仍然是调用之前的状态。让ARR_TO_BST 返回它放置的元素数量:

    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;

你应该没事的。

【讨论】:

  • 但是你在哪里增加它以便数组值被插入到树中?现在它根本不增加它.. 不是吗?
  • 现在返回: 20 3 10 7 8 9 。我认为这是因为我们只在右子树上递增......
【解决方案2】:

您可以通过持续跟踪每个元素的索引来将其添加回结果数组中来完成

  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);
        }
    }

,主要

    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 + " ");
        }
    }

,输出

10 20 5 40 1 60

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    相关资源
    最近更新 更多