数据结构中序线索二叉树
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{ //以标明空子树的先根序列创建一棵子树,算法同BinaryTree
ThreadBinaryNode<T> *p=NULL;
if (i<n)
{
T elem = prelist[i];
i++;
if (elem!=NULL)
{
p = new ThreadBinaryNode<T>(elem);
p->parent=t;
{
if(p->ltag==1&&p->rtag==1)//p为叶子节点
{
ThreadBinaryNode<T> *q=p->parent;//this->getParent(p);
if(q->left==p)//左叶子节点
{
q->left=p->left;
q->ltag=1;
}
else if(q->right==p)//右叶子节点
ThreadBinaryNode<T> *find=NULL; //记载找到结点
if (p!=NULL)
{
if (p->data==value)
return p;
if(p->ltag==0)//查找成功,返回结点指针
find = search(p->left, value); //在左子树中查找,find指向找到结点,递归调用
void inThread(ThreadBinaryNode<T> *p, ThreadBinaryNode<T> *&front); //中序线索化以p结点为根的子树
ThreadBinaryNode<T>* search(ThreadBinaryNode<T> *p, T value); //在以p为根的子树中查找首次出现的值为value的结点
{
cout<<"中根次序遍历中序线索二叉树: ";
ThreadBinaryNode<T> *p=root;
while (p!=NULL && p->ltag==0) //寻找根的最左边的后代结点,即第一个访问结点
p=p->left;
while (p!=NULL)
{
cout<<p->data<<" ";
}
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::innext(ThreadBinaryNode<T> *p) //返回p在中根次序下的后继结点
{
if (p->rtag==1) //若右子树为空
p=p->right; //则p->right是指向p后继结点的线索
{
this->data=data;
this->left=left;
this->right=right;
this->parent=parent;
this->ltag=this->rtag=0;
}
};
////ThreadBinaryNode.h
#include "ThreadBinaryNode.h"
template <class T>
}
if (p->right==NULL) //若p的右子树为空
p->rtag=1; //设置右线索标记
if (front!=NULL && front->rtag==1)
front->right=p; //设置前驱front的right为指向后继p的线索
front=p;
inThread(p->right, front); //中序线索化p的右子树
temp->left=p->right;
p->parent=NULL;
ThreadBinaryNode<T>*front=NULL;
inThread(root,front); //中序线索化二叉树
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::create(T prelist[], int n, int &i,ThreadBinaryNode<T>*t)
{
temp=q;
return temp;
}
q=innext(q);
}
return temp;
}
template <class T>
void ThreadBinaryTree<T>::inThread(ThreadBinaryNode<T>* p, ThreadBinaryNode<T>* &front)
{
q->right=p->right;
q->rtag=1;
}
p->parent=NULL;//将父母节点置空
delete p;
}
else if(p->ltag==0 && p->rtag==1)//p有左孩子,没右孩子
{
ThreadBinaryNode<T> *q=p->parent;//this->getParent(p);
if (find==NULL && p->rtag==0) //若在左子树中未找到
find = search(p->right, value); //则继续在右子树中查找,递归调用
}
return find; //返回查找结果
}
template <class T>
ThreadBinaryTree<T>::~ThreadBinaryTree() //析构函数
if(q!=NULL)
{
if(q->left==p)
{
ThreadBinaryNode<T> *temp=this->inpre(p);
q->left=p->left;
p->left->parent=q;
temp->right=p->right;
p->parent=NULL;
delete p;
}
else if(q->right==p)
{
ThreadBinaryNode<T> *q=p->parent;//this->getParent(p);
if(q!=NULL)
{
if(q->left==p)
{
ThreadBinaryNode<T> *temp=this->innext(p);
q->left=p->right;
p->right->parent=q;
p=innext(p); //返回p在中根次序下的后继结点
}
cout<<endl;
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::search(T value) //查找首次出现的值为value结点
{
return search(root, value);
//线索二叉树的三叉链表结点类
template<class T>
class ThreadBinaryNode
{
public:
T data;
ThreadBinaryNode<T>*left,*right,*parent;
int ltag,rtag;
ThreadBinaryNode(T data,ThreadBinaryNode<T>*left=NULL,ThreadBinaryNode<T>*right=NULL,ThreadBinaryNode<T>*parent=NULL)
{
ThreadBinaryNode<T> *temp=this->inpre(p);//找到p的前驱节点temp
q->right=p->left;
p->left->parent=q;
temp->left=p->left;
p->parent=NULL;
delete p;
}
}
}
else if(p->ltag==1&&p->rtag==0)//p有右孩子,没左孩子
ThreadBinaryNode<T>*findlast() ;
ThreadBinaryNode<T>*findfirst() ;
~ThreadBinaryTree();
void inorder(); //中根次序遍历中序线索二叉
ThreadBinaryNode<T>* create(T prelist[], int n, int &i,ThreadBinaryNode<T>*t); //以标明空子树的先根次序遍历序列创建一棵子树
ThreadBinaryNode<T>*inpre(ThreadBinaryNode<T> *p);
ThreadBinaryNode<T>*insert(ThreadBinaryNode<T> *p, T value, bool leftChild=true); //插入value作为p结点的孩子
ThreadBinaryNode<T>*insertroot(char value,bool rootleft);
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::search(ThreadBinaryNode<T> *p, T value) //在以p为根的子树中查找
{ //先根次序遍历查找值为value的结点,返回首次出现结点指针,若未找到返回NULL
else
{
p=p->right; //进入右子树
while (p->ltag==0) //寻找最左边的后代结点
p=p->left;
}
return p;
}
template <class T>
void ThreadBinaryTree<T>::inorder() //中根次序遍历中序线索二叉树,非递归算法
};
template <class T>
ThreadBinaryTree<T>::ThreadBinaryTree(T prelist[], int n)
{ //以标明空子树的先根序列构造一棵中序线索二叉树
int i=0;
ThreadBinaryNode<T>*p=NULL;
root=create(prelist,n,i,p);
ThreadBinaryNode<T>* innext(ThreadBinaryNode<T> *p); //返回p在中根次序下的后继结点
void destroy(ThreadBinaryNode<T> *p); //撤销以p结点为根的子树
void remove(ThreadBinaryNode<T> *p);//删除
{ //中序线索化以p结点为根的子树,front指向p的前驱结点
if (p!=NULL)
{
inThread(p->left, front); //中序线索化p的左子树
if (p->left==NULL) //若wenku.baidu.com的左子树为空
{
p->ltag=1; //设置左线索标记
p->left=front; //设置p的left为指向前驱front的线索
{
destroy(root);
cout<<endl;
}
template <class T>
void ThreadBinaryTree<T>::destroy(ThreadBinaryNode<T> *p) //撤销以p结点为根的子树
{ //递归算法的后根次序遍历
if (p!=NULL)
{
if (p->ltag==0)
destroy(p->left);
if (p->rtag==0)
destroy(p->right);
delete p;
}
}
template <class T>
void ThreadBinaryTree<T>::remove(ThreadBinaryNode<T> *p)
{
if(p!=NULL)
{
if(p!=this->root)
{
ThreadBinaryNode<T> *q=this->root;
ThreadBinaryNode<T> *temp=NULL;
while (q!=NULL && q->ltag==0) //寻找根的最左边的后代结点,即第一个访问结点
q=q->left;
while (q!=NULL)
{
if(innext(q)==p)
p->left = create(prelist,n,i,p);
p->right = create(prelist,n,i,p);
}
}
return p;
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::inpre(ThreadBinaryNode<T> *p)
class ThreadBinaryTree //中序线索二叉树类
{
public:
ThreadBinaryNode<T> *root;
ThreadBinaryTree(T prelist[], int n);//以标明空子树的先根序列构造一棵中序线索二叉树
ThreadBinaryNode<T>*search(T value);//查找值为value的结点,返回节点
ThreadBinaryNode<T> *p=NULL;
if (i<n)
{
T elem = prelist[i];
i++;
if (elem!=NULL)
{
p = new ThreadBinaryNode<T>(elem);
p->parent=t;
{
if(p->ltag==1&&p->rtag==1)//p为叶子节点
{
ThreadBinaryNode<T> *q=p->parent;//this->getParent(p);
if(q->left==p)//左叶子节点
{
q->left=p->left;
q->ltag=1;
}
else if(q->right==p)//右叶子节点
ThreadBinaryNode<T> *find=NULL; //记载找到结点
if (p!=NULL)
{
if (p->data==value)
return p;
if(p->ltag==0)//查找成功,返回结点指针
find = search(p->left, value); //在左子树中查找,find指向找到结点,递归调用
void inThread(ThreadBinaryNode<T> *p, ThreadBinaryNode<T> *&front); //中序线索化以p结点为根的子树
ThreadBinaryNode<T>* search(ThreadBinaryNode<T> *p, T value); //在以p为根的子树中查找首次出现的值为value的结点
{
cout<<"中根次序遍历中序线索二叉树: ";
ThreadBinaryNode<T> *p=root;
while (p!=NULL && p->ltag==0) //寻找根的最左边的后代结点,即第一个访问结点
p=p->left;
while (p!=NULL)
{
cout<<p->data<<" ";
}
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::innext(ThreadBinaryNode<T> *p) //返回p在中根次序下的后继结点
{
if (p->rtag==1) //若右子树为空
p=p->right; //则p->right是指向p后继结点的线索
{
this->data=data;
this->left=left;
this->right=right;
this->parent=parent;
this->ltag=this->rtag=0;
}
};
////ThreadBinaryNode.h
#include "ThreadBinaryNode.h"
template <class T>
}
if (p->right==NULL) //若p的右子树为空
p->rtag=1; //设置右线索标记
if (front!=NULL && front->rtag==1)
front->right=p; //设置前驱front的right为指向后继p的线索
front=p;
inThread(p->right, front); //中序线索化p的右子树
temp->left=p->right;
p->parent=NULL;
ThreadBinaryNode<T>*front=NULL;
inThread(root,front); //中序线索化二叉树
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::create(T prelist[], int n, int &i,ThreadBinaryNode<T>*t)
{
temp=q;
return temp;
}
q=innext(q);
}
return temp;
}
template <class T>
void ThreadBinaryTree<T>::inThread(ThreadBinaryNode<T>* p, ThreadBinaryNode<T>* &front)
{
q->right=p->right;
q->rtag=1;
}
p->parent=NULL;//将父母节点置空
delete p;
}
else if(p->ltag==0 && p->rtag==1)//p有左孩子,没右孩子
{
ThreadBinaryNode<T> *q=p->parent;//this->getParent(p);
if (find==NULL && p->rtag==0) //若在左子树中未找到
find = search(p->right, value); //则继续在右子树中查找,递归调用
}
return find; //返回查找结果
}
template <class T>
ThreadBinaryTree<T>::~ThreadBinaryTree() //析构函数
if(q!=NULL)
{
if(q->left==p)
{
ThreadBinaryNode<T> *temp=this->inpre(p);
q->left=p->left;
p->left->parent=q;
temp->right=p->right;
p->parent=NULL;
delete p;
}
else if(q->right==p)
{
ThreadBinaryNode<T> *q=p->parent;//this->getParent(p);
if(q!=NULL)
{
if(q->left==p)
{
ThreadBinaryNode<T> *temp=this->innext(p);
q->left=p->right;
p->right->parent=q;
p=innext(p); //返回p在中根次序下的后继结点
}
cout<<endl;
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::search(T value) //查找首次出现的值为value结点
{
return search(root, value);
//线索二叉树的三叉链表结点类
template<class T>
class ThreadBinaryNode
{
public:
T data;
ThreadBinaryNode<T>*left,*right,*parent;
int ltag,rtag;
ThreadBinaryNode(T data,ThreadBinaryNode<T>*left=NULL,ThreadBinaryNode<T>*right=NULL,ThreadBinaryNode<T>*parent=NULL)
{
ThreadBinaryNode<T> *temp=this->inpre(p);//找到p的前驱节点temp
q->right=p->left;
p->left->parent=q;
temp->left=p->left;
p->parent=NULL;
delete p;
}
}
}
else if(p->ltag==1&&p->rtag==0)//p有右孩子,没左孩子
ThreadBinaryNode<T>*findlast() ;
ThreadBinaryNode<T>*findfirst() ;
~ThreadBinaryTree();
void inorder(); //中根次序遍历中序线索二叉
ThreadBinaryNode<T>* create(T prelist[], int n, int &i,ThreadBinaryNode<T>*t); //以标明空子树的先根次序遍历序列创建一棵子树
ThreadBinaryNode<T>*inpre(ThreadBinaryNode<T> *p);
ThreadBinaryNode<T>*insert(ThreadBinaryNode<T> *p, T value, bool leftChild=true); //插入value作为p结点的孩子
ThreadBinaryNode<T>*insertroot(char value,bool rootleft);
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::search(ThreadBinaryNode<T> *p, T value) //在以p为根的子树中查找
{ //先根次序遍历查找值为value的结点,返回首次出现结点指针,若未找到返回NULL
else
{
p=p->right; //进入右子树
while (p->ltag==0) //寻找最左边的后代结点
p=p->left;
}
return p;
}
template <class T>
void ThreadBinaryTree<T>::inorder() //中根次序遍历中序线索二叉树,非递归算法
};
template <class T>
ThreadBinaryTree<T>::ThreadBinaryTree(T prelist[], int n)
{ //以标明空子树的先根序列构造一棵中序线索二叉树
int i=0;
ThreadBinaryNode<T>*p=NULL;
root=create(prelist,n,i,p);
ThreadBinaryNode<T>* innext(ThreadBinaryNode<T> *p); //返回p在中根次序下的后继结点
void destroy(ThreadBinaryNode<T> *p); //撤销以p结点为根的子树
void remove(ThreadBinaryNode<T> *p);//删除
{ //中序线索化以p结点为根的子树,front指向p的前驱结点
if (p!=NULL)
{
inThread(p->left, front); //中序线索化p的左子树
if (p->left==NULL) //若wenku.baidu.com的左子树为空
{
p->ltag=1; //设置左线索标记
p->left=front; //设置p的left为指向前驱front的线索
{
destroy(root);
cout<<endl;
}
template <class T>
void ThreadBinaryTree<T>::destroy(ThreadBinaryNode<T> *p) //撤销以p结点为根的子树
{ //递归算法的后根次序遍历
if (p!=NULL)
{
if (p->ltag==0)
destroy(p->left);
if (p->rtag==0)
destroy(p->right);
delete p;
}
}
template <class T>
void ThreadBinaryTree<T>::remove(ThreadBinaryNode<T> *p)
{
if(p!=NULL)
{
if(p!=this->root)
{
ThreadBinaryNode<T> *q=this->root;
ThreadBinaryNode<T> *temp=NULL;
while (q!=NULL && q->ltag==0) //寻找根的最左边的后代结点,即第一个访问结点
q=q->left;
while (q!=NULL)
{
if(innext(q)==p)
p->left = create(prelist,n,i,p);
p->right = create(prelist,n,i,p);
}
}
return p;
}
template <class T>
ThreadBinaryNode<T>* ThreadBinaryTree<T>::inpre(ThreadBinaryNode<T> *p)
class ThreadBinaryTree //中序线索二叉树类
{
public:
ThreadBinaryNode<T> *root;
ThreadBinaryTree(T prelist[], int n);//以标明空子树的先根序列构造一棵中序线索二叉树
ThreadBinaryNode<T>*search(T value);//查找值为value的结点,返回节点