gha890826

二元樹

Nov 29th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ConsoleApplication2.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. class treenode
  9. {
  10. public:
  11.     int data;
  12.     treenode *l, *r;
  13.     treenode(int in){
  14.         data = in;
  15.         this->l = NULL;
  16.         this->r = NULL;
  17.     }
  18. };
  19.  
  20. class Btree
  21. {
  22. public:
  23.     treenode *root;
  24.     Btree(){ root = NULL; }
  25.     void Add_TreeNode(int);
  26.     void in_order(treenode *pos);
  27.     void pre_order(treenode *pos);
  28.     void post_order(treenode *pos);
  29.     void delnode(int value);
  30. };
  31.  
  32. void Btree::Add_TreeNode(int value)
  33. {
  34.     //cout << "root:" << root << endl;
  35.     if (root == NULL)
  36.     {
  37.         root = new treenode(value);
  38.         cout << "root value=" << root->data << endl;
  39.     }
  40.     else
  41.     {
  42.         //cout << "rootvalue" << root->data << endl;
  43.         //cout << "rootl:" << root->l << endl;
  44.         treenode *pos = root;
  45.         while (true)
  46.         {
  47.             /*
  48.             cout << "posl:" << pos->l << endl;
  49.             cout << "posr:" << pos->r << endl;
  50.             cout << "pos=" << pos << endl;
  51.             */
  52.             if (value < pos->data)
  53.             {
  54.                 if (pos->l != NULL)
  55.                 {
  56.                     pos = pos->l;
  57.                 }
  58.                 else
  59.                 {
  60.                     pos->l = new treenode(value);
  61.                     cout << "add " << value << " at " << pos->l << endl;
  62.                     return;
  63.                 }
  64.             }
  65.             else
  66.             {
  67.                 if (pos->r != NULL)
  68.                 {
  69.                     pos = pos->r;
  70.                 }
  71.                 else
  72.                 {
  73.                     pos->r = new treenode(value);
  74.                     cout << "add " << value << " at " << pos->r << endl;
  75.                     return;
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82. void Btree::in_order(treenode *pos)
  83. {
  84.     if (pos != NULL)
  85.     {
  86.         in_order(pos->l);
  87.         cout << "[" << pos->data << "]";
  88.         in_order(pos->r);
  89.     }
  90. }
  91. void  Btree::pre_order(treenode *pos)
  92. {
  93.     if (pos != NULL)
  94.     {
  95.         cout << "[" << pos->data << "]";
  96.         pre_order(pos->l);
  97.         pre_order(pos->r);
  98.     }
  99. }
  100.  
  101. void  Btree::post_order(treenode *pos)
  102. {
  103.     if (pos != NULL)
  104.     {
  105.         post_order(pos->l);
  106.         post_order(pos->r);
  107.         cout << "[" << pos->data << "]";
  108.     }
  109. }
  110.  
  111. void Btree:: delnode(int value)
  112. {
  113.     cout << "pop " << value << endl;
  114.     treenode *pos = root,*del=pos;
  115.     if (root == NULL)
  116.     {
  117.         cout << "empty tree\n";
  118.         return;
  119.     }
  120.     while (true)
  121.     {
  122.         if (del == NULL)
  123.         {
  124.             cout << "can't find this value\n";
  125.             return;
  126.         }
  127.         else if (del->data != value)
  128.         {
  129.             pos = del;
  130.             if (value>del->data)
  131.                 del = del->r;
  132.             else
  133.                 del = del->l;
  134.         }
  135.         else
  136.         {
  137.             if (del->l == NULL&&del->r == NULL)
  138.             {
  139.                 cout << "delete" << del->data << endl;
  140.                 delete del;
  141.             if (value > pos->data)
  142.                 pos->r = NULL;
  143.             else
  144.                 pos->l = NULL;
  145.             return;
  146.             }
  147.  
  148.         }
  149.     }
  150. }
  151.  
  152. int main()
  153. {
  154.     Btree tr1;
  155.     const int listnum = 6;
  156.     int list[listnum] = {8,17,31,9,98,12};
  157.     for (int i = 0; i < listnum; i++)
  158.         tr1.Add_TreeNode(list[i]);
  159.     cout << "inorder:";
  160.     tr1.in_order(tr1.root);
  161.     cout << "\npreorder:";
  162.     tr1.pre_order(tr1.root);
  163.     cout << "\npostorder:";
  164.     tr1.post_order(tr1.root);
  165.     cout << endl;
  166.  
  167.     tr1.delnode(9);
  168.     return 0;
  169. }
Add Comment
Please, Sign In to add comment