二叉搜索树(递归)

本文详细介绍了二叉排序树的基本概念及其操作实现,包括递归建立、遍历、插入、删除节点等,并提供了完整的C++代码示例。通过具体实例展示了如何使用二叉树进行数据组织和检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

下面是二叉树的简单递归建立删除与遍历及清除,旋转操作方法:
#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;
}







例子:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值