简体   繁体   English

从BST(二进制搜索树)到链表列表

[英]From BST (binary search tree) to linkedlist array

A binary search tree was created by traversing through an array from left to right and inserting each element. 通过从左到右遍历数组并插入每个元素来创建二进制搜索树。 This tree may not be a balanced tree. 这棵树可能不是平衡树。 Given a binary search tree with distinct elements, print all possible arrays that could have led to this tree. 给定具有不同元素的二叉搜索树,请打印所有可能导致此树的数组。

To answer to this question I wrote the following code. 为了回答这个问题,我编写了以下代码。 Still, it seems that it doesn't print all possible arrays that could have lead to the the tree in all the cases. 尽管如此,似乎它并不能在所有情况下都打印出所有可能导致树的数组。 What do you think should be modified ? 您认为应该修改什么?

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


  }

What holds about the array is that if A is ancestor of B in the graph then A precedes B in the array. 关于数组的在于,如果A是图中B的祖先,则A在数组中的B之前。 Nothing else can be assumed. 没有其他假设。 So the arrays can be produced by the following recursive function. 因此,可以通过以下递归函数生成数组。

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

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

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