雙親標記法:
#include<iostream>using namespace std;template<typename T>struct Node{T _data;int parent;};template<typename T>struct Tree{Node<T> array[100];int count;};template<typename T>void addNode(T data,int parent,Tree<T>& tree){tree.array[tree.count]._data=data;tree.array[tree.count].parent=parent;tree.count++;}int main(){Tree<int> tree;tree.count=0;addNode(0,-1,tree);for(int i=0;i<tree.count;i++){cout<<tree.array[i]._data<<' '<<tree.array[i].parent<<endl;}return 0;}
孩子標記法:
#include<iostream>using namespace std;struct Child{int index;Child* next;};struct Node{int data;Child* child;};struct Tree{Node array[100];int count;};void AddNode(Tree& t,int data,int parent){t.array[t.count].data=data;t.array[t.count].child=0;if(parent>-1){Child* ch=new Child;ch->index=t.count;ch->next=0;Child*& temp=t.array[parent].child;while(temp!=0){temp=temp->next;}temp=ch;}t.count++;}int main(){Tree tree;tree.count=0;AddNode(tree,0,-1);for(int i=0;i<tree.count;i++){cout<<tree.array[i].data<<endl;}return 0;}
雙親孩子標記法:
#include<iostream>using namespace std;struct Child{int index;Child* next;};struct Node{int data;int parent; Child* child;};struct Tree{Node array[100];int count;};void AddNode(Tree& t,int data,int parent){t.array[t.count].data=data;t.array[t.count].child=0;t.array[t.count].parent=parent;if(parent>-1){Child* ch=new Child;ch->index=t.count;ch->next=0;Child*& temp=t.array[parent].child;while(temp!=0){temp=temp->next;}temp=ch;}t.count++;}int main(){Tree tree;tree.count=0;AddNode(tree,0,-1);for(int i=0;i<tree.count;i++){cout<<tree.array[i].data<<endl;}return 0;}