1. 程式人生 > 資訊 >效能叫板 PS5,特斯拉第三代車機詳解:搭載 AMD 全家桶,已應用於國產 Model Y 高效能版

效能叫板 PS5,特斯拉第三代車機詳解:搭載 AMD 全家桶,已應用於國產 Model Y 高效能版

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #define MaxSize 100
  4 /*二叉樹的二叉連結串列儲存結構*/
  5 typedef char dataType;
  6 typedef struct node
  7 {
  8     dataType data;
  9     struct node *lchild, *rchild; /**左右孩子指標**/
 10 } *BinTree,BinNode; /**結點型別**/
 11 typedef BinTree QelemType;
 12 typedef struct
13 { 14 QelemType num[MaxSize]; 15 int front; 16 int rear; 17 } Queue; 18 Queue Q; 19 void initilize() ///初始化佇列 20 { 21 Q.front = 0; 22 Q.rear = 0; 23 } 24 int Push(BinNode *T) ///入隊 25 { 26 if((Q.rear+1)==Q.front) 27 return 0; 28 else 29 Q.num[++Q.rear] = T;
30 return 1; 31 } 32 33 BinNode *Pop() ///出隊 34 { 35 if(Q.front==Q.rear) 36 return 0; 37 return Q.num[++Q.front]; 38 } 39 40 int Empty() ///判斷對列是否為空 41 { 42 if(Q.front==Q.rear) 43 return 1; 44 else 45 return 0; 46 } 47 ///建立二叉樹 48 int CreateBiTree(BinTree T)///
輸入字元序列,先序遍歷建立二叉連結串列 49 { 50 char ch; 51 ch = getchar(); ///輸入一個字元 52 if(ch == '#') 53 { 54 T = NULL; 55 } 56 else ///如果結點不為空 57 { 58 T = (BinNode*)malloc(sizeof(BinNode)); ///申請一個結點空間 59 if(!T) 60 { 61 return 0; 62 } 63 T->data = ch; 64 CreateBiTree(T->lchild);///建立左子樹 65 CreateBiTree(T->rchild);///建立右子樹 66 } 67 return 1; 68 } 69 ///中序遍歷 70 void inOder(BinTree T) 71 { 72 if(!T) 73 return ; 74 if(T) 75 { 76 inOder(T->lchild); 77 printf("%c ", T->data); 78 inOder(T->rchild); 79 } 80 } 81 ///先序遍歷 82 void PreOder(BinTree T) 83 { 84 if(!T) 85 return ; 86 if(T) 87 { 88 printf("%c ", T->data); 89 PreOder(T->lchild); 90 PreOder(T->rchild); 91 } 92 } 93 ///後序遍歷 94 void PostOder(BinTree T) 95 { 96 if(!T) 97 return ; 98 if(T) 99 { 100 PostOder(T->lchild); 101 PostOder(T->rchild); 102 printf("%c ", T->data); 103 } 104 } 105 ///二叉樹的層次遍歷 106 void LevelOrder(BinTree T) 107 { 108 BinNode *temp; 109 if(!T) 110 return; 111 Push(T); 112 while (!Empty()) 113 { 114 temp = Pop(); 115 printf("%c ", temp->data); ///輸出隊首結點 116 if(temp->lchild) ///把Pop掉的結點的左子結點加入佇列 117 Push(temp->lchild); 118 if(temp->rchild) ///把Pop掉的結點的右子結點加入佇列 119 Push(temp->rchild); 120 } 121 } 122 ///二叉樹的深度 123 int Depth(BinTree T) 124 { 125 int hl, hr; 126 if(!T) 127 return 0; 128 else 129 { 130 hl = Depth(T->lchild); 131 hr = Depth(T->rchild); 132 if(hl >= hr) 133 return hl + 1; 134 else 135 return hr + 1; 136 } 137 } 138 ///求葉子節點的個數 139 void CountLeaf(BinTree T,int cnt) 140 { 141 if(T) 142 { 143 if((!T->lchild) && (!T->rchild)) 144 cnt++; 145 CountLeaf(T->lchild,cnt); 146 CountLeaf(T->rchild,cnt); 147 } 148 } 149 ///交換左右子樹 150 int exchange(BinTree T) 151 { 152 BinNode *temp; 153 if(!T) 154 return 0; 155 if(T->lchild == NULL && T->rchild == NULL) 156 return 0; 157 else 158 { 159 temp = T->lchild; 160 T->lchild = T->rchild; 161 T->rchild = temp; 162 } 163 if(T->lchild) 164 exchange(T->lchild); 165 if(T->rchild) 166 exchange(T->rchild); 167 return 1; 168 } 169 void menu() 170 { 171 printf("--------------------二叉樹的建立與應用-------------------------\n"); 172 printf("\t1.二叉樹的中序遍歷\n"); 173 printf("\t2.二叉樹的前序遍歷\n"); 174 printf("\t3.二叉樹的後序遍歷\n"); 175 printf("\t4.二叉樹的層次遍歷\n"); 176 printf("\t5.二叉樹的深度\n"); 177 printf("\t6.二叉樹葉子結點的個數\n"); 178 printf("\t7.二叉樹每個結點的左右子樹交換位置\n"); 179 printf("\t8.退出系統\n"); 180 printf("----------------------------------------------------------------\n"); 181 } 182 int main() 183 { 184 BinTree T; 185 int m,cnt=0; 186 menu(); 187 printf("按先序序列建立一個二叉樹:\n"); 188 if(CreateBiTree(T)==1) 189 printf("建立成功!\n"); 190 while(1) 191 { 192 scanf("%d", &m); 193 if(m == 1) 194 { 195 printf("中序遍歷為:\n"); 196 inOder(T); 197 printf("\n"); 198 } 199 else if(m == 2) 200 { 201 printf("先序遍歷為:\n"); 202 PreOder(T); 203 printf("\n"); 204 } 205 else if(m == 3) 206 { 207 printf("後序遍歷為:\n"); 208 PostOder(T); 209 printf("\n"); 210 } 211 else if(m == 4) 212 { 213 printf("層次遍歷為:\n"); 214 initilize(); 215 LevelOrder(T); 216 printf("\n"); 217 } 218 else if(m == 5) 219 { 220 printf("二叉樹深度為:\n"); 221 printf("%d\n", Depth(T)); 222 } 223 else if(m == 6) 224 { 225 printf("二叉樹葉子結點數為:\n"); 226 CountLeaf(T,cnt); 227 printf("%d\n",cnt); 228 } 229 else if(m == 7) 230 { 231 printf("左右子樹進行交換:\n"); 232 if(exchange(T)==1) 233 printf("交換完成!\n"); 234 printf("交換後中序遍歷為:\n"); 235 inOder(T); 236 printf("\n"); 237 printf("交換後先序遍歷為:\n"); 238 PreOder(T); 239 printf("\n"); 240 printf("交換後後序遍歷為:\n"); 241 PostOder(T); 242 printf("\n"); 243 printf("交換後層次遍歷為:\n"); 244 initilize(); 245 LevelOrder(T); 246 printf("\n"); 247 } 248 else 249 break; 250 } 251 return 0; 252 }

個人學習所用;