ata Structure hapter Non-inary Trees r. Patrick han School of omputer Science and ngineering South hina University of Technology Outline Non-inary (eneral) Tree (h.) Parent Pointer mplementation (h.) ist of hildren mplementation (h..) eft-hild/ight-sibling mplementation (h..) ynamic eft-hild/ight-sibling mplementation (h..) ynamic Node mplementation (h..) K-ary Trees (h.) Sequential Tree mplementation (h.) ec : Non-inary Tree
eneral Tree (Non-binary Tree) tree T is a finite set of one or more nodes such that there is one designated node called the root of T The remaining nodes in (T {}) are partitioned into n disjoint subsets T, T,..., T k, each of which is a tree, and whose roots,,..., k, respectively, are children of ec : Non-inary Tree eneral Tree P P V V N M Q inary Tree eneral Tree - Two, one or zero child - ny number of child ec : Non-inary Tree
eneral Tree To maintain the structure of inary Tree, each node has eft child pointer ight child pointer ow about eneral Tree? P V N M Q ec : Non-inary Tree eneral Tree Node: T // eneral tree node T template <class lem> class TNode { public: TNode(const lem&); // onstructor ~TNode(); // estructor lem value(); // eturn value bool iseaf(); // TU if is a leaf TNode* parent(); // eturn parent TNode* leftmost_child(); // irst child TNode* right_sibling(); // ight sibling void setvalue(lem&); // Set value void insert_first(tnode<lem>* n); void insert_next(tnode<lem>* n); void remove_first(); // emove first child void remove_next(); // emove sibling }; P P P V V V ec : Non-inary Tree
eneral Tree: T // eneral Tree T template <class lem> class entree { private: void printhelp(tnode*); // Print helper function public: entree(); ~entree(); void clear(); TNode* root(); // onstructor // estructor // Send nodes to free store // eturn the root // ombine two subtrees void newroot(m, Tnode *, Tnode *); Void print(); // Print a tree }; ec : Non-inary Tree eneral Tree: Traversal template <class lem> void entree<lem>:: printhelp(tnode<lem>* subroot) { if (subroot->iseaf()) cout << "eaf: "; else cout << "nternal: "; cout << subroot->value() << "\n"; for (TNode<lem>* temp = subroot->leftmost_child(); temp!= NU; temp = temp->right_sibling()) printhelp(temp); } Print + temp = P ec : Non-inary Tree P Print + Print + temp = V V Print + Print + Print + P V nternal: nternal: P eaf: eaf: eaf: V eaf:
eneral Tree: mplementation Parent Pointer mplementation ist of hildren mplementation eft-hild/ight-sibling mplementation ynamic eft-hild/ight-sibling mplementation ynamic Node mplementation ec : Non-inary Tree Parent Pointer mplementation Only storing pointer may be the simplest general tree implementation Parent abel W Y Z W ood for answering the question re these two nodes in the same tree? ow to do that? Y Z ec : Non-inary Tree
Parent Pointer mplementation class entree { // entree for UNON/N private: int* array; // Node array int size; // Size of node array int N(int) const; // ind root public: entree(int); // onstructor ~entree() { delete [] array; } // estructor void UNON (int, int); // Merge equivalences void differ (int, int); // TU if not in same tree } ec : Non-inary Tree Parent Pointer mplementation int entree::n(int curr) const { while (array[curr]!=oot) curr = array[curr]; return curr; // t root } // eturn TU if nodes in different trees bool entree::differ(int a, int b) { int root = N(a); // ind root for a int root = N(b); // ind root for b return root!= root; // ompare roots } void entree::unon(int a, int b) { int root = N(a); // ind root for a int root = N(b); // ind root for b if (root!= root) array[root] = root; } array Parent ec : Non-inary Tree abel W Y W Y Z Z
Parent Pointer mplementation quivalence lass ssigning the members of a set to disjoint subsets called equivalence classes.g. Object and are equivalent Object and are equivalent Object and must be equivalence UNON/N implementation heck if two objects are equivalent: differ Set two objects are equivalent : UNON ec : Non-inary Tree Parent Pointer mplementation quivalence lass: xample (, ) (, ) (, ) (, ) (, ) (, ) (, ) ec : Non-inary Tree
Parent Pointer mplementation quivalence lass: xample (, ) s (, )? s (, )? s (, )? Yes Yes No ec : Non-inary Tree Parent Pointer mplementation quivalence lass: educe the cost The search cost can decrease by reducing the height of the tree Weighted Union ule ec : Non-inary Tree oin the tree with fewer nodes to the tree with more nodes ew > More More > ew (, )
Parent Pointer mplementation quivalence lass: educe the cost Path ompression int entree::n(int curr) const { if (array[curr] == OOT) return curr; ase ase return array[curr] = N(array[curr]); ecursive all } ec : Non-inary Tree atree.n(); Small xercise!!!! (, ) f (, )? (, ) f (, )? (, ) f (K, )? (, ) (, ) f (, )? (path compression) (, ) (, ) (, K) K ec : Non-inary Tree
Small xercise!!!! (, ) (, ) K (, ) (, ) (, ) (, ) (, ) K f (, )? f (, )? f (K, )? f (, )? (path compression) Yes No Yes Yes (, K) K ec : Non-inary Tree eneral Tree mplementation ist of hildren ndex Value Parent hildren can be found easily specially for the leftmost child ight sibling is more difficult ombining trees is difficult if the trees are stored in different array ec : Non-inary Tree
eneral Tree mplementation eft-hild/ight-sibling ndex eft-hild Value ec : Non-inary Tree Parent ight-sibling ndex mproved version of ist of hildren ight sibling pointer is added More space efficient as each node requires a fixed amount of space ombining trees is difficult if the trees are stored in different array eneral Tree mplementation ynamic eft-hild/ight-sibling NU NU NU NU ec : Non-inary Tree NU NU NU NU NU NU NU inked version of eft- hild/ight- Sibling onvert as a binary tree annot find the parent of a node
eneral Tree mplementation ynamic Node llocate variable space for each node Two implementation methods: rray-based ist inked ist ec : Non-inary Tree eneral Tree mplementation ynamic Node llocate an array of child pointers as part of the node ssume the number of children is known when the node is created ec : Non-inary Tree
eneral Tree mplementation ynamic Node Store a linked list of child pointers with each node More flexible (no assumption on number of child) but require more space ec : Non-inary Tree K-ary Trees K-ary Trees are trees with nodes have at most K children e.g. inary Tree, K = eneral Tree, K = inf K M ec : Non-inary Tree -ary Tree
K-ary Trees asy to implement relatively Many properties of binary trees can be extended When K becomes large, the potential number of NU pointers increase nternal and leaf nodes should be implemented differently K M ec : Non-inary Tree -ary Tree K-ary Trees K M ull -ary Tree (not complete) omplete -ary Tree (not ull) ull and complete -ary Tree ec : Non-inary Tree
Sequential Tree mplementations undamentally different approach to implementing trees Store a series of node values with the minimum information needed to reconstruct the tree structure Preorder traversal is used ec : Non-inary Tree Sequential Tree mplementations or inary Trees (preorder traversal), o not have enough information to reconstruct the tree / / / / / / / / / / NU pointer should also be added / / / dd to the internal node emove the / (NU pointer) of the leaf node ec : Non-inary Tree
Sequential Tree mplementations or eneral Tree, ) indicates when a node s child list has come to an end ) ) ) ) ) ) ) ec : Non-inary Tree Sequential Tree mplementations Space/Time Tradeoff Space saving No pointer is needed ost the benefit of tree Tree: fficient access O(log n) Sequential Tree: Sequential access O(n) ec : Non-inary Tree