1 | /* Reconstruct GBT-tree from Ntree */ |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <string.h> |
---|
5 | |
---|
6 | #include <arbdb.h> |
---|
7 | #include <arbdbt.h> |
---|
8 | |
---|
9 | #include "CT_mem.hxx" |
---|
10 | #include "CT_part.hxx" |
---|
11 | #include "CT_ntree.hxx" |
---|
12 | #include "CT_rbtree.hxx" |
---|
13 | |
---|
14 | #define RMSTRLEN 81 |
---|
15 | |
---|
16 | char **name_tbl = NULL; |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | /* Initialise the modul */ |
---|
24 | void rb_init(char **names) |
---|
25 | { |
---|
26 | name_tbl = names; |
---|
27 | } |
---|
28 | |
---|
29 | |
---|
30 | /* get the name of a leaf from the index */ |
---|
31 | char *get_name(int idx) |
---|
32 | { |
---|
33 | char *t; |
---|
34 | t = strdup(name_tbl[idx]); |
---|
35 | return t; |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | /* build a remark with the procentile representation of the partition */ |
---|
40 | char *rb_remark(const char *info, int perc, char *txt) |
---|
41 | { |
---|
42 | char *txt2; |
---|
43 | |
---|
44 | txt2 = (char *) getmem(RMSTRLEN); |
---|
45 | sprintf(txt2, "%s%i%%", info, perc/100); |
---|
46 | if(txt) { |
---|
47 | strcat(txt, txt2); |
---|
48 | free(txt2); |
---|
49 | } |
---|
50 | else { |
---|
51 | txt = txt2; |
---|
52 | } |
---|
53 | |
---|
54 | return txt; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | /* doing all the work for rb_gettree() :-)*/ |
---|
59 | /* convert a Ntree into a GBT-Tree */ |
---|
60 | RB_INFO *rbtree(NT_NODE *tree, GBT_TREE *father) |
---|
61 | { |
---|
62 | NSONS *nsonp; |
---|
63 | int idx; |
---|
64 | GBT_TREE *gbtnode; |
---|
65 | RB_INFO *info, *info_res; |
---|
66 | |
---|
67 | |
---|
68 | gbtnode = (GBT_TREE *) getmem(sizeof(GBT_TREE)); |
---|
69 | info = (RB_INFO *) getmem(sizeof(RB_INFO)); |
---|
70 | |
---|
71 | gbtnode->father = father; |
---|
72 | |
---|
73 | info->node = gbtnode; /* return-infos */ |
---|
74 | info->percent = tree->part->percent; |
---|
75 | info->len = tree->part->len; |
---|
76 | nsonp = tree->son_list; |
---|
77 | if(!nsonp) { /* if node is leaf */ |
---|
78 | idx = calc_index(tree->part); |
---|
79 | gbtnode->name = strdup((name_tbl[idx])); /* test: get_name(idx)); */ |
---|
80 | gbtnode->is_leaf = GB_TRUE; |
---|
81 | return info; |
---|
82 | } |
---|
83 | |
---|
84 | gbtnode->is_leaf = GB_FALSE; |
---|
85 | if (info->percent < 10000) { |
---|
86 | gbtnode->remark_branch = rb_remark("", info->percent, gbtnode->remark_branch); |
---|
87 | } |
---|
88 | |
---|
89 | /* leftson */ |
---|
90 | info_res = rbtree(nsonp->node, gbtnode); |
---|
91 | gbtnode->leftson = info_res->node; |
---|
92 | gbtnode->leftlen = info_res->len; |
---|
93 | free((char *)info_res); |
---|
94 | |
---|
95 | nsonp = nsonp->next; |
---|
96 | if(!nsonp) return info; |
---|
97 | |
---|
98 | /* rightson */ |
---|
99 | info_res = rbtree(nsonp->node, gbtnode); |
---|
100 | gbtnode->rightson = info_res->node; |
---|
101 | gbtnode->rightlen = info_res->len; |
---|
102 | free((char *)info_res); |
---|
103 | |
---|
104 | return info; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /* reconstruct GBT Tree from Ntree. Ntree is not destructed afterwards! */ |
---|
109 | GBT_TREE *rb_gettree(NT_NODE *tree) |
---|
110 | { |
---|
111 | RB_INFO *info; |
---|
112 | GBT_TREE *gbttree; |
---|
113 | |
---|
114 | info = rbtree(tree, NULL); |
---|
115 | gbttree = info->node; |
---|
116 | free((char *)info); |
---|
117 | |
---|
118 | return gbttree; |
---|
119 | } |
---|