zoukankan      html  css  js  c++  java
  • 数据结构基础---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
    ";
        cin>>number;
    
        if(Search(root,number) == true) cout<<"Found
    ";
        else cout<<"Not Found
    ";
    
        return 0;
    }
  • 相关阅读:
    N个数字每X个数字组成一组,求组数
    生成带文本的UIImage
    Linux创建环境变量(Mac OS)
    为UIView绘制单边的boder
    ecshop之随机文章
    微软继MVC5后,出现ASP.NET VNEXT
    本科毕业生转正之前谈待遇
    ecshop title优化
    百度地图开发之一】申请Key和配置初览显示地图
    项目总结—jQuery EasyUI-DataGrid 拼表及查看详情
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6526489.html
Copyright © 2011-2022 走看看