/// c language
/// Enter the binary tree as instructed and enter a node
/// You can get the path from the root to the node
/// My programming foundation is poor and clumsy, forgive
#include <stdio.h>
#include <stdlib.h>
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode * lchild, * rchild;
int flag; // / Homemade logo 1 || 0
} BiTNode, * BiTree, * bitree;
typedef bitree SElemType;
typedef struct {// sequence stack
SElemType * base;
SElemType * top;
int StackSize; // currently allocated storage space of the stack
} SqStack;
SqStack s, w; // w is used for storage path
// First build a binary tree
BiTree CreateTree ()
{
char ch;
BiTree T;
scanf ("% c", & ch);
if (ch == '#') T = NULL;
else
{
T = (BiTree) malloc (sizeof (BiTNode));
T-> data = ch;
T-> flag = 1; // 1 means not accessed
T-> lchild = CreateTree ();
T-> rchild = CreateTree ();
}
return T; // Return to the root node
}
int InitStack (SqStack & S) // Initial MOOC of sequential stack
{// Construct an empty stack S
S.base = (SElemType *) malloc (100 * sizeof (SElemType));
if (NULL == S.base) exit ( -1); // Failed to allocate storage
//if(NULL==S.base) exit (OVERFLOW); // Failed to allocate storage, Teacher Hu suggested
S.top = S.base; // top is initially base
S. StackSize = 10;
return 1;
}
int Push (SqStack & S, SElemType e)
// Insert e as the new top element of the stack, if the stack space is not enough, automatically expand (realloc function)
{
if (S.top-S.base> = S.StackSize)
{
S. base = (SElemType *) realloc (S.base, (S.StackSize + 10) * sizeof (SElemType));
if (! S.base) return -1; // Storage allocation failed
S.top = S.base + S.StackSize;
S.StackSize + = 10;
}
* S.top ++ = e; // The element e is pushed into the top of the stack, and the pointer at the top of the stack is increased by 1 // The element at the top of the stack is not saved
return 1;
}
int GetTop (SqStack S, SElemType & e)
{// Use e to return the top element of S
if (S.top == S.base) return -1; // Stack empty
e = * (S.top-1);
return 1;
}
int Pop (SqStack & S, SElemType & e) //
Remove the stack and delete the top element of the stack {// Delete the top element of S and return its value with e
if (S.top == S.base) return -1;
e = *-S.top; // Decrease the top pointer of the stack by 1, and assign the top element to e // The element at the top of the stack does not
return 1;
}
int Search2 (bitree t, char & x, SqStack & w) // path
{
bitree p = t, q = NULL, v;
p-> flag = 0; // 0 means it has been accessed
Push (w, p);
while ( 1) // The stack is not empty
{
if (p-> data == x) // Find
{
break;
}
else if (p-> lchild! = NULL && p-> lchild-> flag! = 0) // Left
{
p -> lchild-> flag = 0; // 0 means it has been accessed
Push (w, p-> lchild);
p = p-> lchild;
}
else if ((p-> lchild == NULL | p-> lchild-> flag == 0)
&& p-> rchild! = NULL) // There is
{
p-> rchild-> flag = 0; // 0 means it has been accessed
Push (w, p-> rchild);
p=p->rchild;
}
else if (p->lchild==NULL&&p->rchild==NULL)//叶子结点
{
Pop(w,q);
GetTop(w,q);
p = q;
}
else
{
Pop(w,v);
GetTop(w,q);
p = q;
}
}
}
int main ()
{
BiTree T, a;
int i = 0;
char x;
char as [100] = {0};
printf ("Please enter the tree, the child node of the leaf node is represented by #: \ n") ;
InitStack (s);
InitStack (w);
T = CreateTree ();
getchar ();
printf ("Please enter search character: \ n");
scanf ("% c", & x);
Search2 (T, x, w); // Find path
while (w.base! = w.top)
{
Pop (w, a);
as [i] = a-> data;
i ++;
}
printf ("The path of this node is:") ;
for (; i> = 0; i--)
{// The array is used to reverse the value in the stack
if (as [i]! = 0 && i! = 0)
printf ("% c->", as [ i]);
else
printf ("% c", as [i]);
}
return 0;
}
/// Welcome axe and suggestions!