【数据结构学习日记一】
前言:以下内容涉及到栈以及二叉树的基本知识。
功能介绍:在一个二叉树中查询数据,存在返回该数据的层数,不存在返回0。
问题分析:
1.用什么方法遍历一棵树
2.怎样计算树节点的层数
程序框架:
1 int main() 2 { 3 建树(); 4 获取需要查询的数据(); 5 查询功能(); 6 输出(); 7 }
主函数代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "stack.h" 4 5 int main() 6 { 7 int high; 8 BiTree tree=(BiTree)malloc(sizeof(Bitnode));//初始化树节点 9 CreateTree(&tree);//创建一颗树 10 11 printf("先序遍历:"); 12 PreOrder(tree); 13 printf(" "); 14 15 printf("中序遍历:"); 16 InOrder(tree); 17 printf(" "); 18 19 DataType str;//存放需要查询的数据 20 printf("input word of need to find "); 21 fflush(stdin); 22 scanf("%c",&str); 23 24 high=Find(tree,str);//进入功能函数 25 printf("%d",high); 26 }
这里有一个小问题,如果把" flush(stdin); "去了会发生什么情况呢?
功能函数代码:
int Find(BiTree root,DataType ch)//root为树的根,ch为查询的数据 { int high=0,flag=0;//high为累加器,flag为查询是否成功的标志,成功为1失败为0 struct TreeNode*TreeRoot;//定义一个temp存放出栈后的节点,TreeRoot存放根节点 struct Dstack s;//定义一个名为s的栈 TreeRoot=root; InitStack(&s);//栈的初始化 while(root||!IsEmpty(&s)) { while(root) { if(ch==root->data) { flag=1;break; } //如果等于根节点的右子节点,则重置高度 if(root->data==TreeRoot->RChild->data) { high=1; PushStack(&s,root); } else if(root->data!=ch) { PushStack(&s,root); } high++; root=root->LChild;//当前结点指向左节点 } if(flag==1) { break; } //如果堆栈里还有节点,就pop一个节点,然后root=pop出来的节点的右节点 if(!IsEmpty(&s)) { root=PopStack(&s); root=root->RChild; } } if(flag==1) { return high+1; } else { return 0; } }
输入用例:
例1:AB..C..
例2:ABC..D..E.F.G..
为了能让大家理解的更清晰,我把栈和树的实现代码也附上
栈和树的实现代码:
1 /* Stack */ 2 3 //-----------------------Structure of Stack------------------------------------- 4 #define Maxsize 20 5 #define NIL NULL 6 typedef struct Dstack* Stack;//定义Stack为 struct Dstack的指针类型 7 typedef struct TreeNode* ElementType; // ElementType为字符类型 8 struct Dstack 9 { 10 ElementType Data[Maxsize]; 11 int Top; 12 }S; 13 //--------------------InitStack Structure of Stack------------------------------ 14 int InitStack(Stack p) 15 { 16 p->Top=-1; 17 int i; 18 for(i=0;i<Maxsize;i++) 19 { 20 p->Data[i]=NIL; 21 } 22 } 23 //--------------------PushStack Structure of Stack------------------------------ 24 int PushStack(Stack Ptrs,ElementType item) 25 { 26 if(Ptrs->Top==Maxsize) 27 { 28 printf("堆栈满了");return 0; 29 }else if(item==NULL) 30 { 31 return 0; 32 } 33 Ptrs->Data[++(Ptrs->Top)]=item; 34 return 1; 35 } 36 //--------------------PopStack Structure of Stack------------------------------- 37 ElementType PopStack(Stack Ptrs) 38 { 39 if(Ptrs->Top==-1) 40 { 41 printf("堆栈1空了");return NULL; 42 } 43 return(Ptrs->Data[Ptrs->Top--]); 44 } 45 //--------------------IsEmpty Structure of Stack-------------------------------- 46 bool IsEmpty(Stack Ptrs) 47 { 48 if(Ptrs->Top==-1) 49 { 50 return true; 51 } 52 else 53 { 54 return false; 55 } 56 } 57 /* End Stack */ 58 59 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 60 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 61 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 62 63 /* Tree */ 64 65 //-----------------------Structure of Tree-------------------------------------- 66 67 typedef char DataType; 68 typedef struct TreeNode { 69 DataType data; 70 struct TreeNode *LChild; 71 struct TreeNode *RChild; 72 }Bitnode, *BiTree; 73 //-----------------------Create Function of Tree-------------------------------- 74 void CreateTree(BiTree *t) 75 { 76 DataType ch; 77 ch=getchar(); 78 if(ch!='.'){ 79 (*t)=(BiTree)malloc(sizeof(Bitnode)); 80 (*t)->LChild=(*t)->RChild=NULL; 81 (*t)->data=ch; 82 CreateTree(&((*t)->LChild)); 83 CreateTree(&((*t)->RChild)); 84 }else 85 { 86 (*t)=NULL; 87 } 88 } 89 //-----------------------PreOrder Function of Tree------------------------------ 90 void PreOrder(BiTree root) 91 { 92 if ( root!=NULL ) 93 { 94 printf("%c ",root->data);//访问根结点 95 PreOrder(root->LChild); 96 PreOrder(root->RChild); 97 } 98 } 99 //-----------------------InOrder Function of Tree------------------------------- 100 void InOrder(BiTree root) 101 { 102 if ( root!=NULL ) 103 { 104 PreOrder(root->LChild); 105 printf("%c ",root->data);//访问根结点 106 PreOrder(root->RChild); 107 } 108 } 109 /* End Tree */ 110 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 111 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 112 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
例