数据结构基础---Binary Search Tree

 

 

 

/// Binary Search Tree - Implemenation in C++
/// Simple program to create a BST of integers and search an element in it
#include<iostream>
#include "cstdio"
#include "queue"
using namespace std;
///Definition of Node for Binary search tree
struct BstNode {
    int data;
    BstNode* left;
    BstNode* right;
};
bool fist=true;
/// Function to create a new Node in heap
BstNode* GetNewNode(int data) {
    BstNode* newNode = new BstNode();
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}

///To insert data in BST, returns address of root node
BstNode* Insert(BstNode* root,int data) {
    if(root == NULL) { // empty tree
        root = GetNewNode(data);
    }

    else if(data <= root->data) {
        root->left = Insert(root->left,data);
    }

    else {
        root->right = Insert(root->right,data);
    }
    return root;
}
///To search an element in BST, returns true if element is found
bool Search(BstNode* root,int data) {
    if(root == NULL) {
        return false;
    }
    else if(root->data == data) {
        return true;
    }
    else if(data <= root->data) {
        return Search(root->left,data);
    }
    else {
        return Search(root->right,data);
    }
}

///PreOrder Display
void PreOrder(BstNode *root)
{
    if(root!=NULL){
        PreOrder(root->left);
        PreOrder(root->right);
        printf("%d ",root->data);
    }
}
///PostOrder Display
void PostOrder(BstNode *root)
{
    if(root!=NULL){
        printf("%d ",root->data);
        PostOrder(root->left);
        PostOrder(root->right);
    }
}

///InOrder Display
void InOrder(BstNode *root)
{
    if(root!=NULL){
        InOrder(root->left);
        printf("%d ",root->data);
        InOrder(root->right);
    }
}


///LevelOrder Display
void LevelOrder(BstNode *root)
{
    queue<BstNode *>Q;
    while(!Q.empty())Q.pop();
    Q.push(root);
    while(!Q.empty())
    {
        root=Q.front();
        Q.pop();
       if(fist==false)
        {
            cout<<root->data;
            fist=true;
        }
        else
        {
            cout<<" "<<root->data;
        }
        if(root->left!=NULL)Q.push(root->left);
        if(root->right!=NULL)Q.push(root->right);
    }
    cout<<endl;
}

///################### Test ############################
int main() {
    BstNode* root = NULL;  // Creating an empty tree, root is to store the address of the root node
    /*Code to test the logic*/
    root = Insert(root,15);
    root = Insert(root,10);
    root = Insert(root,20);
    root = Insert(root,25);
    root = Insert(root,8);
    root = Insert(root,12);
    cout<<"PreOrder:  ";
    PreOrder(root);
    cout<<endl;
    cout<<"InOrder(从小到大):  ";
    InOrder(root);
    cout<<endl;
    cout<<"PostOrder:  ";
    PostOrder(root);
    cout<<endl;
    cout<<"LevelOrder:  ";
    LevelOrder(root);
    cout<<endl;

    int number;
    cout<<"Enter number be searched\n";
    cin>>number;

    if(Search(root,number) == true) cout<<"Found\n";
    else cout<<"Not Found\n";

    return 0;
}

 

posted @ 2017-03-09 16:49  kimsimple  阅读(211)  评论(0编辑  收藏  举报