int maxNode(char * board) //player1 { char evaluation = INF; if (is_winner(board, PLAYER_1)) evaluation=1; else if (is_full(board)) evaluation=0; else { //µ±Ç°½áµã¹ÀÖµ char temp; for (int i = 0; i < BOARD; i++) { if (board[i] == 0) { board[i] = PLAYER_2; temp = minNode(board); if (temp < evaluation) evaluation = temp; //×îС¹ÀÖµ board[i] = 0; //»Ö¸´µ±Ç°×´Ì¬ } } } #if DEBUG printBoard(board); printf("%d\n", evaluation); #endif return evaluation; }
// checks BST invariant static bool checkBSTNode(const AVLNode *node, Comparator comp){ if(node == nullptr) return true; // empty tree satisfies invariant if(!checkBSTNode(node->left(), comp)) return false; // failure left if(!checkBSTNode(node->right(), comp)) return false;// failure right if(node->left() != nullptr){ // left child exists const void* key = maxNode(node->left())->key(); // maximal key in left child if(comp(key,node->key()) >= 0){ // left maximal key is not smaller return false; // binary search tree property is violated } } if(node->right() != nullptr){ // right child exists const void* key = minNode(node->right())->key(); // minimal key in right child if(comp(node->key(),key) >= 0){ // right minimal key is not greater return false; // binary search tree property is violated } } return true; // all tests were successful }
static Node_T* nextNode(BST_T *o, Node_T *n) { assert(n != o->guard); if (n->right != o->guard) return minNode(o, n->right); while (n->parent != o->guard && n->parent->right == n) n = n->parent; return n->parent; }
int BST_remove(BST_T *o, void *k) { assert(o && o->type == TYPE); o->guard->key = k; Node_T **p = &o->root; for (;;) { int cmp = o->cmp(k, (*p)->key); if (cmp == 0) break; if (cmp < 0) p = &(*p)->left; else p = &(*p)->right; } if (*p == o->guard) return 0; Node_T *n = *p; if (n->left != o->guard && n->right != o->guard) { Node_T *min = minNode(o, n->right); n->key = min->key; n->value = min->value; if (min->parent->left == min) min->parent->left = min->right; else min->parent->right = min->right; if (min->right) min->right->parent = min->parent; n = min; } else { Node_T *child = n->left == o->guard ? n->right : n->left; *p = child; if (child) child->parent = n->parent; } free(n); --o->size; return 1; }
const void* AVL::min(const void* *key_p) const{ assert(key_p != nullptr); AVLNode *temp = minNode(d_top_p); // node with minimal key if(temp == nullptr) return nullptr; // tree is empty *key_p = temp->key(); // returning minimal key return temp->val(); // returning corresponding value pointer }
bool erase(const T& val) { Node** pnode = searchNode(val); Node *node = *pnode; if (node == NULL) return false; if (node->left != NULL && node->right != NULL) { pnode = minNode(&node->right); node->val = (*pnode)->val; *pnode = (*pnode)->right; delete *pnode; } else { *pnode = node->left != NULL ? node->left : node->right; delete node; } --m_size; return true; }
static AVLNode *succNode(AVLNode *node, const void* key, Comparator comp){ if(node == nullptr) return nullptr; // key has no successor in tree if(comp(key,node->key()) < 0){ // key is to the left AVLNode *temp = succNode(node->left(),key,comp); // looking left if(temp == nullptr) { // there is no successor of key in left child return node; // current node has successor key } else { return temp; // successor of key in left child is successor key } } if(comp(node->key(),key) < 0){ // key is to the right, if successor exists ... return succNode(node->right(),key,comp); // ... it must be on the right } // both < and > have failed, key is equal to key of current node if (node->right() == nullptr) { // right child does not exist AVLNode *parent = node->parent(); // successor possibly parent if(parent == nullptr) return nullptr; // no parent then no succ if(comp(key,parent->key()) < 0) { // parent key greater, it is successor return parent; } else { // parent key cannot be equal, hence it is smaller and no successor return nullptr; } } // right child does not exist else { // right child does exist return minNode(node->right()); // successor is min of right child } }
static AVLNode *minNode(AVLNode *node){ if(node == nullptr) return nullptr; if(node->left() == nullptr) return node; // no left child, node is min return minNode(node->left()); // minimal key in left child }
int TicTacToeAI(char * stateBoard, int player) //play1:max player2:min { //Minmax char probablyWin[BOARD]; char probablyWinEvaluation[BOARD]; int win[BOARD]; int index; int probablyWin_cnt = 0; int win_cnt = 0; char evaluation; if (player == PLAYER_1) { evaluation = -INF; //µ±Ç°½áµã¹ÀÖµ char temp; for (int i = 0; i < BOARD; i++) { if (stateBoard[i] == 0) { stateBoard[i] = PLAYER_1; temp = maxNode(stateBoard); if (temp >= evaluation) { index = i; //ΨһµÄÂä×Ó·½·¨ probablyWin[probablyWin_cnt] = i; probablyWinEvaluation[probablyWin_cnt++] = temp; evaluation = temp; //×îС¹ÀÖµ } stateBoard[i] = 0; //»Ö¸´µ±Ç°×´Ì¬ } } #if DEBUG printBoard(stateBoard); printf("%d\n", evaluation); #endif } else if (player == PLAYER_2) { evaluation = INF; //µ±Ç°½áµã¹ÀÖµ char temp; for (int i = 0; i < BOARD; i++) { if (stateBoard[i] == 0) { stateBoard[i] = PLAYER_2; temp = minNode(stateBoard); if (temp <= evaluation) { index = i; //ΨһµÄÂä×Ó·½·¨ probablyWin[probablyWin_cnt] = i; probablyWinEvaluation[probablyWin_cnt++] = temp; evaluation = temp; //×îС¹ÀÖµ } stateBoard[i] = 0; //»Ö¸´µ±Ç°×´Ì¬ } } #if DEBUG printBoard(stateBoard); printf("%d\n", evaluation); #endif } for (int i = 0; i < probablyWin_cnt;i++) //find the places which hava a max evaluation if (probablyWinEvaluation[i] == evaluation) win[win_cnt++] = probablyWin[i]; srand((unsigned)time(NULL)); int random=rand()%win_cnt; return win[random]; //randomly choose one place where have a probability to win }
BSTIter_T* BST_begin(BST_T *o) { assert(o && o->type == TYPE); return (BSTIter_T*)(o->root == o->guard ? o->root : minNode(o, o->root)); }