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;
    }
  • 相关阅读:
    Assets Pipeline
    how to execute-shell-commands by ruby
    DFS---迷宫问题
    病毒感染监测
    RE数组开多大?
    C++如何输入含空格的字符串
    后缀算术表达式
    中缀表达式转化为后缀表达式
    基于两端操作的循环队列的实现---怎么判断队满??
    循环队列--忘记分配空间和如何用tag判断队空队满
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6526489.html
Copyright © 2011-2022 走看看