come ricostruire BST utilizzando {pre, a, posta} attraversamenti Ordina i risultati

voti
3

Sappiamo che il pre-ordine, in ordine e attraversamenti post-order. Che algoritmo ricostruire la BST?

È pubblicato 20/03/2011 alle 08:59
fonte dall'utente
In altre lingue...                            


4 risposte

voti
12

Perché è BST, in-orderpossono essere ordinati da pre-ordero post-order<1>. In realtà, sia pre-ordero post-orderè necessaria solo ....

<1> se si sa cosa la funzione di confronto è


Da pre-ordere in-order, di costruire un albero binario

BT createBT(int* preOrder, int* inOrder, int len)
{
    int i;
    BT tree;
    if(len <= 0)
        return NULL;
    tree = new BTNode;
    t->data = *preOrder;
    for(i = 0; i < len; i++)
        if(*(inOrder + i) == *preOrder)
            break;
    tree->left = createBT(preOrder + 1, inOrder, i);
    tree->right = createBT(preOrder + i + 1, inOrder + i + 1, len - i - 1);
    return tree;
}

La logica alla base di questo:

In pre-ordine, il primo nodo è la radice. Trovare la radice nella in-ordine. Poi l'albero può essere diviso in destra e sinistra. Farlo in modo ricorsivo.

Simile per post-ordere in-order.

Risposto il 20/03/2011 a 09:50
fonte dall'utente

voti
0

Personalmente ho trovato la risposta di Dante un po 'difficile da seguire. Ho lavorato la mia strada attraverso la soluzione e l'ho trovato per essere simile a quello postato qui http://geeksforgeeks.org/?p=6633

Complessità è O (N ^ 2).

Ecco un altro approccio per la costruzione di un albero con post-ordine di attraversamento: http://www.technicallyidle.com/2011/02/15/build-binary-search-tree-using-post-order-traversal-trace/

Spero che questo ti aiuti

Risposto il 22/03/2011 a 04:29
fonte dall'utente

voti
0

Per la ricostruzione di un albero binario sia preordine + ordine simmetrico o postorder + simmetrico è necessaria. Come già sottolineato in un BST possiamo ricostruire utilizzando preordine o postorder l'ordinamento nessuno dei due ci darà l'ordine simmetrico.

È possibile utilizzare la seguente funzione che è modifica del codice in @brainydexter ricostruire l'albero senza utilizzare la variabile statica:

struct node* buildTree(char in[],char pre[], int inStrt, int inEnd,int preIndex){

    // start index > end index..base condition return NULL.
    if(inStrt > inEnd)
        return NULL;

    // build the current node with the data at pre[preIndex].
    struct node *tNode = newNode(pre[preIndex]);

    // if all nodes are constructed return. 
    if(inStrt == inEnd)
        return tNode;

    // Else find the index of this node in Inorder traversal
    int inIndex = search(in, inStrt, inEnd, tNode->data);

    // Using index in Inorder traversal, construct left and right subtress
    tNode->left = buildTree(in, pre, inStrt, inIndex-1,preIndex+1);
    tNode->right = buildTree(in, pre, inIndex+1, inEnd,preIndex+inIndex+1);

    return tNode;
}
Risposto il 22/03/2011 a 06:01
fonte dall'utente

voti
0

Ecco una soluzione ricorsiva Rubino

def rebuild(preorder, inorder)
  root = preorder.first
  root_inorder = inorder.index root
  return root unless root_inorder
  root.left = rebuild(preorder[1, root_inorder], inorder[0...root_inorder])
  root.right = rebuild(preorder[root_inorder+1..-1], inorder[root_inorder+1..-1])
  root
end

E un esempio

class Node
  attr_reader :val
  attr_accessor :left, :right

  def initialize(val)
    @val = val
  end

  def ==(node)
    node.val == val
  end

  def inspect
    "val: #{val}, left: #{left && left.val || "-"}, right: #{right && right.val || "-"}"
  end
end

inorder = [4, 7, 2, 5, 1, 3, 8, 6, 9].map{|v| Node.new v }
preorder = [1, 2, 4, 7, 5, 3, 6, 8, 9].map{|v| Node.new v }

tree = rebuild(preorder, inorder)
tree
# val: 1, left: 2, right: 3
tree.left
# val: 2, left: 4, right: 5
tree.left.left
# val: 4, left: -, right: 7
Risposto il 28/06/2015 a 09:03
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more