zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    JavaIO学习:字符流
    Java学习:IO流
    Java中的Filter过滤器
    Java学习:File类中的过滤器接口
    Java学习:File类
    Java学习:递归
    多线程简介(全)
    Java学习:Lambda表达式
    Java学习:线程池
    Java学习:线程间通信
  • 原文地址:https://www.cnblogs.com/fu11211129/p/4901562.html
Copyright © 2011-2022 走看看