コード例 #1
0
Node<T>* min_element(Node<T>* node)
{
	if(is_real(node)) 
	{
	 node = min_node(node, min_element(node->left));
     node = min_node(node, min_element(node->right));
	}

	return node;
}
コード例 #2
0
// preassume that the root is not null
bst_node* successor_node(bst_node* current)
{
    bst_node* parent = current->parent;
    // 如果存在右孩子,则后继为当前结点右子树中最小的那个结点
    if(current->right)
    {
	return min_node(current->right);
    }
    else if(parent)
    {
	while(parent && parent->left != current)
	{
	    current = parent;
	    parent = parent->parent;
	}
	if (parent)
	    return parent;
	else 
	    return NULL;
    }
    else
    {
	return NULL;
    }
}
コード例 #3
0
ファイル: bin_tree.c プロジェクト: abhimanyu12/binary_trees
static struct node* min_node(struct node* root)
{
	if (root == NULL)
		return root;
	if (root->left != NULL)
		return min_node(root->left);
	else
		return root;
}
コード例 #4
0
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;
}
コード例 #5
0
TREE successor(TREE tree_p) 
{
    if(tree_p->right != NULL) 
        return min_node(tree_p->right); 

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

    return tree_p->parent;

}
コード例 #6
0
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);
}
コード例 #7
0
ファイル: bin_tree.c プロジェクト: abhimanyu12/binary_trees
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;
}
コード例 #8
0
ファイル: datas.c プロジェクト: ChowZenki/dnspod-sr
//---------------rbtree debug------------------------------------//
int
rbtree_test(void)
{
    int i, j, len, slice, ret;
    struct timeval tv;
    struct rbnode node, *pn = NULL;
    struct ttlnode *tn = NULL;
    struct rbtree *rbt = NULL;
    rbt = create_rbtree(rbt_comp_ttl_gt, NULL);
    if (rbt == NULL)
        dns_error(0, "create rbtree");
    node = rbt->nil;            //nil
    slice = 8000000;
    //for(i = 0;i < n;i ++)
    //{
    for (j = 0; j < slice; j++) {
        len = random() % 30;
        tn = malloc(sizeof(struct ttlnode) + len);
        if (tn == NULL)
            printf("oom\n");
        tn->exp = j;
        for (i = 0; i < len; i++)
            tn->data[i] = 'a' + i;
        node.key = tn;
        ret = insert_node(rbt, &node);
        if (ret != 0)
            printf("insert error\n");
    }
    printf("insert all\n");
    sleep(2);
    for (j = 0; j < slice; j++) {
        pn = min_node(rbt);
        if (pn != NULL) {
            tn = delete_node(rbt, pn);
            free(tn);
        } else
            printf("error\n");
    }
    printf("delete all\n");
    sleep(5);
    //}
    if (free_rbtree(rbt) != 0)
        dns_error(0, "free");
    //get_time_usage(&tv,0);
    return 0;
}
コード例 #9
0
ファイル: reversi3.c プロジェクト: tiwanari/soft2
// max
int max_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 = min_node(depth + 1, -side, move);   
		
        memcpy(board, pre_board, sizeof(board));    // 戻す
		
        // 値の更新
        if (value >= best)
        {
            best = value;
            if (depth == 0)
                *move = moves[i];
        }
	}
    
	return best;
}
コード例 #10
0
ファイル: nocows.off.c プロジェクト: yuguess/ACM
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;
}
コード例 #11
0
ファイル: author.c プロジェクト: cofyc/dnspod-sr
int
check_ttl_expire(struct author *author)
{
    time_t now;
    struct ttlnode *tn = NULL;
    struct rbnode *pn = NULL;
    mbuf_type *mbuf;
    int ret = -1;
    struct rbtree *rbt = NULL;
    
    mbuf = mbuf_alloc();
    if (NULL == mbuf)
        return -1;
    now = global_now;
    /* ds = author->s->datasets; */
    rbt = author->s->ttlexp;
    pthread_spin_lock(&rbt->lock);
    pn = min_node(rbt);
    while (pn != NULL) {
        tn = pn->key;
        //if exp was 12, now was 11, start
        //if exp was 12, now was 5, break
        if (tn->exp > (now + TTL_UPDATE))       //3 secs after it will not expire
            break;
        /* printf("ttl refresh "); */
        /* dbg_print_td(tn->data); */
        tn = delete_node(rbt, pn);
        pthread_spin_unlock(&rbt->lock);
        if (tn != NULL) {
            memset(mbuf, 0, sizeof(mbuf_type));
            mbuf->qname = tn->type;     //type
            mbuf->qtype = tn->type;
            mbuf->dlen = tn->dlen;
            memcpy(&(mbuf->lowerdomain), tn->lowerdomain, sizeof(packet_type));
            int i;
            for (i = 0; i < tn->lowerdomain->label_count; i++)
            {
                mbuf->lowerdomain.label[i] = mbuf->lowerdomain.domain + mbuf->lowerdomain.label_offsets[i];
            }
            mbuf->qhash = &(mbuf->lowerdomain.hash[0]);
            mbuf->td = mbuf->lowerdomain.domain;
            mbuf->qing = mbuf->td;
            mbuf->qlen = mbuf->dlen;
            mbuf->cid = 0;
            mbuf->fd = -1;
            init_qoutinfo(mbuf);
            ret = htable_insert_list(author->s->qlist, tn->data, tn->dlen, tn->type, (uchar *)mbuf, 0, NULL, tn->hash); //not replace
            if (HTABLE_INSERT_RET_NORMAL == ret)
            {
                mbuf = mbuf_alloc();
                if (NULL == mbuf)
                {
                    free(tn->lowerdomain);
                    free(tn);
                    return -1;
                }
            }
            //else querying lost
            free(tn->lowerdomain);
            free(tn);
        }
        pthread_spin_lock(&rbt->lock);
        pn = min_node(rbt);
    }
    mbuf_free(mbuf);
    pthread_spin_unlock(&rbt->lock);
    return 0;
}
コード例 #12
0
ファイル: mapit.c プロジェクト: sergev/2.11BSD
/* transform the graph to a shortest-path tree by marking tree edges */
void
mapit()
{	register p_node n;
	register p_link l;
	p_link lparent;
	static int firsttime = 0;

	vprintf(stderr, "*** mapping\n");
	Tflag = Tflag && Vflag;		/* tracing here only if verbose */
	/* re-use the hash table space for the heap */
	Heap = (p_link *) Table;
	Hashpart = pack(0L, Tabsize - 1);

	/* expunge penalties from -a option and make circular copy lists */
	resetnodes();

	if (firsttime++) {
		if (Linkout && *Linkout)	/* dump cheapest links */
			showlinks();
		if (Graphout && *Graphout)	/* dump the edge list */
			dumpgraph();
	}

	/* insert Home to get things started */
	l = newlink();		/* link to get things started */
	getlink(l)->l_to = Home;
	(void) dehash(Home);
	insert(l);

	/* main mapping loop */
remap:
	Heaphighwater = Nheap;
	while ((lparent = min_node()) != 0) {
		chkheap(1);
		getlink(lparent)->l_flag |= LTREE;
		n = getlink(lparent)->l_to;
		if (Tflag && maptrace(n, n))
			fprintf(stderr, "%s -> %s mapped\n",
				getnode(getnode(n)->n_parent)->n_name,
				getnode(n)->n_name);
		if (getnode(n)->n_flag & MAPPED)
			die("mapped node in heap");
		getnode(n)->n_flag |= MAPPED;

		/* add children to heap */
		heapchildren(n);
	}
	vprintf(stderr, "heap high water mark was %ld\n", Heaphighwater);

	/* sanity check on implementation */
	if (Nheap != 0)
		die("null entry in heap");

	if (Hashpart < Tabsize) {
		/*
		 * add back links from unreachable hosts to
		 * reachable neighbors, then remap.
		 *
		 * asymptotically, this is quadratic; in
		 * practice, this is done once or twice.
		 */
		backlinks();
		if (Nheap)
			goto remap;
	}
	if (Hashpart < Tabsize) {
		fputs("You can't get there from here:\n", stderr);
		for ( ; Hashpart < Tabsize; Hashpart++) {
			fprintf(stderr, "\t%s", getnode(Table[Hashpart])->n_name);
			if (getnode(Table[Hashpart])->n_flag & ISPRIVATE)
				fputs(" (private)", stderr);
			putc('\n', stderr);
		}
	}
}