Node<T>* max_element(Node<T>* node)
{
	if(is_real(node)) 
	{
	 node = max_node(node, max_element(node->left));
     node = max_node(node, max_element(node->right));
	}

	return node;
}
// preassume that the root is not null
bst_node* precursor_node(bst_node* current)
{
    bst_node* parent = current->parent;
    // 如果存在左孩子,则前驱为当前结点左子树中最大的那个结点
    if(current->left)
    {
	return max_node(current->left);
    }
    else if(parent)
    {
	// 如果当前结点是其父亲的右孩子,则该父亲是其前驱
	while(parent && parent->right != current)
	{
	    current = parent;
	    parent = parent->parent;
	}
	if(parent)
	    return parent;
	else
	    return NULL;
    }
    else
    {
	return NULL;
    }

}
Example #3
0
static struct node* max_node(struct node* root)
{
	if (root == NULL)
		return NULL;
	if (root->right != NULL)
		return max_node(root->right);
	else
		return root;
}
int main()
{
    int i;
    bst_node* root = NULL;
    // 利用原地随机化方法将其打乱
    int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int length = sizeof(array)/sizeof(int);
    /* srand(GetTickCount()); */
    randomize_in_place(array, length);  
    print_array(array, length);  
    for (i = 0; i < length; i++)
    {
	bst_node* y = (bst_node*)malloc(sizeof(bst_node));
    	construct_node(y, array[i]);
    	root = insert_node(root, y);
	/* printf("%x\n", (unsigned int)root); */
    }
    mid_traverse_node(root);
    print_node(max_node(root));
    print_node(min_node(root));
    int s_value;
    scanf("%d", &s_value);
    fflush(stdin);
    while(s_value != -1)
    {
	bst_node* s_node = search_node(root, s_value);
	if (s_node)
	{
	    root = delete_node(s_node);
	}
	else
	{
	    printf("not in the bst tree\n");
	    fflush(stdout);
	}
	length--;
	mid_traverse_node(root);
	scanf("%d", &s_value);
	/* for(i = 0; i<length; i++) */
	/* { */
	/*     int search_key = random(0, length-1); */
	/*     bst_node* current_node = search_node(root, search_key); */
	/*     /\* bst_node* precursor = precursor_node(current_node); *\/ */
	/*     /\* bst_node* successor = successor_node(current_node); *\/ */
	/*     printf("the search key is %d\n", search_key); */
	/*     if(current_node->parent) */
	/* 	printf("the parent of %d is %d\n",current_node->key, current_node->parent->key); */
	/*     fflush(stdout); */
	/*     /\* if(precursor) *\/ */
	/*     /\*     printf("the precursor of %d is %d\n", current_node->key, precursor->key); *\/ */
	/*     /\* if(successor) *\/ */
	/*     /\*     printf("the successor of %d is %d\n", current_node->key, successor->key); *\/ */
	/* } */
    }
    return 0;
}
TREE predecessor(TREE tree_p) 
{
    if(tree_p->left != NULL)
        return max_node(tree_p->left); 

    while(tree_p->parent->left == tree_p)
        tree_p = tree_p->parent; 

    return tree_p->parent; 
}
bool is_bst(NodeBase* node)
{
	if (is_nil(node))
	{
		return true;
	}

	auto m = node;
	m = max_node(m, max_element(node->left));
	m = min_node(m, min_element(node->right));
	return m == node && is_bst(node->left) && is_bst(node_>right);
}
Example #7
0
int main(void)
{
	struct node* root = NULL;
	//struct node* root2 = NULL;
	struct node *find = NULL;

	char str[1024];

	root = insert(root, 10);
	root = insert(root, 15);
	root = insert(root, 9);
	root = insert(root, 8);
	root = insert(root, 13);
	root = insert(root, 7);
	root = insert(root, 5);
	root = insert(root, 18);
	root = insert(root, 22);
	root = insert(root, 3);
	root = insert(root, 4);
	root = insert(root, 2);
	root = insert(root, 1);
	print_ascii_tree(root);
	find = search(root, 18);
	print_ascii_tree(root);
	find = search(root, 22);
	printf("\n\n\nDATA found is %d\n", find->data);
	find = min_node(root);
	printf("Min in this tree is %d\n", find->data);
	find = max_node(root);
	printf("Mx in this tree is %d\n", find->data);
	print_ascii_tree(root);
	preorder(root);
	printf("\n");
	inorder(root);
	printf("\n");
	postorder(root);
	printf("\n");
	printf("DEPTH is %d\n", depth(root));
	tree_to_string(root, str);
	printf("The STR generated is %s\n", str);
	//string_to_tree(&root2, str);
	//print_ascii_tree(root2);
	printf("COUNT is %d\n",nodes(root));
	bool res = hassum(root, 45);
	printf("Bool val is %d\n", (int)res);
	levelorder(root);
	return 0;
}
Example #8
0
// min
int min_node(int depth, int side, XY *move)
{
    int best = INFINITY, value;
    int nmoves;
    XY moves[MOVENUM], opp[MOVENUM];
    int pre_board[8][8];
    int i;
    
    if (depth == DEPTH)
        return eval_func();
    
    // 手を生成
	nmoves = generate_moves(side, moves);
    
    // 手がないとき
	if (nmoves == 0)
	{
		if (generate_moves(-side, opp) == 0) // 相手もおけないときは終了
			return get_terminal_value();
		else    // 違うときはパス
			moves[nmoves++] = PASSMOVE;
	}
    
    // たどっていく
    for (i = 0; i < nmoves; i++)
	{
        memcpy(pre_board, board, sizeof(board));    // 盤面の保存
		place_disk(side, moves[i]);   // 一手進める
        
        // 再帰(recursive)
		value = max_node(depth + 1, -side, move);
        
        memcpy(board, pre_board, sizeof(board));    // 戻す
        
        // 値の更新
        if (value <= best)
        {
            best = value;
            if (depth == 0)
                *move = moves[i];
        }
	}
    
	return best;
}
Example #9
0
/* COMの手を生成する関数 */
void com_player(const int side, XY *move)
{
    XY moves[MOVENUM];
    int value;
	printf( "Com Thinking...\n");
    
    // 手がなければパス
    if (generate_moves(side, moves) == 0)
    {
        printf("Pass!\n\n");
        *move = PASSMOVE;
        return ;
    }
    
    value = max_node(0, side, move); // Min-Max法で手を生成
    printf("value = %d\n", value);
    
    if (value == INFINITY)  // 勝ち
        printf("COM Finds Win!\n");
}
Example #10
0
int main(void)
{
	int i, j;
    /*
	fi=fopen("nocows.in","r");
	fo=fopen("nocows.out","w");
	fscanf(fi,"%d %d",&N,&K);
	fclose(fi);
    */
    K = 13;
    
    for (N = min_node(K); N <= max_node(K); N = N+2) { 
        for (i=1;i<=K;i++)
            dp[1][i]=1;
        dynamicp();
        printf("N: %d, K: %d, %ld\n", N, K, ((dp[N][K]+9901-dp[N][K-1])%9901));

        for (i = 0; i <= N; i++)
            for (j = 0; j <= K; j++) 
                dp[i][j]=0;
    }
	//fclose(fo);
	return 0;
}