?? tree.cpp
字號(hào):
//樹類的實(shí)現(xiàn)Tree.cpp
template<class T>
void Tree<T>::DeleteSubTree(TreeNode<T> *&t)
{if(t==NULL) return;
TreeNode<T> *q=t->firstChild,*p;
while(q!=NULL)
{p=q->nextSibling;
DeleteSubTree(q);
q=p;}
cout<<"釋放:"<<setw(2)<<t->data;
delete t;
}
template<class T>
int Tree<T>::Current(TreeNode<T> *&t)
{if(t==NULL) return 0;
curr=t;
return 1;
}
template<class T>
int Tree<T>::Root()
{if(root==NULL)
{curr=NULL;
return 0;}
return Current(root);
}
template<class T>
int Tree<T>::FirstChild()
{if(curr!=NULL&&curr->firstChild!=NULL)
return Current(curr->firstChild);
else return 0;
}
template<class T>
int Tree<T>::NextSibling()
{if(curr!=NULL&&curr->nextSibling!=NULL)
return Current(curr->nextSibling);
else return 0;
}
template<class T>
int Tree<T>::Parent()
{if(curr==NULL)
{curr=root;
return 0;}
TreeNode<T> *p=SearchParent(root,curr);
if(p==NULL) return 0;
else return Current(p);
}
template<class T>
TreeNode<T> *Tree<T>::SearchParent(TreeNode<T> *&root,TreeNode<T> *&s)
{if(root==NULL) return NULL;
if(root->FirstChild()==s||root->NextSibling()==s)
return root;
TreeNode<T> *p;
if((p=SearchParent(root->FirstChild(),s))!=NULL) return p;
if((p=SearchParent(root->NextSibling(),s))!=NULL) return p;
return NULL;
}
template<class T>
void Tree<T>::InsertChild(T value)
{TreeNode<T> *newNode=new TreeNode<T> (value);
if(root==NULL) //當(dāng)為空樹時(shí)
{root=curr=newNode;
return;}
if(curr->firstChild==NULL)//當(dāng)當(dāng)前結(jié)點(diǎn)無孩子時(shí)
curr->firstChild=newNode;
else //當(dāng)當(dāng)前結(jié)點(diǎn)有孩子時(shí)
{TreeNode<T> *p=curr->firstChild;
while(p->nextSibling!=NULL) p=p->nextSibling;
p->nextSibling=newNode;
}
Current(newNode);//使新建立的結(jié)點(diǎn)成為當(dāng)前結(jié)點(diǎn)
}
template<class T>
int Tree<T>::DeleteChild(int i)
{TreeNode<T> *r=NULL;
if(i==1) //當(dāng)刪除當(dāng)前結(jié)點(diǎn)的第一棵子樹時(shí)
{r=curr->firstChild;
if(r==NULL) return 0;//要?jiǎng)h除子樹為空時(shí)返回
curr->firstChild=r->nextSibling;//脫鏈要?jiǎng)h除的子樹
}
else { //當(dāng)刪除當(dāng)前結(jié)點(diǎn)的其他子樹時(shí)
int k=1;
TreeNode<T> *p=curr->firstChild;
while(p!=NULL&&k<=i-1)//尋找要?jiǎng)h除子樹的指針
{p=p->nextSibling;
k++;}
if(p!=NULL)//尋找到要?jiǎng)h除的子樹的指針
{r=p->nextSibling;
if(r!=NULL)
p->nextSibling=r->nextSibling;
else return 0;
}
else return 0;
}
DeleteSubTree(r);
return 1;
}
template<class T>
int Tree<T>::DeleteChild1(int i)
{if(root==NULL) return 0;//當(dāng)為空樹時(shí)
TreeNode<T> *r=NULL,*q=root->firstChild;
if(i==1&&q!=NULL) //當(dāng)?shù)谝唤Y(jié)點(diǎn)有孩子時(shí)
{r=root->firstChild;
root->firstChild=r->nextSibling;//脫鏈要?jiǎng)h除的子樹
}
else //要?jiǎng)h除第一結(jié)點(diǎn)外的其他子樹時(shí)
{int k=1;
TreeNode<T> *p=root->firstChild;
while(p!=NULL&&k<=i-1)//尋找要?jiǎng)h除子樹的指針
{p=p->nextSibling;
k++;
}
if(p!=NULL) //尋找到要?jiǎng)h除的子樹的指針
{r=p->nextSibling;
if(r!=NULL)
p->nextSibling=r->nextSibling;//脫鏈要?jiǎng)h除的子樹
else return 0;}
else return 0;
}
DeleteSubTree(r);//調(diào)用函數(shù)執(zhí)行刪除
return 1;
}
template<class T>
void Tree<T>::PreOrderTree(TreeNode<T> *&t)
{if(t==NULL) return;
cout<<setw(2)<<t->data;//顯示根結(jié)點(diǎn)數(shù)據(jù)
if(t->firstChild!=NULL)//先根遍歷子樹
PreOrderTree(t->firstChild);
if(t->nextSibling!=NULL)
PreOrderTree(t->nextSibling);
}
template<class T>
void Tree<T>::DisplayTree()
{PreOrderTree(root);}
template<class T>
void Tree<T>::DisplayTree1()
{PosOrderTree(root);}
template<class T>
void Tree<T>::PosOrderTree(TreeNode<T> *&t)
{if(t==NULL) return;
if(t->firstChild!=NULL)//后根遍歷子樹
PosOrderTree(t->firstChild);
cout<<setw(2)<<t->data;//顯示根結(jié)點(diǎn)數(shù)據(jù)
if(t->nextSibling!=NULL)
PosOrderTree(t->nextSibling);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -