/* * return node's succeed * */ struct rb_node * _find_succ(struct rb_node *node) { if(node->rb_right){ return _find_min(node->rb_right); } struct rb_node *parent = node->rb_parent; while(parent && (node == node->rb_parent->rb_right)){ node = parent; parent = node->rb_parent; } return parent;//if NULL means no succ }
/* * find all node whose key is in range [MIN, to] * return number of nodes. * * */ int rb_del_in_range(_key_t to)//[MIN, to] { struct rb_node *min_ptr = _find_min(this_rb->root), *ptr; ap_key tmp; int num = 0; while(min_ptr && min_ptr->key <= to){ ptr = _find_succ(min_ptr); tmp.node_ptr = min_ptr; rb_delete(&tmp, DEL_RUNALL_CB); min_ptr = ptr; num++; } return num; }
static int _walk_int(struct ptree_node *pn, ptree_order_t order, int level, ptree_walk_res_t (*func)(struct ptree_node *, int level, void *, void *), void *arg1, void *arg2) { ptree_walk_res_t res; ptree_node_t *next; if (!pn) return PTREE_WALK_CONTINUE; if (order == PTREE_INORDER) { int nlevel; pn = _find_min(pn, &level); while (pn) { nlevel = level; next = _find_succ(pn, &nlevel); if ((res = func(pn, level, arg1, arg2)) != PTREE_WALK_CONTINUE) return res; level = nlevel; if (level < 0) level = 0; pn = next; } return PTREE_WALK_CONTINUE; } if (order == PTREE_PREORDER && (res = func(pn, level, arg1, arg2)) != PTREE_WALK_CONTINUE) return res; if ((res = _walk_int(pn->pn_left, order, level + 1, func, arg1, arg2)) != PTREE_WALK_CONTINUE) return res; if ((res = _walk_int(pn->pn_right, order, level + 1, func, arg1, arg2)) != PTREE_WALK_CONTINUE) return res; if (order == PTREE_POSTORDER && (res = func(pn, level, arg1, arg2)) != PTREE_WALK_CONTINUE) return res; return PTREE_WALK_CONTINUE; }
void dijkstra_show_all_path(graph_t * g, char start) { if (!g || g->vex_count() <= 1) { return; } int C = g->vex_count(); int * path = new int[C]; int limit = graph::numeric_weight<int>::unconnected_value(); list_t * L = g->get_structure(); std::vector<_dj> vect; vect.reserve(C); int s = g->id(start); path[s] = -1; for (int i = 0; i < C; ++i) { if (i != s) { _dj dj; dj.v = i; dj.w = get_weight(L,s,i); vect.push_back(dj); if (dj.w != limit) { path[i] = s; } else { path[i] = -1; } } } while (vect.size() > 0) { int m = _find_min(vect); if (vect[m].w == limit) { break; } if (m != vect.size() - 1) { _dj tmp = vect[m]; vect[m] = vect[vect.size()-1]; vect[vect.size()-1] = tmp; } _dj dj = vect[vect.size()-1]; vect.pop_back(); int v = dj.v; for (size_t i = 0; i < vect.size(); ++i) { int u = vect[i].v; int vu = get_weight(L, v, u); if (vu < limit && vu + dj.w < vect[i].w) { vect[i].w = vu + dj.w; path[u] = v; } } } std::stack<int> stk; for (int i = 0; i < C; ++i) { if (i != s) { int k = i; while (k >= 0) { stk.push(k); k = path[k]; } while (!stk.empty()) { std::cout << g->vex(stk.top()) << ' '; stk.pop(); } std::cout << '\n'; } } delete[] path; }