dataStructure@ Binary Search Tree

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<limits>
 5 #include<vector>
 6 using namespace std;
 7 struct BST{
 8     int key;
 9     BST *lc, *rc;
10     BST(){
11         this->key = 0;
12         lc = rc = NULL;
13     }
14     BST(int k){
15         this->key = k;
16         lc = rc = NULL;
17     }
18 };
19 BST* newNode(int item){
20     BST *tmp = (BST*)malloc(sizeof(BST));
21     tmp->key = item;
22     tmp->lc = tmp->rc = NULL;
23     return tmp;
24 }
25 BST* insert(BST *root, int item){
26     if(root==NULL) return newNode(item);
27     if(item < root->key){
28         root->lc = insert(root->lc,item);
29     }
30     else if(item > root->key){
31         root->rc = insert(root->rc,item);
32     }
33     return root;
34 }
35 void inorder(BST *root){
36     if(root!=NULL){
37         inorder(root->lc);
38         cout<< root->key << endl;
39         inorder(root->rc);
40     }
41 }
42 int main(){
43     BST *root = new BST(50);
44     for(int i=10;i<=200;i+=30){
45         root = insert(root, i);
46     }
47     inorder(root);
48     return 0;
49 }
原文地址:https://www.cnblogs.com/fu11211129/p/4901562.html