查找二叉树指定结点的层次

我建了两个栈,每次遍历一个层次以后就换一个栈,我只想到了这种方法,但感觉这做法不是很好。

 1 /*
 2 假设二叉树采用链式方式存储,t为其根结点,编写一个函数int Depth(bintree t, char x),求值为x的结点在二叉树中的层次。
 3 */
 4 #include "bintree.h"
 5 char *a="ABC##D##EF#G###";          /*扩充二叉树序树t的前序序列*/
 6 
 7 /*
 8      函数Depth,功能:求结点x所在的层次
 9 */
10 int Depth(bintree t,char x)
11 {
12     int k=1,flag=1;
13     binnode *p=t;
14     binnode *q;
15     seqstack L1,L2;    
16     init(&L1);
17     init(&L2);
18     push(&L1,p);
19     while(1)
20     {
21        if(flag==1)
22        {
23           while(!empty(&L1))
24           {
25               q=pop(&L1);
26               if(q->data==x)  return k;
27               if(q->lchild!=NULL)  push(&L2,q->lchild);
28               if(q->rchild!=NULL)  push(&L2,q->rchild);
29           }
30           k++;
31           flag=0;
32        }
33        else if(flag==0)
34        {
35           while(!empty(&L2))
36           {
37               q=pop(&L2);
38               if(q->data==x)  return k;
39               if(q->lchild!=NULL)  push(&L1,q->lchild);
40               if(q->rchild!=NULL)  push(&L1,q->rchild);
41           }
42           k++;
43           flag=1;
44        }
45     }
46 }
47 
48 int main()
49 {  bintree root;
50    char x;
51    int k=0;
52    root=creatbintree();
53    printf("请输入树中的1个结点值:\n");
54    scanf("%c",&x);
55    k=Depth(root,x);
56    printf("%c结点的层次为%d\n",x,k);
57 }

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 100
 4 extern char *a;     /*存放扩充二叉树的前序序列*/
 5 typedef struct node /*二叉树结构定义*/
 6 {
 7     char data;
 8     struct node *lchild,*rchild;
 9 }binnode;
10 typedef binnode *bintree;
11 
12 /*函数creatbintree (根据扩充二叉树的前序序列(字符串a)建立二叉树t的存储结构*/
13 bintree  creatbintree()
14 {
15     char ch=*a++;
16     bintree t;
17     if  (ch=='#')  t=NULL;
18     else
19     { t=(bintree)malloc(sizeof(binnode));
20       t->data=ch;
21       t->lchild=creatbintree();
22       t->rchild=creatbintree();
23     }
24     return t;
25 }
26 
27 void preorder(bintree t)  /*前序递归遍历二叉树*/
28 {
29     if (t)
30     {
31         printf("%c",t->data);
32         preorder(t->lchild);
33         preorder(t->rchild);
34     }
35 }
36 void postorder(bintree t)  /*后序递归遍历二叉树*/
37 {
38     if (t)
39     {
40 
41         postorder(t->lchild);
42         postorder(t->rchild);
43         printf("%c",t->data);
44     }
45 }
46 
47 /*顺序栈定义*/
48 typedef struct
49 {
50     bintree data[N];
51     int top;
52     int tag[N];
53 }seqstack;
54 
55 void init(seqstack *s)   /*初始化空栈*/
56 {
57     s->top=-1;
58 }
59 int empty(seqstack *s)   /*判断栈是否为空*/
60 {
61     if (s->top>-1) return 0;
62     else return 1;
63 }
64 int full(seqstack *s)   /*判断栈是否为满*/
65 {
66     if (s->top==N-1) return 1;
67     else return 0;
68 }
69 void push(seqstack *s ,bintree x)   /*进栈*/
70 {
71     if (!full(s))
72         s->data[++s->top]=x;
73 }
74 bintree pop(seqstack *s)            /*出栈*/
75 {
76     if (!empty(s))
77         return s->data[s->top--];
78 }

 

posted @ 2016-12-11 15:24  Kayden_Cheung  阅读(5390)  评论(0编辑  收藏  举报
//目录