1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct BiNode{ 5 char data; 6 struct BiNode* LChild; 7 struct BiNode* RChild; 8 }BiNode,*BiTree; 9 10 void CreateBiTree(BiTree &T);//创建二叉树 11 int GetTreeHeight(BiTree T);//返回二叉树高度 12 int GetAllNode(BiTree T);//返回二叉树结点数 13 int GetZeroNode(BiTree T);//返回出度为0的结点数 14 int GetOneNode(BiTree T);//返回出度为1的结点数 15 int GetTwoNode(BiTree T);//返回出度为2的结点数 16 BiTree CopyBiTree(BiTree T);//复制二叉树并将副本返回 17 BiTree GetExchange(BiTree T);//交换二叉树左右子树并返回交换后的二叉树 18 int IsSimilar(BiTree T1,BiTree T2);//判断两个二叉树是否相似 19 int GetNodePosition(BiTree T,char c);//获取结点c在二叉树中所在层数 20 21 void main() 22 { 23 char clear,c; 24 BiTree T1,T2,copyT1; 25 CreateBiTree(T1); 26 scanf("%c",&clear); 27 28 CreateBiTree(T2); 29 scanf("%c",&clear); 30 31 printf("Height:%d\n",GetTreeHeight(T1)); 32 printf("All Node:%d\n",GetAllNode(T1)); 33 printf("Zero Node:%d\n",GetZeroNode(T1)); 34 printf("One Node:%d\n",GetOneNode(T1)); 35 printf("Two Node:%d\n",GetTwoNode(T1)); 36 37 scanf("%c",&c); 38 printf("%c at %d\n",c,GetNodePosition(T1,c)); 39 40 copyT1 = CopyBiTree(T1); 41 42 if (IsSimilar(T1,T2)) 43 { 44 printf("Similar!\n"); 45 } 46 else 47 { 48 printf("No Similar!\n"); 49 } 50 } 51 52 void CreateBiTree(BiTree &T) 53 { 54 char c; 55 scanf("%c",&c); 56 if (c == ' ') 57 T = NULL; 58 else 59 { 60 T = (BiNode *)malloc(sizeof(BiNode)); 61 T->data = c; 62 CreateBiTree(T->LChild); 63 CreateBiTree(T->RChild); 64 } 65 } 66 int GetTreeHeight(BiTree T) 67 { 68 int l,r; 69 l = r = 0; 70 if (T->LChild) l = GetTreeHeight(T->LChild); 71 if (T->RChild) r = GetTreeHeight(T->RChild); 72 if (T->LChild == NULL && T->RChild == NULL) l=r=1; 73 return l>r?l:r; 74 } 75 int GetAllNode(BiTree T) 76 { 77 return T==NULL?0:(GetAllNode(T->LChild)+GetAllNode(T->RChild)+1); 78 } 79 int GetZeroNode(BiTree T) 80 { 81 if (T==NULL) return 0; 82 return (T->LChild==NULL&&T->RChild==NULL)?1:(GetZeroNode(T->LChild)+GetZeroNode(T->RChild)); 83 } 84 int GetOneNode(BiTree T) 85 { 86 int i; 87 if (T==NULL) return 0; 88 i = GetOneNode(T->LChild)+GetOneNode(T->RChild); 89 if ((T->LChild||T->RChild) && !(T->LChild && T->RChild)) i++; 90 return i; 91 } 92 int GetTwoNode(BiTree T) 93 { 94 int i; 95 if(T == NULL ) return 0; 96 i = GetTwoNode(T->LChild)+GetTwoNode(T->RChild); 97 if (T->LChild && T->RChild) i++; 98 return i; 99 } 100 BiTree CopyBiTree(BiTree T) 101 { 102 BiTree cT; 103 if (T == NULL) return NULL; 104 cT = (BiNode *)malloc(sizeof(BiNode)); 105 cT->data = T->data; 106 cT->LChild = CopyBiTree(T->LChild); 107 cT->RChild = CopyBiTree(T->RChild); 108 return cT; 109 } 110 BiTree GetExchange(BiTree T) 111 { 112 BiTree p; 113 if(T == NULL) return NULL; 114 p = GetExchange(T->LChild); 115 T->LChild = GetExchange(T->RChild); 116 T->RChild = p; 117 return T; 118 } 119 int IsSimilar(BiTree T1,BiTree T2) 120 { 121 int l,r; 122 if (T1 == NULL && T2 == NULL) return 1; 123 if (T1==NULL&&T2 || T1&&T2==NULL) return 0; 124 l = IsSimilar(T1->LChild,T2->LChild); 125 r = IsSimilar(T1->RChild,T2->RChild); 126 return l&&r?1:0; 127 } 128 int GetNodePosition(BiTree T,char c) 129 { 130 int l=0,r=0; 131 if (T->data == c) return 1; 132 if (T->LChild!=NULL) l=GetNodePosition(T->LChild,c); 133 if (T->RChild!=NULL) r=GetNodePosition(T->RChild,c); 134 return l+r?l+r+1:0; 135 }