Example #1
0
     *     1       2
     *    /\      / \
     *   /  \    /   \
     *  4    5  7     3
     *      / \      / \
     *     6   8    9   13
     *    / \      /   /
     *   10  11   12  14
     */

    bintree* bt = bt_create(int, refi(0));

    btnode* n[15];

    n[0 ] = bt->root;
    n[1 ] = bt_lchild(n[0 ], bt_new_node(bt, refi(1 )));
    n[2 ] = bt_rchild(n[0 ], bt_new_node(bt, refi(2 )));
    n[3 ] = bt_rchild(n[2 ], bt_new_node(bt, refi(3 )));
    n[4 ] = bt_lchild(n[1 ], bt_new_node(bt, refi(4 )));
    n[5 ] = bt_rchild(n[1 ], bt_new_node(bt, refi(5 )));
    n[6 ] = bt_lchild(n[5 ], bt_new_node(bt, refi(6 )));
    n[7 ] = bt_lchild(n[2 ], bt_new_node(bt, refi(7 )));
    n[8 ] = bt_rchild(n[5 ], bt_new_node(bt, refi(8 )));
    n[9 ] = bt_lchild(n[3 ], bt_new_node(bt, refi(9 )));
    n[10] = bt_lchild(n[6 ], bt_new_node(bt, refi(10)));
    n[11] = bt_rchild(n[6 ], bt_new_node(bt, refi(11)));
    n[12] = bt_lchild(n[9 ], bt_new_node(bt, refi(12)));
    n[13] = bt_rchild(n[3 ], bt_new_node(bt, refi(13)));
    n[14] = bt_lchild(n[13], bt_new_node(bt, refi(14)));

    bt_traverse(n[0], prl, print_node, NULL); putchar('\n');
int
read_seno_dtree_file(dtree_t **out_dt,
		     const char *file_name)
{
    uint32 n_base;
    uint32 n_cd;
    char tree_id[64];
    float32 ent;
    uint32 key, l_key, r_key, n_key = 0, *k2q = NULL;
    char q_str[1024], *rem_q_str;
    FILE *fp;
    int i, j, n_quest;
    dtree_t *dt;
    bt_node_t *node;
    bt_t *tree;
    comp_quest_t *q;
    float32 *q2ent;

    *out_dt = dt = (dtree_t *)ckd_calloc(1, sizeof(dtree_t));

    dt->tree = tree = bt_new();

    fp = fopen(file_name, "r");
    if (fp == NULL) {
	E_WARN_SYSTEM("Unable to open %s for reading", file_name);

	return S3_ERROR;
    }

    for (n_quest = 0; fgets(q_str, 1024, fp) != NULL; n_quest++);

    --n_quest;	/* account for header line */

    dt->n_quest = n_quest;

    rewind(fp);

    read_header_line(&n_base, tree_id, &n_cd, fp);
    for (i = 0; read_node_line(&ent, &key, &l_key, &r_key, q_str, fp) > 0; i++) {
	if (n_key < l_key)
	    n_key = l_key;
	if (n_key < r_key)
	    n_key = r_key;
    }
    ++n_key;
    dt->n_key = n_key;

    rewind(fp);

    read_header_line(&n_base, tree_id, &n_cd, fp);

    E_INFO("Reading tree %s (%u base phones, %u CD phones, %u quest)\n",
	   tree_id, n_base, n_cd, n_quest);

    dt->quest = q = (comp_quest_t *)ckd_calloc(n_quest,
						    sizeof(comp_quest_t));
    dt->k2q   = k2q   = (uint32 *)ckd_calloc(n_key, sizeof(uint32));
    dt->q2ent = q2ent = (float32 *)ckd_calloc(n_quest, sizeof(float32));

    for (i = 0; i < n_key; i++) {
	k2q[i] = NO_MAP;
    }

    for (i = 0; read_node_line(&ent, &key, &l_key, &r_key, q_str, fp) > 0; i++) {
	if (tree->root) {
	    E_INFO("%u\n", key);

	    node = bt_find_node(tree, key);
	    if (node) {
		/* grow left and right children */
		bt_add_left(node, l_key);
		bt_add_right(node, r_key);
	    }
	    else {
		E_FATAL("Find node w/ key %u failed\n", key);
	    }
	}
	else {
	    E_INFO("root %u\n", key);
	    tree->root = bt_new_node(key);
	    bt_add_left(tree->root, l_key);
	    bt_add_right(tree->root, r_key);
	}

	k2q[key] = i;
	q2ent[i] = ent;
	parse_compound_q(&q[i], q_str);
    }

    assert(i == n_quest);

    return S3_SUCCESS;
}