2010-08-18 4 views
1

여기에 내가 지금까지 가지고있는 것이 있습니다. 나는 나무 ADT 구현을 아이들의 목록으로 만들기로되어있다. 내가하는 일이 옳은지 아닌지 나는 확신 할 수 없다. 어떤 도움이라도 대단히 감사하겠습니다. 미리 감사드립니다.C++에서 어린이 목록을 사용하는 트리 구현

#include <string> 
#include <iostream> 

using namespace std; 

const int maxcells = 1000; 
const int maxnodes = 1000; 

template<class T> class LoCTree{ 

public: 

    LoCTree(); 
    ~LoCTree(); 
    int firstNodeSpot(); 
    int firstCellSpot(); 
    int lastN(int head); 
    int nodePos(int head, int n); 
    int getNodePosition(int node); 
    int getCellPosition(int header); 
    T label(int node); 
    int create0(T label); 
    int create1(T label, int tree1); 
    int create2(T label, int tree1, int tree2); 
    int create3(T label, int tree1, int tree2, int tree3); 
    int leftmostChild(int node); 
    int rightSibling(int node); 
    int parent(int node); 
    int root(int node); 
    void makenull(); 
    void print(int head); 
    void preorder(int node); 

    private: 
    struct node{ 

     T label; 
     int header; 
     int position; 

     node(){ 
      label = T(); 
      header = -1; 
      position = -1; 
     } 

    }; 
    node nodespace[maxnodes]; 

    struct cell{ 
     int node; 
     int next; 
    }; 
    cell cellspace[maxcells]; 
}; 

template<class T> LoCTree<T>::LoCTree(){ 
    for(int a=0;a<maxcells;a++){ 
     cellspace[a].node = -1; 
     cellspace[a].next = -1; 
    } 
    for(int b=0; b< maxnodes;b++){ 
     nodespace[b].label = T(); 
     nodespace[b].header = -1; 
    } 
} 
template<class T> LoCTree<T>::~LoCTree(){ 

} 
template<class T> int LoCTree<T>::firstNodeSpot(){ 
    for(int a=0; a<maxnodes; a++){ 
     if(nodespace[a].header==-1){ 
      return a; 
     } 
    } 
     return -1; 
} 
template<class T> int LoCTree<T>::firstCellSpot(){ 
    for(int a=0; a<maxcells; a++){ 
     if(cellspace[a].node==-1){ 
      return a; 
     } 
    } 
     return -1; 
} 

template<class T> int LoCTree<T>::create0(T label){ 
    int nodespot = firstNodeSpot(); 
    if(nodespot != -1){ 
     nodespace[nodespot].label = label; 
     nodespace[nodespot].position = 0; 
     return nodespot; 
    } 
    return -1; 
} 
template<class T> int LoCTree<T>::create1(T label, int tree1){ 
    int nodespot = create0(label); 
    if(nodespot != -1){ 
     int cellspot = firstCellSpot(); 
     nodespace[nodespot].header = cellspot; 
     cellspace[cellspot].node = nodespot; 
     nodespace[tree1].position = nodespot; 
     cellspace[cellspot].node = tree1; 
     return nodespot; 
    } 
    return -1; 
} 
template<class T> int LoCTree<T>::create2(T label, int tree1, int tree2){ 
    int anode = create1(label,tree1); 
    if(anode != -1){ 
     cellspace[nodespace[anode].header].next = firstNodeSpot(); 
     nodespace[tree2].position = anode; 
     cellspace[firstNodeSpot()].node = tree2; 
     return anode; 
    } 
    return -1; 
} 

template<class T> void LoCTree<T>::print(int head){ 
    //cout << head; 

    int nodespot = head; 
    int cellspot = -1; 
    int tmp = -1; 
    while(nodespace[nodespot].header!=-1){ 
     cout << nodespace[nodespot].label; 
     cellspot = nodespace[nodespot].header; 
     tmp = cellspace[cellspot].next; 
     if(tmp == -1){ 
      int tmp1 = nodespace[nodespot].header; 
      nodespot = cellspace[cellspot].node; 
      if(nodespot == tmp1){ 
       break; 
      } 
     } 
     else{ 
      nodespot = cellspace[cellspot].next; 
     } 
    } 
    cout << nodespace[nodespot].label; 

    //cout << nodespace[3].label; 
    //cout << nodespace[getNodePosition(cellspace[getCellPosition(0)].node)].label << endl; 
    //cout << nodespace[getNodePosition(cellspace[getCellPosition(0)].node)].label << endl; 
     /* 
     while(node!=-1){ 
      cout << nodespace[node].label; 
      node = cellspace[nodespace[node].header].next; 
     } 
    }*/ 
} 
+0

나중에 사용자를 위해 포맷을 수정했습니다. 나중에 코드를 선택하고 101010 버튼을 클릭하여 읽기 쉽게 형식을 지정하십시오. – bdonlan

+0

감사합니다. 처음으로이 사이트를 사용하고 있습니다. – arad

답변

1

자녀에게 연결된 목록을 사용하는 특별한 이유가 있습니까? 예를 들어 다음은 의사 코드의 실제 예입니다.

template<class T> 
class Treenode 
{ 
public: 
    Treenode* children; 
    T data; 

    Treenode(T _data) { 
     children = 0; 
     data = _data; 
    } 

    addChild(Treenode* t) { 
     t->next = children; 
     children = t; 
    } 

    addNewChild(T _data) 
    { 
     addChild(new Treenode(_data)); 
    } 

    Treenode* getNthChild(int n) { 
     int i = 0; 
     for (Treenode* t = children, int i = 0 ; t != 0 ; t = t->next, i++) { 
     if (i == n) return t; 
     } 
     return 0; 
    } 
} 
+0

예, 불행히도이 작업은 내가 사용한 구조를 사용하도록 명시 적으로 요청하고 자식에 대한 목록을 연결하지 않습니다. 나는 정직하게 꽤 혼란 스럽다 – arad

+0

흠. 좋습니다. 물론 두 개가 아닌 하나의 배열을 사용하여 코드를 간소화 할 수 있습니다. 노드 공간과 셀 공간을 위해 별도의 배열을 가져야하는 이유가 있다고 생각하지 않습니다. 각 노드에는 해당 셀이 있습니다. 맞습니까? 그리고 노드와 셀 사이에 일대일 관계가 있습니까? 그렇다면 모든 것을 하나의 구조체에 넣고 그 중 하나의 배열을 가질 수 있습니다. – eeeeaaii

+0

좋습니다. 작동하는지 확인해보십시오. 감사합니다 – arad