[分享] 單班計程11.3 測試用main

看板b99902HW作者 (小小郭)時間15年前 (2010/12/02 00:55), 編輯推噓4(400)
留言4則, 4人參與, 最新討論串1/1
喵~這次就幫大家寫 以後檢查code要嘗試自己寫呀@_@a... ----------------- #include<stdio.h> struct treenode{ int data; struct treenode *left,*right; }; /********************************* Put your code here. **********************************/ int print(struct treenode *root,int depth){ if( root==NULL )return 0; int sum=0; sum+=print( root->left , depth+1 ); printf("level:%-5d| %*s\n",depth,2*depth,"oo"); sum+=print( root->right, depth+1 ); return sum+depth; } void insert(struct treenode *root,struct treenode *ptr){ if( ptr->data < root->data ) root->left==NULL?root->left=ptr:insert(root->left,ptr); else root->right==NULL?root->right=ptr:insert(root->right,ptr); } #include<time.h> int main(){ srand(time(NULL)); int n=10,ary[1000],i; struct treenode *root=NULL,*ptr=NULL; for(i=0;i<n;++i)ary[i] = i; for(i=0;i<n;++i){ int a = rand()%n; int b = rand()%n; int tmp=ary[a]; ary[a]=ary[b]; ary[b]=tmp; } for(i=0;i<n;++i){ ptr = (struct treenode*)malloc(sizeof(struct treenode)); ptr->data = ary[i]; ptr->left = ptr->right =NULL; if( root==NULL )root = ptr; else insert( root, ptr ); } puts("Print the tree:"); printf("Real Level-Sum of this tree is %d.\n",print( root,1 )); printf("Your Level-Sum of this tree is %d.\n",level_sum(root)); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.160.236.199

12/02 16:46, , 1F
謝謝胖胖郭Q//////Q
12/02 16:46, 1F

12/02 17:45, , 2F
胖郭Q//////Q
12/02 17:45, 2F

12/02 22:35, , 3F
胖胖郭Q//////Q
12/02 22:35, 3F

12/04 19:16, , 4F
胖胖郭Q//////Q
12/04 19:16, 4F
文章代碼(AID): #1Czdt-za (b99902HW)