目录
1)找到以p为根,第一个为中序遍历的结点,其实就是最左边的叶节点
3)能找到后继,就可以不使用递归来遍历了,能得到中序遍历结果
4)找到以p为根结点,最后一个被中序遍历的结点,也就是p最右边的叶子结点
- typedef struct{
- ElemType data;
- int parent;
- }PTNode;
-
- typedef struct{
- PTNode nodes[MAXSIZE];
- int n; //结点数
- }PTree;
- struct CTNode{
- int child; //孩子在数组中的下标
- CTNode *next;
- };
-
- typedef struct {
- ElemType data;
- CTNode *firstChild;
- }CTBox;
-
- typedef struct{
- CTBox nodes[MAXSIZE];
- int n,r;//结点数、根的下标
- }CTree;
- struct TreeNode{
- ElemType value;
- bool isEmpty;
- };
-
- TreeNode t[MAXSIZE]; //一般从下标为1开始存储,为了和下标对应起来,方便查找孩子
-
- //顺序存储初始化将所有的isEmpty变为TRUE
-
- //左孩子为2i 右孩子为2i+1 父节点为i/2
-
- //i>n/2就是叶子结点
- //二叉树的链式存储
- typedef struct BiTNode{
- ElemType data;
- BiTNode *lchild,*rchild;
- }BiTNode,*BTree;
- BTree CreatBTree(ElemType rootdata)
- {
- BiTNode *root = (BiTNode*)malloc(sizeof(BiTNode));
- root->data=rootdata;
- root->lchild=NULL;
- root->rchild=NULL;
- return root;
- }
- BiTNode *CreatBiTNode(ElemType data)
- {
- BiTNode* NewNode = (BiTNode*)malloc(sizeof(BiTNode));
- NewNode->data=data;
- NewNode->lchild=NULL;
- NewNode->rchild=NULL;
- return NewNode;
- }
- void PreOrder(BTree T)
- {
- if(T!=NULL){
- visit(T);
- PreOrder(T->lchild);
- PreOrder(T->rchild);
- }
- }
- //会使用栈
- void PreOrder1(BTree T)
- {
- SqStack s;
- InitStack(s);
- BTree p =T;
- while (p||!StackEmpty(s)) {
- if(p){
- visit(p);
- push(s,p);
- p=p->lchild;
- }
- else {
- pop(s,p);
- p=p->rchild;
- }
- }
- }
- void InOrder(BTree T)
- {
- if(T!=NULL){
- InOrder(T->lchild);
- visit(T);
- InOrder(T->rchild);
- }
- }
- void InOrder1(BTree T)
- {
- SqStack s;
- InitStack(s);
- BTree p = T;
- while (p||!StackEmpty(s)) {
- if(p){
- push(s,p);
- p=p->lchild;
- }
- else {
- pop(s,p);
- visit(p);
- p=p->rchild;
- }
- }
- }
- void PostOrder(BTree T)
- {
- if(T!=NULL){
- PostOrder(T->lchild);
- PostOrder(T->rchild);
- visit(T);
- }
- }
- void PostOrder1(BTree T)
- {
- SqStack s;
- InitStack(s);
- BTree p = T;
- BTree r=NULL;
- while (p||!StackEmpty(s)) {
- if(p){
- push(s,p);
- p=p->lchild;
- }
- else {
- GetTop(s,p);
- if(p->rchild!=r&&p->rchild)
- p=p->rchild;
- else {
- pop(s,p);
- visit(p);
- r=p;
- p=NULL;
- }
- }
- }
- }
-
- void LevelOrder(BTree T)
- {
- LinkQueue Q;
- InitLinkQueue(Q);
- BTree p;
- EnLinkQueue(Q,T);
- while (!LinkQueueEmpty(Q)) {
- DeLinkQueue(Q,p);
- visit(p);
- if(p->lchild!=NULL)
- EnLinkQueue(Q,p->lchild);
- if(p->rchild!=NULL)
- EnLinkQueue(Q,p->rchild);
- }
- }
- void PreThread(ThreadTree T, ThreadTree &pre)
- {
- if(T!=NULL){
- if(T->lchild==NULL){
- T->lchild=pre;
- T->ltag=1;
- }
- if(pre!=NULL and pre->rchild==NULL){
- pre->rchild=T;
- pre->rtag=1;
- }
- pre=T;
- if(T->ltag==0)
- PreThread(T->lchild,pre);
- PreThread(T->rchild,pre);
-
- }
- }
-
- void CreatPreThread(ThreadTree T)
- {
- ThreadTree pre= NULL;
- if(T!=NULL){
- PreThread(T,pre);
- pre->rchild=NULL;
- pre->rtag=1;
- }
- }
- void InThread(ThreadTree T,ThreadTree &pre)
- {
- if(T!=NULL){
- InThread(T->lchild,pre);
-
- if(T->lchild==NULL){
- T->lchild=pre;
- T->ltag=1;
- }
- if(pre!=NULL and pre->rchild==NULL){
- pre->rchild=T;
- pre->rtag=1;
- }
- pre=T;
-
- InThread(T->rchild,pre);
- }
-
- }
-
- void CreatInThread(ThreadTree T)
- {
- ThreadTree pre= NULL;
- if(T!=NULL){
- InThread(T,pre);
- pre->rchild=NULL;
- pre->rtag=1;
- }
- }
- void PostThread(ThreadTree T, ThreadTree &pre)
- {
- if(T!=NULL){
- PostThread(T->lchild,pre);
- PostThread(T->rchild,pre);
-
- if(T->lchild==NULL){
- T->lchild=pre;
- T->ltag=1;
- }
- if(pre!=NULL and pre->rchild==NULL){
- pre->rchild=T;
- pre->rtag=1;
- }
- pre=T;
- }
- }
-
- void CreatPostThread(ThreadTree T)
- {
- ThreadTree pre= NULL;
- if(T!=NULL){
- PostThread(T,pre);
- pre->rchild=NULL;
- pre->rtag=1;
- }
- }
- ThreadNode *FirstNode(ThreadNode *p)
- {
- //找到最左边叶节点
- while (p->ltag==0) {
- p=p->lchild;
- }
- return p;
- }
- ThreadNode *NextNode(ThreadNode *p)
- {
- if(p->rtag==0)
- return FirstNode(p->rchild);
- else {
- return p->rchild;
- }
- }
- void InOrder1(ThreadTree T)
- {
- for(ThreadNode *p = FirstNode(T);p!=NULL;p=NextNode(p)){
- visit_Thread(p);
- }
- }
- ThreadNode *LastNode(ThreadNode *p)
- {
- while (p->rtag==0) {
- p=p->rchild;
- }
- return p;
- }
- ThreadNode *preNode(ThreadNode *p){
- if (p->ltag==0) {
- return LastNode(p->lchild);
- }
- else {
- return p->lchild;
- }
- }
- void RevInOrder(ThreadTree T)
- {
- for(ThreadNode *p = LastNode(T);p!=NULL;p=preNode(p)){
- visit_Thread(p);
- }
- }
- ThreadNode *NextNode_pre(ThreadNode *p)
- {
- if(p->rtag==0){
- if(p->lchild!=NULL)
- return p->lchild;
- if(p->rchild!=NULL)
- return p->rchild;
- }
- else {
- return p->rchild;
- }
- }
- ThreadNode *preNode_post(ThreadNode *p)
- {
- if(p->ltag==1){
- return p->lchild;
- }
- else {
- if(p->rchild!=NULL)
- return p->rchild;
- else {
- return p->lchild;
- }
- }
- }
- typedef struct BSTNode{
- ElemType data;
- BSTNode *lchild,*rchild;
- }BSTNode,*BSTree;
- BSTree CreatBSTree(ElemType rootdata)
- {
- BSTNode *NewNode = (BSTNode*)malloc(sizeof (BSTNode));
- NewNode->data=rootdata;
- NewNode->lchild=NULL;
- NewNode->rchild=NULL;
- return NewNode;
- }
- BSTNode *CreatBSTNode(ElemType data)
- {
- BSTNode *NewNode = (BSTNode*)malloc(sizeof (BSTNode));
- NewNode->data=data;
- NewNode->lchild=NULL;
- NewNode->rchild=NULL;
- return NewNode;
- }
- BSTNode *BSTSearch(BSTree bt, ElemTpye key)
- {
- if(bt==NULL)
- return NULL;
- if(bt->data==key)
- return bt;
- else if (bt->data>key) {
- return BSTSearch(bt->lchild,key);
- }
- else {
- return BSTSearch(bt->rchild,key);
- }
- }
- BSTNode *BSTSearch(BSTree bt, ElemType key)
- {
- while(bt!=NULL and key!=bt->data){
- if(key
data) - bt=bt->lchild;
- else {
- bt=bt->rchild;
- }
- }
- return bt;
- }
- bool BSTInsert(BSTree &bt, ElemType key)
- {
- if(bt==NULL){
- BSTNode *Newnode = CreatBSTNode(key);
- bt=Newnode;
- return true;
- }
- if(bt->data==key){
- return false;
- }
- else if(bt->data>key){
- return BSTInsert(bt->lchild,key);
- }
- else {
- return BSTInsert(bt->rchild,key);
- }
-
- }
- void CreatBST(BSTree &bt, int key[], int n)//key[] 关键字数组,n 关键字数组长度
- {
- bt = NULL;
- for (int i=0;i
- BSTInsert(bt,key[i]);
- }
- }