Tree and Forest v2.0 hierarchy non-recursive create trees and forests, trees in the forest are not connected

Source: Internet
Author: User

#include <iostream> #include "queue.h"//Pre-written class #include "stack.h"//Pre-written class using namespace std;/* Implement root pointer to the root node of the first tree using a binary tree */template <class T>class forest;//========================================// Forest node class declaration template <class t>class forestnode{private:forestnode<t> *firstchild,*nextbrother;//      Pointer to big child node and pointer to Big Brother node T data;//data Public:friend class forest<t>; Constructor Forestnode (const t& item,forestnode *lptr=null,forestnode *rptr=null): Data (item), FirstChild (lptr), NE Xtbrother (rptr) {} forestnode () {} forestnode<t> * Getfirstchild () Const{return firstchild;} Returns the older son node void setfirstchild (Forestnode<t> * L) {firstchild=l;} Set the big son knot forestnode<t> * getnextbrother () Const{return Nextbrother;} Returns the large sibling node void Setnextbrother (Forestnode<t> * R) {nextbrother=r;}    Set large sibling node T GetData () {return data;} void SetData (T item) {data=item;}};/ /===============================================================//The sound of forest classMing template <class t>class forest{private:forestnode<t> *root;//Virtual root node declaration int number;//Tree number public:forest (forestnode<t>*t=null): root (T) {}//constructor virtual ~forest () {Del (root);} destructor deletes the entire forest//finds the node forestnode<t> *find (forestnode<t> *t,const t&item) of the data domain in the forest that is the root node of nodes T cons T forestnode<t>* Createnewtree (); A tree is added to the forest forestnode<t>* Createtree ();        Create a tree forestnode<t>* gettree (int n);//Obtain the nth tree in the forest//delete node T and its child forest void Del (forestnode<t> *t);    First, the root traverses and outputs the sub forest void preorder (forestnode<t> *t) const with node T as the root node;    The posterior root traverses and outputs a postorder (forestnode<t> *t) const with node T as the root node;    A non-recursive first root traverses and outputs a norepreorder (forestnode<t> *t) const with node T as the root node;    A non-recursive post-root traversal and output of a child forest void Norepostorder (forestnode<t> *t) const with node T as the root node;    The recursive hierarchy traverses and outputs a levelorder (forestnode<t> *t) const with node T as the root node of the child forest Void;            Create Forest forestnode<t>* createforest (); Other operations forestnode<t>* Getroot (){return root;}    void Setroot (forestnode<t> * T) {root=t;}        BOOL IsEmpty () {return root==null;} void output ();};    Template <class t>void forest<t>::output () {cout<< "The first root traversal sequence of the forest is:";    Preorder (Getroot ());            cout<<endl;    cout<< "The sequence of the posterior root traversal of the forest is:";    Postorder (Getroot ());             cout<<endl;    cout<< "The sequence of hierarchical traversal of forests is:";    Levelorder (Getroot ()); Cout<<endl;} =======================================//finds the node template <class the data field as item in a sub-forest that is the root node of nodes T T>forestnode<t    >* forest<t>::find (forestnode<t> *t,const t&item) const{forestnode<t> * p;    Recursive exit if (t==null) return NULL;    if (T->getdata () ==item) return t;    Recursive P=find (T->getfirstchild (), item);        if (p!=null) return p;    P=find (T->getnextbrother (), item);    if (p!=null) return p; return NULL;} ======================================//Create forest template <class t>forestnode<t>* Forest<t>:: Createforest (){forestnode<t>*p,*t;cout<< "Number of trees in forest:"; cin>>number;int i=1;if (number!=0) {cout<< "Create a section" < <i<< "Tree" <<endl; T=root=createtree (); i++; while (I<=number) {cout<< "create" <<i<< "Tree" <<endl;p=createtree (); T->setnextbrother (P); t=p;i++;}} return root;} Template <class t>forestnode<t>* forest<t>::createtree () {cout<< "Input node value";   forestnode<t>* currptr;   T data;   cin>>data;    Currptr=new forestnode<t> (data,null,null);   cout<< "Enter the number of sub-trees of the node"; int N;cin>>n;int i=1;if (n!=0) {cout<< "create" <<data<< "1th subtrees Tree" <<endl; forestnode<t>* Temp1 = Createtree (); Currptr->setfirstchild (TEMP1); I++;while (i<=n) {cout<< "Create" <<data<< "subtrees" <<i<< "The Tree" <<endl; forestnode<t>* Temp2=createtree (); Temp1->setnextbrother (TEMP2); temp1=temp2;temp2=null;i++;}} return currptr;} Template <class t>forestnode<t>* Forest<t>::createnewtree ()//increase in the forestAdd a tree {forestnode<t>* p=createtree (); forestnode<t>* T=root;while (T->getnextbrother ()!=null) T=t->getnextbrother (); T->SetNextBrother (P) ; return p;} Template <class t>forestnode<t>* forest<t>::gettree (int k)//Return to K-tree {if (k>number&&k< 1) return null;//out of bounds int i=1; Forestnode<t>*t=root;while (i!=k) {t=t->getnextbrother (); i++;} return t;} =============================================================//first-order traversal forest template <class t>void Forest<T   ::P Reorder (forestnode<t>* t) const {if (t = = NULL) {return;      } cout<<t->getdata () << "";   Preorder (T->getfirstchild ()); Preorder (T->getnextbrother ());} Template <class t>void forest<t>::norepreorder (forestnode<t>* T) const{lstack<forestnode<t&    Gt;*> Q;   if (t!=null) {Q.push (t);    } forestnode<t> *node;       while (!q.isempty ()) {Q.pop (node);      Cout<<node->getdata () << ""; IF (node->getfirstchild () = NULL) {Q.push (Node->getfirstchild ());      } if (Node->getnextbrother () = NULL) {Q.push (Node->getnextbrother ()); }}}//=========================================//post-traversal forest template <class t>void forest<t>::P Ostorder (   forestnode<t>* T) const {if (T = = NULL) {return;   } postorder (T->getfirstchild ()); Cout<<t->getdata () << ""; Postorder (T->getnextbrother ());} Template <class t>void forest<t>::norepostorder (forestnode<t>* T) const{lstack<forestnode<t    >*> Q;   if (t!=null) {Q.push (t);    } forestnode<t> *node;      while (!q.isempty ()) {Q.pop (node); if (node->getfirstchild () = NULL) {Q.push (Node->getfirstchild ());      } cout<<node->getdata () << "";      if (node->getnextbrother () = NULL) {Q.push (Node->getnextbrother ()); }}}//======================================//Hierarchy Traverse TempLate <class t>void Forest<t>::levelorder (forestnode<t>* T) const{LQueue<ForestNode<T>* > q; forestnode<t>*s=null;   All FirstChild are queued and brother immediately accesses if (t!=null) {Q.qinsert (t);    } forestnode<t> *node; while (s!=null| |!      Q.isempty ()) {while (s!=null) {node=s;s=null;cout<<node->getdata () << "";      if (node->getfirstchild () = NULL) {Q.qinsert (Node->getfirstchild ());   } if (Node->getnextbrother () = NULL) {s=node->getnextbrother ();}      } if (!q.isempty ()) {Q.qdelete (node); Cout<<node->getdata () << "";      if (node->getfirstchild () = NULL) {Q.qinsert (Node->getfirstchild ());  } if (Node->getnextbrother () = NULL) {s=node->getnextbrother (); }}}}//========================================//delete forest template <class t>void forest<t>::D El (ForestNode    <T> *t) {if (T! = NULL) {Del (T->getfirstchild ()); Del (T->getnextbrotHer ());  Delete t;}}

#include <iostream> #include "queue.h" #include "stack.h" using namespace std;template <class t>class tree;// ========================================//tree node class declaration template <class t>class treenode{private:treenode<t> *      firstchild,*nextbrother;//Pointer to big child node and pointer to Big Brother node T data;//data Public:friend class tree<t>; Constructor TreeNode (const t& item,treenode *lptr=null,treenode *rptr=null): Data (item), FirstChild (LPTR), Nextbrot Her (rptr) {} treenode<t> * Getfirstchild () Const{return firstchild;} Returns the older son node void setfirstchild (Treenode<t> * L) {firstchild=l;} Set the big son knot treenode<t> * getnextbrother () Const{return Nextbrother;} Returns the large sibling node void Setnextbrother (Treenode<t> * R) {nextbrother=r;}    Set large sibling node T GetData () {return data;} void SetData (T item) {data=item;}};/ /===============================================================//Tree Class declaration template <class T>class Tree{private : treenode<t> *root;//root node of the declaration T stop; Public   Tree (treenode<t>*t=null): Root (T), Stop (' * ') {}//constructor virtual ~tree () {Del (root);}    The destructor deletes the whole tree//finds the node treenode<t> *find (treenode<t> *t,const t&item) const of the data domain as the item in the subtree that nodes T is the root node; Search for the parent node of node p in a subtree with node t as the root node treenode<t> * Father (treenode<t> *t,treenode<t> *p) const;//in China with node T as root node     Delete node T and its subtree void Delsubtree (treenode<t>* t,treenode<t> *p);    Delete node T and its subtree void Del (treenode<t> *t);    First, the root traverses and outputs the subtree void preorder (treenode<t> *t) const with node T as the root node;    The posterior root traverses and outputs a postorder (treenode<t> *t) const with node T as the root node of the subtree;    A non-recursive first root traverses and outputs a subtree void Norepreorder (treenode<t> *t) const with node T as the root node;    A non-recursive post-root traversal and output of a subtree void Norepostorder (treenode<t> *t) const with node T as the root node;    A non-recursive hierarchy traverses and outputs a subtree void Norelevelorder (treenode<t> *t) const with node T as the root node;            Create Tree treenode<t>* createtree ();    Other operations treenode<t>* Getroot () {return root;}    void Setroot (treenode<t> * T) {root=t;} BOOL IsEmpty () {return Root==null;} void output ();};    The first root traversal sequence of the template <class t>void tree<t>::output () {cout<< "tree is:";    Preorder (Getroot ());            cout<<endl;    cout<< "The sequence of the posterior root traversal of the tree is:";    Postorder (Getroot ());             cout<<endl;    cout<< "The sequence of hierarchical traversal of a tree is:";    Norelevelorder (Getroot ()); Cout<<endl;} =======================================//finds the node template <class the data field is the item in the subtree that nodes T is the root node t>treenode<t>    * Tree<t>::find (treenode<t> *t,const t&item) const{treenode<t> * p;    Recursive exit if (t==null) return NULL;    if (T->getdata () ==item) return t;    Recursive P=find (T->getfirstchild (), item);        if (p!=null) return p;    P=find (T->getnextbrother (), item);    if (p!=null) return p; return NULL;} In the subtree that nodes T is the root node, search for the parent node of P, template <class t>treenode<t>* tree<t>::father (treenode<t>* T, treenode<t>*p) const{if (t==null| | P==null)//If t,p has a null return null;if (p==root) return null; If T is the root node, there is no parent.reenode<t>*result=null; Treenode<t>*q=t->getfirstchild ();//Start searching for the while (q!=null&&q!=p) {result=father (q,p) from the first subtrees tree; Result) Q=q->getnextbrother (); else return result;} if (q==p) return T;return NULL;} ======================================//non-recursive create tree template <class t>treenode<t>* tree<t>:: Createtree () {lqueue<treenode<t>*> q;//Processing root node//cout<< "Input root node and enter \ n"; cout<< "input root node \ n"; T Item;//item=getchar (); cin>>item;if (item==stop) return null;root=new treenode<t> (item); Q.qinsert (root); treenode<t>* node; treenode<t>* child; while (! Q.isempty ()) {Q.qdelete (node);//getchar ();//filter carriage return//cout<< "input" <<node->getdata () << "child nodes (end with *) , and enter \ n "; Item=getchar ();cout<< "input" <<node->getdata () << "Sub-node \ n"; Cin>>item;while (item!=stop) { if (Node->getfirstchild () ==null) {child=new treenode<t> (item); Node->setfirstchild (child); Q.qinsert (child);//item=getchar ();} Else{treenode<t>*nextcHild=new treenode<t> (item); Child->setnextbrother (Nextchild); Q.qinsert (Nextchild); Child=nextchild;//item=getchar ();} Cin>>item;}} Output (); return root;} =============================================================//first-order traversal tree template <class t>void Tree<T>:   :P Reorder (treenode<t>* t) const {if (t = = NULL) {return;   } cout<<t->getdata () << "";   Preorder (T->getfirstchild ()); Preorder (T->getnextbrother ());} Template <class t>void tree<t>::norepreorder (treenode<t>* T) const{lstack<treenode<t>*    > q;   if (t!=null) {Q.push (t);    } treenode<t> *node;      while (!q.isempty ()) {Q.pop (node);      Cout<<node->getdata () << "";      if (node->getfirstchild () = NULL) {Q.push (Node->getfirstchild ());      } if (Node->getnextbrother () = NULL) {Q.push (Node->getnextbrother ()); }}}//=========================================//sequential Traversal tree TemplaTe <class t>void tree<t>::P ostorder (treenode<t>* t) const {if (T = = NULL) {return;   } postorder (T->getfirstchild ());   Cout<<t->getdata () << ""; Postorder (T->getnextbrother ());} Template <class t>void tree<t>::norepostorder (treenode<t>* T) const{lstack<treenode<t>*& Gt    S     if (t!=null) s.push (t);    Treenode<t> *node;      while (!s.isempty ()) {S.pop (node); if (node->getfirstchild () = NULL) {s.push (Node->getfirstchild ());      } cout<<node->getdata () << "";      if (node->getnextbrother () = NULL) {s.push (Node->getnextbrother ()); }}}//======================================//hierarchy traversal, non-recursive template <class t>void tree<t>::norelevelorder ( treenode<t>* T) const{lqueue<treenode<t>*> q; treenode<t>*s=null;   All FirstChild are queued and brother immediately accesses if (t!=null) {Q.qinsert (t);    } treenode<t> *node; while (s!=null| |!      Q.isempty ()) {while (s!=null) {node=s;s=null;cout<<node->data<< "";      if (node->getfirstchild () = NULL) {Q.qinsert (Node->getfirstchild ());   } if (Node->getnextbrother () = NULL) {s=node->getnextbrother ();}      } if (!q.isempty ()) {Q.qdelete (node); Cout<<node->getdata () << "";      if (node->getfirstchild () = NULL) {Q.qinsert (Node->getfirstchild ());  } if (Node->getnextbrother () = NULL) {s=node->getnextbrother (); }}}}//========================================//Delete the node and its left and right subtree template <class t>void tree<t>::D El (    Treenode<t> *t) {if (T! = NULL) {Del (T->getfirstchild ());    Del (T->getnextbrother ()); Delete t;}} Template <class t>void tree<t>::D elsubtree (treenode<t>* t,treenode<t> *p) {if (T! = NULL& &p!=null) {treenode<t>*q=null,*result=null;result=findfather (t,p); if (result)//if the P parent node exists {if (result-> Getfirstchild () = =p)//if p is the large child of F node {result->setfirstchild (P->getnextbrother ());D El (P); return;} else//if not the older child node {q=result->getfirstchild (); while (Q->getnextbrother ()!=p) Q=q->getnextbrother ();q-> Setnextbrother (P->getnextbrother ());D El (P); return;}}  Else{del (P); root=null;}}}

Title, there's nothing to say.

Tree and Forest v2.0 hierarchy non-recursive create trees and forests, trees in the forest are not connected

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.