# 4th Chapter 1th Exercises 8 Print the ancestor node of the specified node

Source: Internet
Author: User

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-&GT;RCHILD!=NULL&AMP;&AMP;P-&GT;RCHILD!=R)                {p=p->rchild;                Push (&AMP;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-&GT;RCHILD!=NULL&AMP;&AMP;P-&GT;RCHILD!=R)                {p=p->rchild;                Push (&AMP;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

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.