zoukankan      html  css  js  c++  java
  • 二叉排序树的创建、插入、删除

    二叉排序树的创建

    首先定义树节点treeNode:包含节点的值value 以及其左右孩子指针left right 

    1 class treeNode
    2 {
    3 public:
    4     int value;
    5     treeNode *right,*left;
    6     treeNode():right(NULL),left(NULL){}
    7 };

    定义二叉排序树Tree:包含根节点Root,节点个数num,以及构造函数(创建树)、中序遍历(输出排序结果)、插入和删除函数 

    #include<bits/stdc++.h>
    using namespace std;
    class Node{
        public:
            int value;
            Node *lchild,*rchild;
            Node():lchild(NULL),rchild(NULL){};
            ~Node(){};
    };
    class Tree{
        int num;
        Node *root;
        void Inorder(Node *t);
        void CreateTree(int a,Node *&t);
        void Search(int a,Node *&t);
        void Delete(int a,Node *&t);
        void setnum();
        public:
        Tree(){
            root=NULL;num=0;
        } 
        ~Tree(){};
        void Inorder();
        void Create(int a);
        void Search(int a);
        void Delete(int a);
    };
    void Tree::setnum(){
        num=0;
    }
    void Tree::Create(int a){
        CreateTree(a,root);
    } 
    void Tree::CreateTree(int a,Node *&t){
        if(t==NULL){
            t=new Node;
            t->value=a;
            t->lchild=NULL;t->rchild=NULL;
        }
        else{
            if(t->value>a){
                CreateTree(a,t->lchild);
            }
            else{
                CreateTree(a,t->rchild);
            }
        }
    }
    void Tree::Inorder(){
        Inorder(root);
        cout<<endl;
    }
    void Tree::Inorder(Node *t){
        if(t){
            Inorder(t->lchild);
            cout<<t->value<<" ";
            Inorder(t->rchild);
        }
    }
    void Tree::Search(int a){
        setnum();
        Search(a,root);
    }
    void Tree::Search(int a,Node *&t){
        if(t==NULL){
            cout<<"-1"<<endl;return;
        }
        else{
            if(a==t->value){
                num++;cout<<num<<endl;return;
            }
            else if(a<t->value){
                num++;Search(a,t->lchild);
            }
            else if(a>t->value){
                num++;Search(a,t->rchild);
            }
        } 
    }
    void Tree::Delete(int a){
        Delete(a,root);
    }
    void Tree::Delete(int a,Node *&t){
        if(t){
            if(a==t->value){
                if(t->lchild==NULL){
                    Node *n=t;
                    t=t->rchild;
                    free(n);
                }
                else if(t->rchild==NULL){
                    Node *n=t;
                    t=t->lchild;
                    free(n);
                }
                else{
                    Node *m=t->lchild;
                    Node *n=t; 
                    while(m->rchild){
                        n=m;
                        m=m->rchild;
                    }
                    t->value=m->value;
                    if(n!=t){
                        n->rchild=m->lchild;
                    }
                    else{
                        n->lchild=m->lchild;
                    }
                    free(m);
                }
            }
            else if(a<t->value){
                Delete(a,t->lchild);
            }
            else if(a>t->value){
                Delete(a,t->rchild);
            }
        } 
    }
    int main()
    {
        int t;cin>>t;
        while(t--){
            int n;cin>>n;
            int *a=new int[n];
            Tree tree;
            for(int i=0;i<n;i++){
                cin>>a[i];
                tree.Create(a[i]);
            }
            tree.Inorder();
            int m;cin>>m;
            while(m--){
                int b;cin>>b;
                tree.Delete(b);
                tree.Inorder();
            }
        }
        return 0;
    }

    18.设二叉排序树中关键字由1至1000的整数构成,现要查找关键字为363的结点,下述关键字序列(  )不可能是在二叉排序树上查找到的序列?  

    A)2,252,401,398,330, 344,397,363

    B)924, 220, 911, 244, 898, 258, 362, 363

    C)2, 399, 387, 219, 266, 382, 381, 278, 363

    D)925, 202, 911, 240, 912, 245, 363

    【答案】D

  • 相关阅读:
    OSI,TCP/IP,五层协议的体系结构,以及各层协议
    linux及安全期中总结——20135227黄晓妍
    linux及安全《Linux内核设计与实现》第四章——20135227黄晓妍
    linux及安全《Linux内核设计与实现》第三章——20135227黄晓妍
    linux及安全第八周总结——20135227黄晓妍
    linux及安全第七周总结——20135227黄晓妍
    linux及安全第六周总结——20135227黄晓妍
    linux及安全第五周总结——20135227黄晓妍
    linux及安全第四周总结——20135227黄晓妍
    linux及安全《Linux内核设计与实现》第二章——20135227黄晓妍
  • 原文地址:https://www.cnblogs.com/Liu269393/p/10212528.html
Copyright © 2011-2022 走看看