2011-03-26 100 views
0

我試圖複製一棵樹。樹被保存在SQLite數據庫中,並將其加載到樹視圖中。表結構如下:id,父級,級別,位置。在樹視圖中,id被賦予節點對象。現在我想遞歸地複製樹。這是需要產生新的ID和設置父ID的正確:複製樹並設置新的數據庫ID的

procedure copyTree(machineId, parent : integer; startNode : TTreeNode); 
var 
    tmpId : integer; 
    tmpData : TAssignmentData; // Record 
begin 
    if assigned(startNode) then 
    begin 
    //Read out data from existing node 
    tmpData := data.getAssignment(integer(startnode.Data)); 
    //Set node data to new machine 
    tmpData.machine := machineId; 
    //If node is on the top level 
    if tmpData.parent <> 0 then 
     tmpData.parent := parent; 
    //Write dataset (new node) to the database 
    tmpId := data.insertAssignment(tmpData); 

    copyTree(machineId, tmpId, startnode.getFirstChild); 
    copyTree(machineId, tmpId, startnode.getNextSibling); 
    end; 
end; 

我試過這個代碼,但父ID是不是在數據庫中設置正確。作業丟失了。我的錯誤在哪裏?

最好的問候......

回答

1

假設data.insertAssignment(tmpData);返回新ID爲一個節點,則只有該節點的孩子應該有自己的父ID設置爲tmpId。您還將下一個兄弟的參數設置爲tmpId!

copyTree(machineId, tmpId, startnode.getNextSibling); 

考慮剛好路過父母的兄弟節點:

copyTree(machineId, parent, startnode.getNextSibling); 
+0

謝謝,這就是它! – fillibuster 2011-03-27 19:46:35