Problem description
Looking for a node with a value of x in a binary tree, try to write an algorithm that prints all the ancestor nodes of a node with a value of x, assuming no more than one node with a value of X
Algorithmic thinking
Because only the ancestor nodes are printed, consider using a non-recursive post-order traversal method.
In a non-recursive sequential traversal, all elements that remain in the stack (except the top of the stack) are necessarily ancestor nodes at the top of the stack, so just find the X node and then stack all the nodes in the stack.
Algorithm description
voidAncestorx (bitnode* T,elemtype x) {Sqstack S; Initstack (&s); bitnode* p=t; bitnode* R=null; while(p| | Isemptystack (&s)) {if(P!=null) {Push (&s,p); p=p->lchild; }Else{p=gettop (&s);if(P->RCHILD!=NULL&&P->RCHILD!=R) {p=p->rchild; Push (&S,P); p=p->lchild; }Else{P=pop (&s);if(p->data==x) { while(Isemptystack (&s)! =0) {P=pop (&s);printf("%c", P->data); }} r=p; P=null; } } } }
See the attached code for details.
Attachment
//ab#dg## #CE # #F # ##include <stdio.h>#include <stdlib.h>#define MaxSizetypedef CharElemtype;typedef structbitnode{elemtype data;structBitnode *lchild, *rchild;} Bitnode,*bitree;typedef structsqstack{bitnode* Data[maxsize];intTop;} Sqstack;voidInitstack (sqstack*);voidPush (sqstack*,bitnode*); bitnode* Pop (sqstack*); bitnode* GetTop (sqstack*);intIsemptystack (sqstack*); Bitree Createbitree (bitnode*);voidAncestorx (Bitnode*,elemtype);voidJudge (bitnode*);voidPreorder (bitnode*);voidInorder (bitnode*);//-------------------------------------------intMainintargcChar* argv[]) {bitnode* t= (bitnode*)malloc(sizeof(Bitnode)); T=createbitree (T); Judge (T); Ancestorx (T,' F ');printf("\ n");return-1;}//-------------------------------------------Bitree Createbitree (bitnode* T) {elemtype x;scanf("%c", &x);if(x==' # '){returnNULL; } t= (bitnode*)malloc(sizeof(Bitnode)); t->data=x; T->lchild=createbitree (T->lchild); T->rchild=createbitree (T->rchild);returnT;}voidAncestorx (bitnode* T,elemtype x) {Sqstack S; Initstack (&s); bitnode* p=t; bitnode* R=null; while(p| | Isemptystack (&s)) {if(P!=null) {Push (&s,p); p=p->lchild; }Else{p=gettop (&s);if(P->RCHILD!=NULL&&P->RCHILD!=R) {p=p->rchild; Push (&S,P); p=p->lchild; }Else{P=pop (&s);if(p->data==x) { while(Isemptystack (&s)! =0) {P=pop (&s);printf("%c", P->data); }} r=p; P=null; } } } }//-------------------------------------------voidJudge (bitnode* T) {printf("---------------\ n"); Preorder (T);printf("\ n"); Inorder (T);printf("\ n");}voidPreorder (bitnode* T) {if(T==null) {return; }printf("%c", T->data); Preorder (T->lchild); Preorder (t->rchild);}voidInorder (bitnode* T) {if(T==null) {return; } inorder (T->lchild); Inorder (T->rchild);printf("%c", t->data);}//-------------------------------------------voidInitstack (sqstack* S) {s->top=-1;}voidPush (sqstack* S, bitnode* T) {if(s->top==maxsize-1){return; } s->data[++s->top]=t;} bitnode* Pop (sqstack* S) {if(s->top==-1){returnNULL; }returns->data[s->top--];} bitnode* GetTop (sqstack* S) {if(s->top==-1){returnNULL; }returnS->data[s->top];}intIsemptystack (sqstack* S) {if(s->top==-1){return 0; }return-1;}
4th Chapter 1th Exercises 8 Print the ancestor node of the specified node