Пример #1
0
static int
upload_tree(struct test_tree *base, struct gui_tree *tree, struct tree_node *node)
{
    int i = 0, n = 0;
    enum gui_tree_node_operation op;
    if (node->count) {
        i = 0;
        op = gui_panel_tree_begin_node(tree, node->name, &node->state);
        while (i < node->count)
            i += upload_tree(base, tree, node->children[i]);
        gui_panel_tree_end_node(tree);
    }
    else op = gui_panel_tree_leaf(tree, node->name, &node->state);

    switch (op) {
    case GUI_NODE_NOP: break;
    case GUI_NODE_CUT:
        tree_remove_node(node->parent, node);
        tree_push_node(base, node);
        return 0;
    case GUI_NODE_DELETE:
        tree_remove_node(node->parent, node); break;
        return 0;
    case GUI_NODE_PASTE:
        i = 0; n = base->count;
        while (i++ < n)
            tree_add_node(node, tree_pop_node(base));
    case GUI_NODE_CLONE:
    default:break;
    }
    return 1;
}
Пример #2
0
int tree_add_node(tree_t *root,node_t *parent,node_t *n){
  int cmp=root->item_cmp(&(parent->item),&(n->item));
  if (cmp>0){
    if(parent->left==NULL){
      parent->left=n;
      return 0;
    }else{
      return tree_add_node(root,parent->left,n);
    }
  }else if (cmp<0){
    if (parent->right==NULL){
      parent->right=n;
      return 0;
    }else{
      return tree_add_node(root,parent->right,n);
    }
  }else{                        /* 说明parent与n所含item 同值, 不允许同值item的存在,添加失败 */
    return -1;
  }
}
Пример #3
0
int main()
{
	int choice = 0;
	void *root = NULL;
	while (1) {

		printf ("1. Add node\n");
		printf ("2. Print tree in BFS\n");
		printf ("3. Print tree in DFS\n");
		printf ("4. Exit\n");
		printf ("Enter your choice: ");
		scanf("%d", &choice);

		switch (choice) {
			case 1:
				{
					int *data = NULL, parent;
					void *pnode = NULL, *n = NULL;

					printf ("Enter parent: ");
					scanf ("%d", &parent);
					pnode = tree_traverse(root, BFS, compare_data, &parent);
					if (!pnode)
					{
						assert(!root);
					}
					data = (int *)calloc (1, sizeof(*data));
					printf ("Enter data: ");
					scanf ("%d", data);
					n = tree_add_node(pnode, data);
					if (!root)
						root = n;
				}
				break;
			case 2:
				tree_traverse(root, BFS, print_tree, NULL);
				printf ("\n");
				break;
			case 3:
				tree_traverse(root, DFS, print_tree, NULL);
				printf ("\n");
				break;
			case 4:
				exit(0);
				break;
			default:
				printf ("Invalid choice!\n");
				break;
		}
	}
}
Пример #4
0
/* 向tree添加节点,成功返回0,失败返回-1 */
int tree_add(tree_t *root,Item item){
  node_t *new_node;
  int ret=-1;
  if (make_node(&item,&new_node)!=-1){
    if(root->root==NULL){
      root->root=new_node;
      root->size++;
      ret=0;
    }else{
      ret=tree_add_node(root,root->root,new_node);
      if(ret ==0){
        root->size++;
      }
    }
  }else{
    ret= -1;                  /* 如创建节点不成功 */
  }
  return ret;
}
Пример #5
0
void main()
{
    int a[30];
    int count = 0;
    struct tree_node *root = NULL;
    root = tree_add_node(1);
    root->left = tree_add_node(2);
    root->right = tree_add_node(3);
    root->left->left = tree_add_node(4);
    root->left->left->right = tree_add_node(10);
    root->left->right = tree_add_node(5);
    root->left->right->right = tree_add_node(50);
    root->right->left = tree_add_node(6);
    root->right->right = tree_add_node(7);

    struct tree_node *root2 = NULL;
    root2 = tree_add_node(1);
    root2->left = tree_add_node(2);
    root2->right = tree_add_node(3);
    root2->left->left = tree_add_node(4);
    root2->left->left->right = tree_add_node(10);
    root2->left->right = tree_add_node(5);
    root2->right->left = tree_add_node(6);
    root2->right->right = tree_add_node(7);
    //tree_preorder_print(root);
    tree_postorder_iterative_print(root);
    printf("Tree size(9) = %d\n", tree_size(root));
    printf("Height is %d\n", tree_height(root));
    printf("Check identical for same tree = %d\n", tree_check_identical(root, root2));
    tree_mirror(root);
    tree_postorder_iterative_print(root);
    tree_r2l_path(root, a, count);
}
Пример #6
0
const void *immutable (const void * data, unsigned long length)
{
    const char *rv;
    const char *data_char = (const char *)data;
    struct tree_node *n;
    int_pointer hash = hash_murmur2_pt (&data, sizeof (const void *), 0);

    if (tree_get_node (&immutable_data_tree, hash)
        != (struct tree_node *)0)
    {
        return data;
    }

    hash = hash_murmur2_pt (data, length, 0);

    if ((n = tree_get_node (&immutable_hashes, hash))
        != (struct tree_node *)0)
    {
        return (const void *)node_get_value (n);
    }

    if ((length + 1) > immutable_data_space_left) {
        unsigned long new_size = IMMUTABLE_CHUNKSIZE;
        lock_immutable_pages();

        if (length > IMMUTABLE_CHUNKSIZE) {
            new_size = ((length / IMMUTABLE_CHUNKSIZE) +
                         (((length % IMMUTABLE_CHUNKSIZE) != 0) ? 1 : 0))
                       * IMMUTABLE_CHUNKSIZE;
        }

        if (immutable_data != (void *)0)
        {
            lock_immutable_pages();
        }

        immutable_data = get_mem(new_size);

        immutable_data_space_left = new_size;
        immutable_cursor = immutable_data;
        immutable_data_size = new_size;
    }

    for (rv = immutable_cursor; length != 0;
         immutable_cursor++,
         data_char++,
         length--,
         immutable_data_space_left--)
    {
         *immutable_cursor = *data_char;
    }

    *immutable_cursor = 0; /* write an extra 0 after whatever we just wrote */
    immutable_cursor++;
    immutable_data_space_left--;

    while ((((unsigned long)immutable_cursor) % sizeof(void *)) != 0)
    {
        *immutable_cursor = 0;
        immutable_cursor++;
        immutable_data_space_left--;
    }

    tree_add_node_value (&immutable_hashes, hash, (void *)rv);

    hash = hash_murmur2_pt (&rv, sizeof (const void *), 0);

    tree_add_node (&immutable_data_tree, hash);

    return rv;
}
Пример #7
0
/*
 *	Add a child to the parent structure.
 */
int pdb_add_tree_node_child_cb(struct pdb_node_t* parent, char* id,
	struct pdb_node_t* child) {
		
	struct binaryTree* tptr = parent->data;
	return tree_add_node(tptr, id, (void*)child);
}