大连理工大学C语言题目(十八)

74 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1.二叉搜索树的插入、删除、查找

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

class Node {
    friend class BinarySearchTree;
    int data;
    Node *leftChild;
    Node *rightChild;
public:
    explicit Node(int _data=0,Node*_leftChild=nullptr,Node*_rightChild=nullptr):data(_data),leftChild(_leftChild),rightChild(_rightChild){}

    int ShowData() const{
        return data;
    }
};

class BinarySearchTree {
    Node *root;
    stack<Node*>stack;
    queue<Node*>queue;
public:
    explicit BinarySearchTree(Node*_root= nullptr):root(_root){}

    bool InsertData(int data){
        if (!root)
            root=new Node(data);
        else{
            Node*p=root,*pre=nullptr;
            while (p){
                if (p->data==data)
                    return false;
                else if (p->data>data){
                    pre=p;
                    p=p->leftChild;
                } else{
                    pre=p;
                    p=p->rightChild;
                }
            }
            if (pre->data>data)
                pre->leftChild=new Node(data);
            else
                pre->rightChild=new Node(data);
        }
        return true;
    }

    Node* FindData(int data){
        Node*p=root;
        while (p){
            if (p->data==data)
                return p;
            else if (p->data>data)
                p=p->leftChild;
            else
                p=p->rightChild;
        }
        return nullptr;
    }

    bool DeleteData(int data){
        Node*p=root,*pre=nullptr;
        while (p){
            if (p->data==data){
                if (p->leftChild&&p->rightChild){
                    p=p->rightChild;
                    Node*q=nullptr;
                    while (p->leftChild){
                        q=p;
                        p=p->leftChild;
                    }
                    if (pre->data>data)
                        pre->leftChild->data=p->data;
                    else
                        pre->rightChild->data=p->data;
                    if (q->data>data)
                        q->leftChild=p->rightChild;
                    else
                        q->rightChild=p->rightChild;
                } else if (p->leftChild){
                    if (pre->data>data)
                        pre->leftChild=p->leftChild;
                    else
                        pre->rightChild=p->leftChild;
                } else if (p->rightChild){
                    if (pre->data>data)
                        pre->leftChild=p->rightChild;
                    else
                        pre->rightChild=p->rightChild;
                } else{
                    if (pre->data>data)
                        pre->leftChild= nullptr;
                    else
                        pre->rightChild= nullptr;
                }
                return true;
            } else if (p->data>data){
                pre=p;
                p=p->leftChild;
            } else{
                pre=p;
                p=p->rightChild;
            }
        }
        return false;
    }

    void BreadthFirstSearch(){
        Node*mark=new Node();
        int cnt=0;
        while (!queue.empty())
            queue.pop();
        queue.push(mark);
        queue.push(root);
        while (queue.front()!=queue.back()){
            if (queue.front()==mark){
                queue.push(mark);
                queue.pop();
                cnt++;
            } else{
                cout<<queue.front()->data<<' ';
                if (queue.front()->leftChild)
                    queue.push(queue.front()->leftChild);
                if (queue.front()->rightChild)
                    queue.push(queue.front()->rightChild);
                queue.pop();
            }
        }
        cout<<endl<<cnt<<" layers"<<endl;
    }

    void PreSearch(){
        Node*p=root;
        while (!stack.empty()||p){
            if (p){
                stack.push(p);
                cout<<p->data<<' ';
                p=p->leftChild;
            } else {
                p=stack.top()->rightChild;
                stack.pop();
            }
        }
    }
};

int main() {
    auto* binarySearchTree=new BinarySearchTree();
    binarySearchTree->InsertData(400);
    binarySearchTree->InsertData(122);
    binarySearchTree->InsertData(99);
    binarySearchTree->InsertData(110);
    binarySearchTree->InsertData(105);
    binarySearchTree->InsertData(250);
    binarySearchTree->InsertData(200);
    binarySearchTree->InsertData(300);
    binarySearchTree->InsertData(330);
    binarySearchTree->InsertData(450);
    binarySearchTree->InsertData(500);
    binarySearchTree->BreadthFirstSearch();
    if (binarySearchTree->FindData(500))
        cout<<binarySearchTree->FindData(500)->ShowData();
    cout<<endl;
    binarySearchTree->DeleteData(122);
    binarySearchTree->BreadthFirstSearch();
    binarySearchTree->PreSearch();
    return 0;
}

2.最小堆的初始化、插入、根结点删除(函数实现部分)


    void InsertData(int data){
        heap[currentSize++]=data;
        int temp;
        for (int i = currentSize-1; i >0 ; i=(i-1)/2) {
            if (heap[i]<heap[(i-1)/2]){
                temp=heap[i];
                heap[i]=heap[(i-1)/2];
                heap[(i-1)/2]=temp;
            }
        }
    }

    void DeleteTopData(){
        heap[0]=heap[currentSize---1];
        int temp;
        for (int j = 0; j <= currentSize/2-1; j++) {
            if (2*j+2<currentSize){
                if (heap[j]>heap[2*j+1]||heap[j]>heap[2*j+2]){
                    if (heap[2*j+1]>heap[2*j+2]){
                        temp=heap[j];
                        heap[j]=heap[2*j+2];
                        heap[2*j+2]=temp;
                    } else{
                        temp=heap[j];
                        heap[j]=heap[2*j+1];
                        heap[2*j+1]=temp;
                    }
                }
            } else{
                if (heap[j]>heap[2*j+1]){
                    temp=heap[j];
                    heap[j]=heap[2*j+1];
                    heap[2*j+1]=temp;
                }
            }
        }
    }

    void PrintHeap(){
        for (int i = 0; i < currentSize; ++i) {
            cout<<heap[i]<<' ';
        }
        cout<<endl;
    }
};