int main(void){ int i=0, array[]={34,12,65,23,2,100,120}; Node* root = NULL; root = TreeInit(49); for (i=0;i<7;++i){ NodeInsert(root,array[i]); } InorderPrintTree(root); putchar('\n'); PostorderPrintTree(root); printf("\nAddress of node with key=13 is %p\n",(void *)TreeSearch(root,13)); printf("Minimum : %d \n", (TreeMinimum(root))->key); printf("Maximum : %d \n", (TreeMaximum(root))->key); printf("Treesize : %d \n", TreeSize(root)); printf("Max depth : %d \n", MaxDepth(root)); TreeDelete(root); getchar(); return 0; }
Node NodeInsert(Node rootnode , int data){ if (rootnode == NULL){ rootnode = CreatNode(NULL , NULL , data); return rootnode; } if (data == rootnode -> data){ printf("Insert Node %d Failed !\n" , data); } if (data < rootnode -> data){ rootnode -> left = NodeInsert(rootnode -> left , data); rootnode -> height = MAX(Height(rootnode -> left) , Height(rootnode -> right)) + 1; //lose balance int rightheight = 0; if (rootnode -> right != NULL){ rightheight = rootnode -> right ->height; } if (Height(rootnode -> left) >= rightheight + 2){ if (data < rootnode -> left -> data){ rootnode = LL(rootnode); }else{ rootnode = LR(rootnode); } } }else{ if (data > rootnode -> data){ rootnode -> right = NodeInsert(rootnode -> right , data); rootnode -> height = MAX(Height(rootnode -> left) , Height(rootnode -> right)) + 1; //lose balance int leftheight = 0; if (rootnode -> left != NULL){ leftheight = rootnode -> left -> height; } if (Height(rootnode -> right) >= leftheight + 2){ if (data > rootnode -> right -> data){ rootnode = RR(rootnode); }else{ rootnode = RL(rootnode); } } } } rootnode -> height = MAX(Height(rootnode -> left) , Height(rootnode -> right)) + 1; return rootnode; }
int main(void) { printf("This is an application of AVL Tree !\n >_< \n"); printf("please enter the node and end with an -1 : \n"); int node = 0; Node rootnode = NULL; do{ scanf("%d" , &node); if (node == -1){ break; } rootnode = NodeInsert(rootnode , node); printf("Here is the tree : \n"); PrintNode(rootnode , 0 , rootnode -> data); }while(1); printf("Here is the tree : \n"); PrintNode(rootnode , 0 , rootnode -> data); return 0; }
int main(void) { /* 不同花色分组 */ // int card[4][13] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* 不分花色版 */ int cards[13] = {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}; int total = CARDTOTAL; int num, position, i, turn = 1; Node *p; LinkList cardDeck1 = InitList(); LinkList cardDeck2 = InitList(); srand((unsigned int)time(NULL)); printf("扑克牌配对开始,输入1-13的数字抓牌吧。\n"); while (total > 0) { if (turn) { //printf("换行即抽牌"); //while (getchar() != '\n'); printf("玩家正在抽牌"); for (i = 0; i < 3; i++) { _sleep(rand() % 10 * 100); putchar('.'); } while(num = 1+(int)(13.0*rand()/(RAND_MAX+1.0)), cards[num - 1] <= 0); printf("抽到牌:%d", num); putchar('\n'); position = GetElem(cardDeck1, num); p = NodeShow(cardDeck1, position); if (p && p->data == num) { NodeDelete(cardDeck1, position); } else { NodeInsert(cardDeck1, position, num); } total--; cards[num - 1]--; printf("玩家手中牌(NULL表示无):"); if (ElemCount(cardDeck1)) { Traverse(cardDeck1, Show); } else { printf("NULL"); } } else { printf("系统正在抽牌"); for (i = 0; i < 3; i++) { _sleep(rand() % 10 * 100); putchar('.'); } while(num = 1+(int)(13.0*rand()/(RAND_MAX+1.0)), cards[num - 1] <= 0); printf("抽到牌:%d", num); putchar('\n'); position = GetElem(cardDeck2, num); p = NodeShow(cardDeck2, position); if (p && p->data == num) { NodeDelete(cardDeck2, position); } else { NodeInsert(cardDeck2, position, num); } total--; cards[num - 1]--; printf("系统手中牌(NULL表示无):"); if (ElemCount(cardDeck2)) { Traverse(cardDeck2, Show); } else { printf("NULL"); } } putchar('\n'); turn = !turn; } printf("本轮结束,牌全部用完,谢谢拉\n"); getchar(); return 0; }