zoukankan      html  css  js  c++  java
  • 普通树(有根树)C++

    对于普通树实现的细节包括

    1 树结点的结构体

    2 初始化及删除树结点(关注内存泄露)

    3 递归先序遍历

    4 通过关键值的查询操作,返回关键值的结点

    5 凹入表实现

    6 广义表实现

    7 非递归先序遍历,利用栈作为辅助的数据结构

      1 #include <iostream>
      2 #include <crtdbg.h>
      3 #include <cstring>
      4 #include <assert.h>
      5 using namespace std;
      6 
      7 typedef int DataType;
      8 struct TreeNode
      9 {
     10     TreeNode()
     11     {
     12         sibling = NULL;
     13         child = NULL;
     14     }
     15     DataType data;
     16     TreeNode * sibling; //右兄弟结点
     17     TreeNode * child;   //左子结点
     18 };
     19 typedef TreeNode *DataStack; // 存储类型为 TreeNode*
     20 //普通树
     21 class Tree
     22 {
     23 private:
     24     TreeNode *root;
     25     Tree(const Tree &) {}//防止拷贝构造 与 赋值
     26     Tree & operator=(const Tree &) { return *this; }
     27 public:
     28     Tree(const DataType &data);
     29     ~Tree();
     30     const TreeNode * GetRoot()const { return root;}
     31     TreeNode * GetRoot() { return root;}
     32     void Init(const DataType &data);
     33     void Destroy(TreeNode * p);//删除根结点
     34     void DestroySub(TreeNode * p);//删除子结点;
     35     TreeNode * InsertChild(TreeNode *p, DataType data);
     36     TreeNode * InsertSibling(TreeNode *p, DataType data);
     37     void Print(const TreeNode * p); //先序遍历
     38     TreeNode *  Find(TreeNode * p, DataType data);//寻找data返回并返回结点
     39     void Print2(); //非递归先序遍历
     40 };
     41 //辅助类栈
     42 class Stack
     43 {
     44 private:
     45     struct Node
     46     {
     47         DataStack data;
     48         Node *next;
     49     };
     50     Node * head;
     51     void Init()
     52     {
     53         head = NULL;
     54     }
     55     void Destroy()
     56     {
     57         for (Node* p = head; p != NULL;)
     58         {
     59             Node *pTemp = p->next;
     60             delete p ;
     61             p = pTemp;
     62         }
     63         head = NULL;
     64     }
     65 public:
     66     Stack()
     67     {
     68         Init();
     69     }
     70     ~Stack()
     71     {
     72         Destroy();
     73     }
     74     void Push(DataStack  data)
     75     {
     76         Node *p = new Node;
     77         p->data = data;
     78         p->next = head;
     79         head = p;
     80     }
     81     DataStack Pop()
     82     {
     83         if (head == NULL)
     84         {
     85             return NULL;
     86         }
     87         Node *p = head;
     88         DataStack temp = p->data;
     89         head = head->next;
     90         delete p;
     91         p = NULL;
     92         return temp;
     93     }
     94     int Getlenth()
     95     {
     96         int n = 0;
     97         for (Node *p = head; p != NULL; p = p->next)
     98         {
     99             ++n;
    100         }
    101         return n;
    102     }
    103     DataStack Getop()
    104     {
    105         if (head == NULL)
    106         {
    107             return NULL;
    108         }
    109         return head->data;
    110     }
    111     bool Empty()
    112     {
    113         return (head == NULL);
    114     }
    115 };
    116 //普通树方法实现
    117 Tree::Tree(const DataType &data) 
    118 {
    119     Init(data);
    120 }
    121 Tree::~Tree () 
    122 { 
    123     Destroy(root);
    124 }
    125 void Tree::Init(const DataType &data)
    126 {
    127     root = new TreeNode;
    128     root->child = NULL;
    129     root->sibling = NULL;
    130     root->data = data;
    131 }
    132 void Tree::Destroy(TreeNode * p)
    133 {
    134     if (p == NULL)
    135     {
    136         return;
    137     }
    138     Destroy(p->sibling);
    139     Destroy(p->child);
    140     delete p;
    141     p = NULL;
    142 }
    143 void Tree::DestroySub(TreeNode * p)
    144 {
    145     if (p->child)
    146     {
    147         Destroy(p->child);
    148         p->child = NULL;
    149     }
    150 }
    151 TreeNode * Tree::InsertChild(TreeNode * p, DataType data)
    152 {
    153     if (p->child)
    154     {
    155         return p->child; //已有子结点
    156     }
    157     TreeNode *pNew = new TreeNode;
    158     pNew->data = data;
    159     p->child = pNew;
    160     return pNew;
    161 }
    162 TreeNode * Tree::InsertSibling(TreeNode * p, DataType data)
    163 {
    164     if (p->sibling) 
    165     {
    166         return p->sibling;//已有兄弟结点
    167     }
    168     TreeNode *pNew = new TreeNode;
    169     pNew->data = data;
    170     p->sibling = pNew;
    171     return pNew;
    172 }
    173 //先序遍历
    174 void  Tree::Print(const TreeNode * p)
    175 {
    176     if(p==NULL)
    177     {
    178         return;
    179     }
    180     cout << p->data << endl;
    181     Print(p->child);
    182     Print(p->sibling);
    183 }
    184 //寻找data并返回结点
    185  TreeNode *  Tree::Find(TreeNode * p, DataType data)
    186 {
    187     if(p == NULL)
    188         return NULL;
    189     if (p->data == data)
    190     {
    191         return p;
    192     }
    193     TreeNode *pFind = Find(p->child, data);
    194     if (pFind != NULL)
    195     {    
    196         return pFind;
    197     }
    198 
    199     return Find(p->sibling, data);
    200 }
    201 //非递归先序遍历
    202 void Tree::Print2()
    203 {
    204     TreeNode *p = GetRoot();
    205     if (p == NULL)
    206     {
    207         return;
    208     }
    209     Stack stack;
    210     
    211     while( (p != NULL) || (!stack.Empty()) )
    212     {
    213         if (p != NULL)
    214         {
    215             cout << p->data << endl;
    216             stack.Push(p);
    217             p = p->child;
    218         }
    219         else
    220         {
    221              p = stack.Pop();
    222              p = p->sibling;
    223         }
    224     }
    225 }
    226 void AoRuBiao(const TreeNode *p, int depth)
    227 {
    228     if (p == NULL)
    229     {
    230         return;
    231     }
    232     for (int i=0; i<depth; ++i)
    233     {
    234         cout << " ";
    235     }
    236     cout << p->data;
    237     for (int i=0; i<10-depth; ++i)
    238     {
    239         cout << " - ";
    240     }
    241     cout << endl;
    242     AoRuBiao(p->child, depth+1);
    243     AoRuBiao(p->sibling, depth);
    244 }
    245 void GuangYiBiao(const TreeNode *p)
    246 {
    247     if (p == NULL)
    248     {
    249         return;
    250     }
    251     cout << p->data;
    252     if (p->child)
    253     {
    254         cout << "(";
    255     }
    256     GuangYiBiao(p->child);
    257     if (p->child)
    258     {
    259         cout << ")";
    260     }
    261     if (p->sibling)
    262     {
    263         cout << ",";
    264     }
    265     GuangYiBiao(p->sibling);
    266 }
    267 
    268 void main()
    269 {
    270     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    271 
    272     Tree tree(10);
    273 
    274     //Stack s;
    275     //s.Push(tree.GetRoot());
    276     //cout << s.Getop()->data;
    277 
    278     TreeNode * p11 = tree.InsertChild(tree.GetRoot(), 11);
    279     TreeNode * p12 = tree.InsertSibling(p11,12);
    280     TreeNode * p13 = tree.InsertSibling(p12,13);
    281 
    282     TreeNode * p111 = tree.InsertChild(p11, 111);
    283     TreeNode * p112 = tree.InsertSibling(p111,112);
    284 
    285     TreeNode * p121 = tree.InsertChild(p12, 121);
    286     TreeNode * p122 = tree.InsertSibling(p121,122);
    287 
    288     tree.Print(tree.GetRoot());
    289     //tree.DestroySub(p12);
    290     //tree.Print(tree.GetRoot());
    291     cout << "
    
    Search p12" << endl;
    292     TreeNode * temp = tree.Find(tree.GetRoot(),12);
    293     if (temp != NULL)
    294     {
    295         cout << temp->child->data <<endl;
    296         cout << temp->sibling->data << endl;
    297     }
    298 
    299     cout << "
    
    AoRuBiao " << endl;
    300     AoRuBiao(tree.GetRoot(),1);
    301     
    302     cout << "
    GuangYiBiao" << endl;
    303     GuangYiBiao(tree.GetRoot());
    304 
    305     cout << "
    
    非递归遍历" << endl;
    306     tree.Print2();
    307 
    308     system("pause");
    309 } 

    (转载请注明作者和出处^_*  Seven++ http://www.cnblogs.com/sevenPP/  )

  • 相关阅读:
    OleView.exe:查看机器上的COM 组件。
    COM中导出GUID
    进程外组件以及进程间通信方式
    拼接多个 wchar_t *
    wstring to wchar_t*
    BSTR
    GetProcAddress 使用注意事项
    C++和.net的集合类对应
    COM的一些基本概念
    Error Lookup工具
  • 原文地址:https://www.cnblogs.com/sevenPP/p/3669840.html
Copyright © 2011-2022 走看看