Advertisement
gha890826

Binary Search Tree

Nov 22nd, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. // ConsoleApplication4.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. class treenode
  9. {
  10. public:
  11.     int value=NULL;
  12.     treenode *l=NULL, *r=NULL;
  13. };
  14.  
  15. class tree
  16. {
  17. public:
  18.     void append(int);
  19.     void InOrder(treenode*);
  20.     void PreOrder(treenode*);
  21.     void PostOrder(treenode*);
  22.     treenode *root = new(treenode);
  23. private:
  24. };
  25.  
  26. void tree::append(int in)
  27. {
  28.     treenode* pos = root;
  29.     if (root->value == NULL)
  30.     {
  31.         root->value = in;
  32.         cout << "append " << in << " to root" << endl;
  33.         goto end;
  34.     }
  35.     while (true)
  36.     {
  37.         if (pos->value < in)
  38.         {
  39.             if (pos->l != NULL)
  40.             {
  41.                 pos = pos->l;
  42.             }
  43.             else
  44.             {
  45.                 pos->l = new(treenode);
  46.                 pos->l->value = in;
  47.                 cout << "append " << in << endl;
  48.                 goto end;
  49.             }
  50.         }
  51.         else if (pos->value > in)
  52.         {
  53.             if (pos->r != NULL)
  54.             {
  55.                 pos = pos->r;
  56.             }
  57.             else
  58.             {
  59.                 pos->r = new(treenode);
  60.                 pos->r->value = in;
  61.                 cout << "append " << in << endl;
  62.                 goto end;
  63.             }
  64.         }
  65.         else
  66.         {
  67.             cout << pos->value << ' ' << in;
  68.             cout << "append same value\n";
  69.             goto end;
  70.         }
  71.     }
  72.     end:
  73.         return;
  74. }
  75.  
  76. void tree::InOrder(treenode *pos)
  77. {
  78.     if (pos != NULL)
  79.     {
  80.         InOrder(pos->l);
  81.         cout << '[' << pos->value << ']';
  82.         InOrder(pos->r);
  83.     }
  84. }
  85.  
  86. void tree::PreOrder(treenode *pos)
  87. {
  88.     if (pos != NULL)
  89.     {
  90.         cout << '[' << pos->value << ']';
  91.         PreOrder(pos->l);
  92.         PreOrder(pos->r);
  93.     }
  94. }
  95. void tree::PostOrder(treenode *pos)
  96. {
  97.     if (pos != NULL)
  98.     {
  99.         PostOrder(pos->l);
  100.         PostOrder(pos->r);
  101.         cout << '[' << pos->value << ']';
  102.     }
  103. }
  104.  
  105. int main()
  106. {
  107.     tree ta;
  108.     ta.append(50);
  109.     ta.append(40);
  110.     ta.append(30);
  111.     ta.append(45);
  112.     ta.append(48);
  113.     ta.append(65);
  114.     ta.append(70);
  115.     ta.InOrder(ta.root);
  116.     cout << endl;
  117.     ta.PreOrder(ta.root);
  118.     cout << endl;
  119.     ta.PostOrder(ta.root);
  120.     cout << endl;
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement