【问题标题】:From BST (binary search tree) to linkedlist array从 BST(二叉搜索树)到链表数组
【发布时间】:2016-05-15 17:30:48
【问题描述】:

通过从左到右遍历数组并插入每个元素来创建二叉搜索树。这棵树可能不是平衡树。给定具有不同元素的二叉搜索树,打印所有可能导致这棵树的数组。

为了回答这个问题,我编写了以下代码。尽管如此,它似乎并没有打印在所有情况下可能导致树的所有可能数组。你觉得应该修改什么?

public class Main {

  public static LinkedList<Integer> passed = new LinkedList<>();
    public static LinkedList<BinaryTree> notyet = new LinkedList<>();
    public static ArrayList<LinkedList<Integer>> results = new ArrayList<LinkedList<Integer>>();



public static void main(String args[]) {
    BinaryTree tr = readTree();
    ArrayList<LinkedList<Integer>> result = allSequences(tr);
    for (LinkedList<Integer> l : result){
        for(int elem: l) System.out.print(elem+" ");
        System.out.println("");
    }
}
private static BinaryTree readTree() {
    BinaryTree tr = new BinaryTree(2, null, null);
    tr.left = new  BinaryTree(1, null, null);
    tr.right = new  BinaryTree(3, null, null);
    return tr;
}

public static ArrayList<LinkedList<Integer>> allSequences(BinaryTree tr){
    // implement here
    ArrayList<LinkedList<Integer>> result = new ArrayList<LinkedList<Integer>>();


    findseqs(passed,notyet,tr);
    //result=results.clone();
    for(LinkedList<Integer> sample :results){
        result.add(sample);
    }


    return result;
}


public static void findseqs(LinkedList<Integer> passed, LinkedList<BinaryTree> notyet, BinaryTree tr) {
    passed.add(tr.value);

    if (tr.left != null) notyet.add(tr.left);
  if (tr.right != null) notyet.add(tr.right);

  if (notyet.isEmpty()) {
    results.add(passed);
  }

  for (BinaryTree elem: notyet) {
    LinkedList<Integer> temp = (LinkedList<Integer>) passed.clone();
    LinkedList<BinaryTree> ptemp = (LinkedList<BinaryTree>) notyet.clone();
    ptemp.remove(elem);
    findseqs(temp, ptemp, elem);
  }


  }

【问题讨论】:

    标签: java tree linked-list binary-tree binary-search-tree


    【解决方案1】:

    关于数组的情况是,如果 A 是图中 B 的祖先,则 A 在数组中位于 B 之前。没有别的可以假设。 所以数组可以通过下面的递归函数生成。

    function sourceArrays(Tree t)
    
      // leafe node
      if t == null
        return empty list;
    
      r = root(t);
      append r to existing arrays;
    
      la = sourceArrays(t.left);
      ra = sourceArrays(t.right);
    
      ac = createArrayCombitations(la, ra);
      append ac to existing arrays;
    
    end
    
    function createArrayCombitations(la, ra)
    
    
      foreach a in la
        foreach b in ra
          r = combineArrays(a,b);
          add r to result;
        end
      end
    
    end
    
    function combineArrays(a, b)
      generate all combinations of elements from two array such that order of elements in each array is preserved.
      Ie if x precedes y in a or b the x precedes y in result
    

    【讨论】:

    • 我不明白你回答的最后一部分!你能给我解释一下吗?
    • “最后一部分”是什么意思?
    • 函数 combineArrays(a, b) 从两个数组中生成所有元素的组合,从而保留每个数组中元素的顺序。即,如果 x 在 a 或 b 中先于 y,则 x 在结果中先于 y
    • 您有 2 个有序集合 A 和 B,并且您想要创建它们的所有组合 C,这样如果 x 在 A 或 B 中位于 y 之前,那么 x 在 C 中也位于 y 之前。 HTH
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2012-05-13
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    相关资源
    最近更新 更多