/** A node to be added to the tree must not belong to any tree (including this one). */ void Tree::add (Tree::Node* n) throw (Tree::DuplicateNode, Tree::NodeInUse, Tree::EmptyNode) { if (n == 0) throw EmptyNode(); if (node_set.find(n) != node_set.end()) throw DuplicateNode(n); if (n->in_use) throw NodeInUse(n); if (root_node == 0) root_node = n; n->in_use = true; node_set.insert(n); preorder_needed = postorder_needed = rpostorder_needed = true; }
/* Make a copy of the entire tree */ TREENODE *DuplicateTree(TREENODE *node) { TREENODE *kid; TREENODE *dup; assert(node != NULL); dup = DuplicateNode(node); kid = GetChild(node,1); while (kid != 0) { AddChild(dup,DuplicateTree(kid)); kid = NextSibling(kid); } return dup; }
/*! A node to be added to the tree must not belong to any tree (including this one). */ void Tree::addNode(OA_ptr<Tree::Node> n) throw (Tree::DuplicateNode, Tree::NodeInUse, Tree::EmptyNode) { if (n.ptrEqual(0)) { throw EmptyNode(); } if (node_set->find(n) != node_set->end()) { throw DuplicateNode(n); } if (n->in_use) { throw NodeInUse(n); } if (root_node.ptrEqual(0)) { root_node = n; } n->in_use = true; node_set->insert(n); preorder_needed = postorder_needed = rpostorder_needed = true; }
/** * {{{ 添加节点 */ int AddNodeAscend(Link to_add) { Link pn, prev, curr; struct Node dummy; int i; pn = (Link) malloc(sizeof(struct Node)); if (pn == NULL) { return 0; } memcpy(pn, to_add, sizeof(struct Node)); // 设置虚拟节点 dummy.Next = Head; prev = &dummy; curr = Head; for (;; prev = curr, curr = curr->Next) { if (curr == NULL) { // 到达了链表的结尾 break; } i = NodeCmp(pn, curr); if (i <= 0) { break; } } if (curr && i == 0) { if (DuplicateNode(curr, pn) == 0) { return 1; } } prev->Next = pn; pn->Next = curr; Head = dummy.Next; // 回收虚拟节点 return 1; }