数据结构与算法-实验7-二叉树基础

 1 #define Tree_Level tree_Level(Root, 0)
 2 
 3 struct DataType
 4 {
 5     char data[9];
 6 };
 7 struct BinaryTree
 8 {
 9     DataType  Data;
10     BinaryTree* Lchild;
11     BinaryTree* Rchild;
12     int x;
13     int y;
14 };
15 
16 BinaryTree *Root=NULL;
17 
18 BinaryTree* Grow()
19 {
20     BinaryTree* Leaf;
21     Leaf = new BinaryTree;
22     Leaf->Lchild = NULL;
23     Leaf->Rchild = NULL;
24     strcpy_s(Leaf->Data.data,"");
25     return Leaf;
26 }
27 
28 bool tree_IsEmpty(BinaryTree* tree)
29 {
30     if (Root==NULL)
31         return 1;
32     else return 0;
33 }
34 
35 void tree_PreOrder(BinaryTree* T)
36 {
37     if (T==NULL)
38         return ;
39     else
40     {
41         printf_s("%s
", T->Data.data);
42         tree_PreOrder(T->Lchild);
43         tree_PreOrder(T->Rchild);
44     }
45 }
46 void tree_MidOrder(BinaryTree* T)
47 {
48     if (T == NULL)
49         return;
50     else
51     {
52         tree_PreOrder(T->Lchild);
53         printf_s("%s
", T->Data.data);
54         tree_PreOrder(T->Rchild);
55     }
56 }
57 void tree_BackOrder(BinaryTree* T)
58 {
59     if (T == NULL)
60         return;
61     else
62     {
63         tree_PreOrder(T->Lchild);
64         tree_PreOrder(T->Rchild);
65         printf_s("%s
", T->Data.data);
66     }
67 }
68 
69 int tree_Level(BinaryTree* T,int NowLevel)//获取最大层次        
70 {
71     int MaxLevel=0;
72     if (T == NULL)
73         return NowLevel-1;
74     else
75     {
76         NowLevel++;
77         int L_Level=tree_Level(T->Lchild, NowLevel);
78         int R_Level=tree_Level(T->Rchild, NowLevel);
79         MaxLevel = L_Level > MaxLevel ? L_Level : MaxLevel;
80         MaxLevel = R_Level > MaxLevel ? R_Level : MaxLevel;
81     }
82     if (NowLevel > MaxLevel)
83         MaxLevel = NowLevel;
84     return MaxLevel;
85 }
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include"BinaryTree.h"
 4 
 5 int tree_serch(BinaryTree* T,char* s,int Level)//level:层数
 6 {
 7     if (T == NULL)
 8         return 0;
 9     else
10     {
11         if (!strcmp(T->Data.data, s))
12         {
13             return Level;
14         }
15         int L_Level =tree_serch(T->Lchild , s , Level + 1);
16         if (L_Level != 0)
17             return L_Level;
18 
19         int R_Level = tree_serch(T->Rchild, s, Level + 1);
20         if (R_Level != 0)
21             return R_Level;
22     }
23 }
24 int tree_serch(char* s)//重载 默认情况
25 {
26     return tree_serch(Root, s, 1);
27 }
28 
29 30 
31 int main()
32 {
40     Root                            = Grow();        strcpy_s(Root                            ->Data.data, "A");
41     Root->Lchild                    = Grow();        strcpy_s(Root->Lchild                    ->Data.data, "B");
42     Root->Lchild->Rchild            = Grow();        strcpy_s(Root->Lchild->Rchild            ->Data.data, "D");
43     Root->Lchild->Rchild->Lchild    = Grow();        strcpy_s(Root->Lchild->Rchild->Lchild    ->Data.data, "F");
44     Root->Lchild->Rchild->Rchild    = Grow();        strcpy_s(Root->Lchild->Rchild->Rchild    ->Data.data, "G");
45     Root->Rchild                    = Grow();        strcpy_s(Root->Rchild                    ->Data.data, "C");
46     Root->Rchild->Rchild            = Grow();        strcpy_s(Root->Rchild->Rchild            ->Data.data, "E");
47     Root->Rchild->Rchild->Rchild    = Grow();        strcpy_s(Root->Rchild->Rchild->Rchild    ->Data.data, "H");
48     
51 
52     printf_s("请输入要搜索的结点
");
53     char s[9] = {0};
54     scanf_s("%s", s,sizeof(s));
55     printf_s("%d
", tree_serch(s));
56     return 0;
57 }
原文地址:https://www.cnblogs.com/HeLongYi/p/12796018.html