Senrin and two-fork tree conversion

Source: Internet
Author: User

1. Why do trees and forests convert to binary trees?

Because in the actual processing problem, most of the situation is one-to-many, to the tree, forest such data structure!

We are already familiar with the binary tree, so we turn to the familiar structure and handle it well.

2. The method of the child's brother tree

Grasp the principle of left child right brother :

(1), the transformation of the tree and the two-:i> tree with the root node of the tree as the root of the two fork tree;

Ii> the left child pointer points to the first child node of the root;

Iii> right child pointer pointing to "sibling knot."

(2), binary tree indicates that the root node of the forest:i> two fork tree is the root node of the first tree in the forest.

The right child of the ii> root node is the root node of other trees in the forest.

3. Graphic representation method

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/85/87/wKiom1enrW3zOQebAAAtNus3Yt4687.png-wh_500x0-wm_3 -wmp_4-s_2713273021.png "title=" Qq20160808055119.png "alt=" Wkiom1enrw3zoqebaaatnus3yt4687.png-wh_50 "/>

4. Tree creation-----> Binary Tree

Storage structures that should have: tree nodes and trees

template<typename type>class treenode{    friend class tree< Type>;p Ublic:    treenode ()  : data (Type ()),  firstchild (NULL),  NextSibling (NULL) {}    treenode (Type d, treenode *first = null,  treenode *next = null)  :    data (d),  firstChild (first),  nextsibling (Next) {}    ~treenode () {}private:    type data;     treenode *firstchild;  //first child     treenode * nextsibling; //Next Brother};template<typename type>class tree{public:     Tree ()  : root (null) {}    tree (type ref)  : root (null),  refval (ref) {}    ~tree () {}private:    treenode<type> *root;     type            refval; //' # '}; 

5, the method should be implemented:

Public:void createtree (const char *STR);     Create a tree int size () const;   The number of nodes in the tree is int height () const;  The Tree high treenode<type>* root_1 () const;  Returns the root node bool IsEmpty () const;  Treenode<type> *firstchild () const of the tree empty; Returns the first child node of treenode<type> *nextsibling () const; Returns the first sibling node of treenode<type>* find (Type key) const;  Finds the current node treenode<type>* parent (treenode<type> *cur) const; Finds the parent of the current node.

(1), creating a tree (binary tree)-----> In our mind is the creation of a binary tree.

(2), to find the number of nodes---> the same as the root binary tree

(3), find the current node-----> like a binary tree

(4), Seeking tree height (forest can also be found):

    int height (treenode<type> *t) const{         TreeNode<Type> *p;        int m;         int max = 0;         if (t == null) {             return 0;  //empty tree, high 0        }else if (t-> Firstchild == null) {            return  1;  //only root, for 1        }else{             p = t->firstchild; //first child              while (p != null) {           &nBsp;     m = height (p);  //Gao                  if (max < m) {                     max = m;    //traverse all branches, each seeking the highest                  }                 p = p->nextsibling;  //every time to the right branch, but still ask for the left tree high;             }             return max+1;  //return with root height         }     }

         (5), find the parent of the current node (draw more tracking yourself)

    treenode<type>* parent (Treenode<type> *t, treenode<type > *cur) const{        if (t == null | |  cur == null | |  t == cur) {  //parent is null             return NULL;        }         TreeNode<Type> *p = t->firstChild;         while (p != null && p != cur) {             treenode<type> *tmp = parent (P, cur)  //recursively finds its parent node, and the results are given to TMP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF (TMP  != null) {                 return tmp;            }             p = p->nextsibling;  //'s looking right.         }        if (p !=  null && p == cur) {             return t;       //found the          }else{            return  null;        }    }

6, all code, test code, test results

(1), because of less, so all within the class implementation:

#ifndef  _TREE_H_#define _TREE_H_#include<iostream>using namespace std;template< typename type>class tree;template<typename type>class treenode{     friend class Tree<Type>;p Ublic:    treenode ()  : data (Type ( )),  firstchild (null),  nextsibling (null) {}    treenode (Type d, treenode  *first = null, treenode *next = null)  :    data (d ),  firstchild (first),  nextsibling (next) {}    ~treenode () {}private:     type data;    treenode *firstchild;    treenode  *nextSibling;}; Template<typename type>class tree{public:    tree ()  : root (NULL) {}    tree (Type ref)  : root (NULL),  refval (ref) {}  &nbsP; ~tree () {}public:    void createtree (CONST&NBSP;CHAR&NBSP;*STR) {         createtree (ROOT,&NBSP;STR);    }     int size () const{        return size (root);     }    int height () const{         Return height (root);     }    treenode<type>* root_1 () const{        return root;    }     bool isempty () const{        return root ==  null;    }    treenode<type> *firstchild () const{         if (root != null) {             return root->firstchild;        }         return NULL;    }    TreeNode< Type> *nextsibling () const{        if (Root != NULL) {             return root->nextSibling;         }        return  Null;    }    treenode<type>* find (const Type  &key) Const{        return find (Root, key);     }     treenode<type>* parent (TreeNode<Type> *cur) Const{        return parent (root, cur);     }protected:  &Nbsp; void createtree (TREENODE&LT;TYPE&GT;&NBSP;*&AMP;T,&NBSP;CONST&NBSP;CHAR&NBSP;*&AMP;STR) {         if (*str == refval) {             t = null;        }else{             t = new TreeNode< Type> (*STR);             createtree (t-> FIRSTCHILD,&NBSP;++STR);             createtree (t &GT;NEXTSIBLING,&NBSP;++STR);        }    }     int size (treenode<type> *t) const{         if (t == null) {            return  0;   &nBsp;    }        return size (T->firstChild )  + size (t->nextsibling)  + 1;    }    treenode <type>* parent (treenode<type> *t, treenode<type> *cur) const{         if (t == null | |  cur == null | |  t == cur) {            return  Null;        }        treenode <type> *p = t->firstchild;        while (P  != null && p != cur) {             treenode<type> *tmp = parent (p, cur);         &nBsp;   if (tmp != null) {                 return tmp;             }            p = p- >nextSibling;        }         if (p != null && p == cur) {             return t;        }else{             return NULL;         }    }    treenode<type>* find ( Treenode<type> *t, const type &key) const{         if (t == null) {            return NULL;         }        if (T->data == key) {            return t;         }        treenode<type>* p   = find (T->firstchild, key);         if (p  != null) {            return p;         }        return find (t >nextsibling, key);     }    int height (TreeNode<Type &GT;&NBSP;*T) const{        treenode<type> *p;         int m;        int max = 0;         if (t == null) {             return 0;        }else if (t-> Firstchild == null) {            return  1;        }else{             p = t->firstChild;             while (p != null) {                 m = height (P);                 if (max < m) {                     max = m;                 }                 p = p->nextSibling;             }             return max+1;        }    }private:     TreeNode<Type> *root;    Type            refval;  //' # '}; #endif

(2), test code:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/85/87/wKiom1enrW3zOQebAAAtNus3Yt4687.png-wh_500x0-wm_3 -wmp_4-s_2713273021.png "title=" Qq20160808055119.png "alt=" Wkiom1enrw3zoqebaaatnus3yt4687.png-wh_50 "/>

#include "tree.h" int main (void) {char *str = "rad#e# #B #cfg#h#k#####";    A sequence of two forks of the first root sequence;    Tree<char> t (' # ');    T.createtree (str);    treenode<char> *p = t.find (' C ');    treenode<char> *q = T.parent (p);    treenode<char> *m = t.find (' R ');    printf ("%p%p\n", Q, M);    Cout<<t.size () <<endl;        Cout<<t.height () <<endl; return 0;}

(3), test results

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/85/87/wKiom1entV3CmAvMAABj8zVE5jM470.png-wh_500x0-wm_3 -wmp_4-s_1954633056.png "title=" Qq20160808062236.png "alt=" Wkiom1entv3cmavmaabj8zve5jm470.png-wh_50 "/>





This article is from the "11586096" blog, please be sure to keep this source http://11596096.blog.51cto.com/11586096/1835487

Senrin and two-fork tree conversion

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.