二叉搜索树(递归)

来源:互联网 发布:中信出版社 知乎 编辑:程序博客网 时间:2024/05/20 08:24

二叉排序树也叫二叉搜索树和二叉查找树,是比较常见的一种二叉树

下面是二叉树的简单递归建立删除与遍历及清除,旋转操作方法:
#include<iostream>using namespace std;class binary {    private:    struct node {        int data;        node* left;        node* right;        node(int t= 0, node* ll= NULL, node* rr= NULL): data(t), left(ll), right(rr) {}    };    node* root;    public:    binary(): root(NULL) {}    ~binary() {        clear(root);    }    void prevtravel() {        prevtravel(root);}    void prevtravel(node* tree) {        if (tree== NULL) return;        cout<< " "<< tree->data;        prevtravel(tree->left);        prevtravel(tree->right);    }    void intravel() {    intravel(root);}    void intravel(node* tree) {        if (tree== NULL) return;        intravel(tree->left);        cout<< " "<< tree->data;        intravel(tree->right);    }    void posttravel() {    posttravel(root);}    void posttravel(node* tree) {        if (tree== NULL) return;        posttravel(tree->left);        posttravel(tree->right);        cout<< " "<< tree->data;    }    void insert(int t) {        insert(root, t);    }    void insert(node*& tree, int t) {        if (tree== NULL) tree= new node(t);        else if (t<= tree->data) insert(tree->left, t);        else insert(tree->right, t);    }    void remove(int t) {    remove(t, root);}void remove(int t, node*& r) {if (r== NULL) return;if (t< r->data) remove(t, r->left);else if (r->data< t) remove(t, r->right);else if (r->left!= NULL&&r->right!= NULL) {node* temp= r->right;while (temp->left!= NULL) temp= temp->left;r->data= temp->data;remove(r->data, r->right);} else {node* n= r;r= (r->left!= NULL)? r->left: r->right;delete n;}}void reverse() {      queue<Node*> q;    if (root!= NULL) q.push(root);    while (!q.empty()) {        Node *t_root= q.front();        q.pop();        if (t_root->left!= NULL||t_root->right!= NULL) {        if (t_root->left== NULL) {        t_root->left= t_root->right;            t_root->right= NULL;        } else if (t_root->right== NULL) {        t_root->right= t_root->left;        t_root->left= NULL;        } else if (t_root->left!= NULL&&t_root->right!= NULL) {        Node* temp= t_root->left;        t_root->left= t_root->right;        t_root->right= temp;        }        Node *l= t_root->left;    Node *r= t_root->right;        if (l!= NULL&&(l->left!= NULL||l->right!= NULL)) q.push(l);        if (r!= NULL&&(r->left!= NULL||r->right!= NULL)) q.push(r);}    }  }    void clear(node* tree) {        if (tree== NULL) return;        clear(tree->left);        clear(tree->right);        delete tree;    }};int main() {    int n;    binary tree;    cin>> n;    for (int i= 0; i< n; i++) {        tree.insert(i);    }    cout<< "Inorder:";    tree.intravel();    cout<< "\n";    cout<< "Preorder:";    tree.prevtravel();    cout<< "\n";    cout<< "Postorder:";    tree.posttravel();    cout<< "\n";    for (int i= 0; i< n; i+= 2) // 删除偶数    tree.remove(i);    cout<< "Inorder:";    tree.intravel();    cout<< "\n";    cout<< "Preorder:";    tree.prevtravel();    cout<< "\n";    cout<< "Postorder:";    tree.posttravel();    cout<< "\n";    return 0;}







例子:



0 0
原创粉丝点击