基本的二叉查找树代码:
#include <iostream>
using namespace std;
struct node{
int data;
struct node *parent;
struct node *lchild;
struct node *rchild;
};
//输出
void display(struct node *p)
{
if (p)
{
display(p->lchild);
cout<<p->data<<" ";
display(p->rchild);
}
return;
}
//最小值节点
struct node *min(struct node *root)
{
struct node *p, *back;
p = root;
while (p)
{
back = p;
p = p->lchild;
}
return back;
}
//返回最大值节点
struct node *max(struct node *root)
{
struct node *p, *back;
p = root;
while (p)
{
back = p;
p = p->rchild;
}
return back;
}
//后继
struct node *Successor(struct node *q)
{
struct node *y;
if (q->rchild != NULL)
{
return min(q->rchild);
}
y = q->parent;
while (y && y->rchild == q)
{
q = y;
y = y->parent;
}
return y;
}
//前驱
struct node *Presuccessor(struct node *q)
{
struct node *y;
if (q->lchild != NULL)
{
return max(q->lchild);
}
y = q->parent;
while (y && y->lchild == q)
{
q = y;
y = y->parent;
}
return y;
}
//查找
struct node *find(struct node *root, int value)
{
struct node *p;
p = root;
while(p && p->data != value){
if (p->data < value)
{
p = p->rchild;
}
else
{
p = p->lchild;
}
}
return p;
}
//插入节点
struct node *insert(struct node *root, struct node *q)
{
struct node *back = new node;
struct node *p = new node;
back = NULL;
p = root;
while (p != NULL)
{
back = p;
if (p->data < q->data)
{
p = p->rchild;
}
else
{
p = p->lchild;
}
}
q->parent = back;
if (back == NULL)
{
root = q;
}
else
{
if (back->data < q->data)
{
back->rchild = q;
}
else
{
back->lchild = q;
}
}
return root;
}
//删除节点
struct node *del(struct node *root, struct node *z)
{
struct node *y = new node;
struct node *x = new node;
x = NULL;
if (z->lchild == NULL || z->rchild == NULL){
y = z;
}
else {
y = Successor(z);
}
if (y->lchild != NULL) {
x = y->lchild;
}
else {
x = y->rchild;
}
if (x != NULL) {
x->parent = y->parent;
}
if (y->parent == NULL) {
root = x;
}
else {
if (y == y->parent->lchild){
y->parent->lchild = x;
}
else{
y->parent->rchild = x;
}
}
//
if (y != z){
z->data = y->data;
}
return root;
}
int main()
{
struct node *root = NULL;
int input;
cin>>input;
while (input)
{
struct node *p = new node;
p->data = input;
p->parent = NULL;
p->lchild = NULL;
p->rchild = NULL;
root = insert(root, p);
cin>>input;
}
display(root);
cout<<endl;
cout<<"Input the data you want to find: ";
cin>>input;
struct node *f = find(root, input);
if (f == NULL)
{
cout<<"Not exit!!"<<endl;
}
cout<<"Delete..."<<endl;
root = del(root, f);
//struct node *y = del(&root, f);
cout<<"After deteting...."<<endl;
display(root);
cout<<endl;
return 0;
}
分享到:
相关推荐
用C++实现的最优二叉查找树,简单,明了,是数据结构里经典必学算法,初学者适用
1、 定义二叉查找树的类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)实现二叉查找树结构; 2) 实现二叉查找树的查找、插入和删除等算法;
这个程序实现了二叉查找树的删除,增加,先序遍历,后序遍历,中序遍历,还有一些非递归和层次遍历!
二叉查找树c 源码
findwords查找文件是否有某单词,这是一个二叉查找树的练习
二叉查找树的C++实现
资源内容:完整的二叉查找树C++头文件,包括运算符重载,bst类构造器、bst类析构器、destroy()、size()、insert(),迭代器类的声明与实现,++运算符重载(前置、后置)、--运算符重载、*运算符重载、!=运算符重载、...
二叉查找树实现简单的信息检索
二叉查找树的实现。包括树的平衡以及树节点的删除。以及树的广度优先遍历,深度优先遍历。
课程的随堂作业,C语言的,用dev就能运行,萌新代码,勿喷,仅仅帮助不想写作业的朋友方便一下,反正老师也不会仔细检查的
二叉查找树
c++实现的二叉查找树,代码简陋,大家互相学习即可
二叉查找树的常用操作,含C++代码,找工作的时候可以放在手机里看。
二叉查找树的C语言实现,实现功能:插入、删除、查找。
作者给出了一种新的二叉查找树———红黑树的定义和建树方法,并给出了它在最坏情况下的查找效率估计。
二叉查找树 二分搜索树(英语:Binary Search Tree),也称为 二叉查找树 、二叉搜索树 、有序二叉树或排序二叉树。满足以下几个条件: 若它的左子树不为空,左子树上所有节点的值都小于它的根节点。 若它的右子树...
动态规划ppt(最优BST,矩阵连乘) 动态规划问题求解的步骤及分析 最优二叉查找树、矩阵连乘的问题分析、建模、伪代码
这里是二叉查找树的实现代码,如果有不明白的可以联系我,很多其他的C++代码我都有