查找算法之二叉查找树
2015-08-30 08:47
393 查看
一 基本概念
由于线性表的查找算法对动态操作的支持效率很低,因此将几种特殊的树或者二叉树作为表的一种组织方式。
二叉树查找树非空树的话,具有下列性质:
(1)若它的左子树非空,则左子树上所有的结点的值均小于根结点的值。
(2)若它的右子树非空,则右子树上所有的结点的值均大于根结点的值。
(3)左子树和右子树又均为一颗二叉查找树。
其实,二叉查找树的中序遍历是一个有序序列。
二 基本操作
取二叉链表作二叉查找树的存储结构。
1.结点定义:
2.二叉树表定义:
3. 二叉查找树的生成:
用任意一关键字序列构造二叉查找树,其实是对这一序列数进行排序,所以二叉查找树又称为二叉排序树。
4.插入算法:
(1)若二叉查找树为空,则使新插入的结点为根结点插入树中。
(2)若二叉查找树不为空,则将带插入结点的关键字值与根节点的关键字值相比较,若插入的元素值小于根节点值,则将元素插入到左子树中。若插入的元素值不小于根节点值,则将元素插入到右子树中。
5.查找算法:
(1)若二叉查找树为空,查找失败。
(2)若二叉查找树根节点的关键值与查找值相等,查找成功。
(3)若查找的元素值小于根节点值,则在左子树中查找。
(4)若插入的元素值不小于根节点值,则在右子树中查找。
(1)递归查找:
(2)非递归查找
6.删除算法:
删除点p分三种情况讨论:
(1)删除结点p为叶子节点,直接删除该节点,再修改其父节点的指针(注意分是根节点和不是根节点)。
(2)删除结点p为单支节点(即只有左子树或右子树),让p的子树与p的父亲节点相连,删除p即可。
(3)删除结点p的左子树和右子树均不空,找到p的直接前驱(或直接后驱)s,利用s代替p,再删除s。(将二叉查找树中序遍历后,看到顺序序列后就知道直接前驱和直接后驱)
7.附件:
完整版程序:
由于线性表的查找算法对动态操作的支持效率很低,因此将几种特殊的树或者二叉树作为表的一种组织方式。
二叉树查找树非空树的话,具有下列性质:
(1)若它的左子树非空,则左子树上所有的结点的值均小于根结点的值。
(2)若它的右子树非空,则右子树上所有的结点的值均大于根结点的值。
(3)左子树和右子树又均为一颗二叉查找树。
其实,二叉查找树的中序遍历是一个有序序列。
二 基本操作
取二叉链表作二叉查找树的存储结构。
1.结点定义:
class node { public: node() { rightchild = nullptr; leftchild = nullptr; value = 0; } ~node(){} private: int value; node* rightchild; node* leftchild; };
2.二叉树表定义:
class tree { friend ostream& operator<<(ostream& out,tree& list); public: tree():head(nullptr){} ~tree(){} private: node* head; public: void create(); //生成二叉树表 void find(int val); //查找 void erase(int val);//删除 void insert(int val);//插入 private: void InOrder(node* d); //中序遍历,用来辅助输出 node* find(node* ptr, int val); int erasenode(node* ptr, int val); void insert(node*& ptr, int val); };
3. 二叉查找树的生成:
用任意一关键字序列构造二叉查找树,其实是对这一序列数进行排序,所以二叉查找树又称为二叉排序树。
void tree::create() { int num; cout << "输入数据" << endl; while (cin >> num) { if (head == nullptr) { head = new node; head->value = num; } else insert(head, num); } }
4.插入算法:
(1)若二叉查找树为空,则使新插入的结点为根结点插入树中。
(2)若二叉查找树不为空,则将带插入结点的关键字值与根节点的关键字值相比较,若插入的元素值小于根节点值,则将元素插入到左子树中。若插入的元素值不小于根节点值,则将元素插入到右子树中。
void tree::insert(node*& ptr, int val) { if (ptr == nullptr) { ptr = new node; ptr->value = val; } else { if (val > ptr->value) insert(ptr->rightchild, val); else insert(ptr->leftchild, val); } }
5.查找算法:
(1)若二叉查找树为空,查找失败。
(2)若二叉查找树根节点的关键值与查找值相等,查找成功。
(3)若查找的元素值小于根节点值,则在左子树中查找。
(4)若插入的元素值不小于根节点值,则在右子树中查找。
(1)递归查找:
node* tree::find(node* ptr, int val) { if (ptr == nullptr) //查找失败 return nullptr; else { if (ptr->value == val) //查找成功 return ptr; else { if (val > ptr->value) return find(ptr->rightchild, val); //在右子树查找 else return find(ptr->leftchild, val); //在左子树查找 } } }
(2)非递归查找
node* tree::find(node* ptr, int val) { if (ptr == nullptr) //查找失败 return nullptr; else { while (ptr) { if (ptr->value == val) //查找成功,跳出循环 break; else { if (val > ptr->value) ptr = ptr->rightchild; else ptr = ptr->leftchild; } } if (ptr == nullptr) //查找失败 return nullptr; else return ptr; //查找成功,返回结点信息 } }
6.删除算法:
删除点p分三种情况讨论:
(1)删除结点p为叶子节点,直接删除该节点,再修改其父节点的指针(注意分是根节点和不是根节点)。
(2)删除结点p为单支节点(即只有左子树或右子树),让p的子树与p的父亲节点相连,删除p即可。
(3)删除结点p的左子树和右子树均不空,找到p的直接前驱(或直接后驱)s,利用s代替p,再删除s。(将二叉查找树中序遍历后,看到顺序序列后就知道直接前驱和直接后驱)
int tree::erasenode(node* ptr,int val) { node* temp = ptr; node* ps = nullptr; while (temp) //查找待删除结点 { if (temp->value == val) //查找成功 break; else { if (val > temp->value) { ps = temp; //记录父节点 temp = temp->rightchild; } else { ps = temp; temp = temp->rightchild; } } } if (temp == nullptr) //没找到 return 0; if (temp->rightchild == nullptr&&temp->leftchild == nullptr) //待删除结点是叶子结点 { if (temp == ptr) ptr = nullptr; else { if (temp == ps->leftchild) ps->leftchild = nullptr; else ps->rightchild = nullptr; } } else if ((temp->leftchild == nullptr) || (temp->rightchild == nullptr)) //待删除结点仅有左孩子或右孩子 { if (temp == ptr) { if ((temp == ps->leftchild) && (temp->leftchild != nullptr)) { ps->leftchild = temp->leftchild; } else if ((temp == ps->leftchild) && (temp->rightchild != nullptr)) { ps->leftchild = temp->rightchild; } else if ((temp == ps->rightchild) && (temp->rightchild != nullptr)) { ps->rightchild = temp->rightchild; } else if ((temp == ps->rightchild) && (temp->leftchild != nullptr)) { ps->rightchild = temp->leftchild; } } } else if ((temp->leftchild != nullptr) && (temp->rightchild != nullptr)) //待删除结点同时有左孩子和右孩子 { node* p = temp, *q = temp->leftchild; while(q->rightchild != nullptr) //找到待删除结点的前驱 { p = q; q = q->rightchild; } temp->value = q->value; //待删除结点用直接前驱结点代替 if (p == temp) temp->leftchild = q->leftchild; else p->rightchild = q->leftchild; delete q; //删除前驱结点代替待删除结点 } return 1; //表示已找到要删除的结点,并成功删除 }
7.附件:
完整版程序:
#include<iostream>
#include<string>
using namespace std;
class node
{
friend class tree;
public:
node()
{
rightchild = nullptr;
leftchild = nullptr;
value = 0;
}
~node(){}
private:
int value;
node* rightchild;
node* leftchild;
};
class tree { friend ostream& operator<<(ostream& out,tree& list); public: tree():head(nullptr){} ~tree(){} private: node* head; public: void create(); //生成二叉树表 void find(int val); //查找 void erase(int val);//删除 void insert(int val);//插入 private: void InOrder(node* d); //中序遍历,用来辅助输出 node* find(node* ptr, int val); int erasenode(node* ptr, int val); void insert(node*& ptr, int val); };
void tree::erase(int val)
{
if (1 == erasenode(head, val))
cout << "删除成功" << endl;
else
cout << "删除失败" << endl;
}
void tree::insert(int val)
{
insert(head, val);
}
void tree::find(int val)
{
node* temp = find(head, val);
if (temp == nullptr)
cout << "查找失败" << endl;
else
cout << "查找成功" << endl;
}
ostream& operator<<(ostream& out, tree& list)
{
list.InOrder(list.head);
//cout << endl;
return out;
}
void tree::InOrder(node* d)
{
if (d)
{
InOrder(d->leftchild);
cout << d->value <<" ";
InOrder(d->rightchild);
}
}
void tree::create() { int num; cout << "输入数据" << endl; while (cin >> num) { if (head == nullptr) { head = new node; head->value = num; } else insert(head, num); } }
void tree::insert(node*& ptr, int val)
{
if (ptr == nullptr)
{
ptr = new node;
ptr->value = val;
ptr->leftchild = nullptr;
ptr->rightchild = nullptr;
}
else
{
if (val > ptr->value)
insert(ptr->rightchild, val);
else
insert(ptr->leftchild, val);
}
}
/*node* tree::find(node* ptr, int val) { if (ptr == nullptr) //查找失败 return nullptr; else { if (ptr->value == val) //查找成功 return ptr; else { if (val > ptr->value) return find(ptr->rightchild, val); //在右子树查找 else return find(ptr->leftchild, val); //在左子树查找 } } }*/
node* tree::find(node* ptr, int val) { if (ptr == nullptr) //查找失败 return nullptr; else { while (ptr) { if (ptr->value == val) //查找成功,跳出循环 break; else { if (val > ptr->value) ptr = ptr->rightchild; else ptr = ptr->leftchild; } } if (ptr == nullptr) //查找失败 return nullptr; else return ptr; //查找成功,返回结点信息 } }
int tree::erasenode(node* ptr,int val) { node* temp = ptr; node* ps = nullptr; while (temp) //查找待删除结点 { if (temp->value == val) //查找成功 break; else { if (val > temp->value) { ps = temp; //记录父节点 temp = temp->rightchild; } else { ps = temp; temp = temp->rightchild; } } } if (temp == nullptr) //没找到 return 0; if (temp->rightchild == nullptr&&temp->leftchild == nullptr) //待删除结点是叶子结点 { if (temp == ptr) ptr = nullptr; else { if (temp == ps->leftchild) ps->leftchild = nullptr; else ps->rightchild = nullptr; } } else if ((temp->leftchild == nullptr) || (temp->rightchild == nullptr)) //待删除结点仅有左孩子或右孩子 { if (temp == ptr) { if ((temp == ps->leftchild) && (temp->leftchild != nullptr)) { ps->leftchild = temp->leftchild; } else if ((temp == ps->leftchild) && (temp->rightchild != nullptr)) { ps->leftchild = temp->rightchild; } else if ((temp == ps->rightchild) && (temp->rightchild != nullptr)) { ps->rightchild = temp->rightchild; } else if ((temp == ps->rightchild) && (temp->leftchild != nullptr)) { ps->rightchild = temp->leftchild; } } } else if ((temp->leftchild != nullptr) && (temp->rightchild != nullptr)) //待删除结点同时有左孩子和右孩子 { node* p = temp, *q = temp->leftchild; while(q->rightchild != nullptr) //找到待删除结点的前驱 { p = q; q = q->rightchild; } temp->value = q->value; //待删除结点用直接前驱结点代替 if (p == temp) temp->leftchild = q->leftchild; else p->rightchild = q->leftchild; delete q; //删除前驱结点代替待删除结点 } return 1; //表示已找到要删除的结点,并成功删除 }
int main()
{
tree lst;
lst.create();
cout << lst << endl;
lst.find(10);
lst.erase(5);
cout << lst << endl;
lst.insert(18);
cout << lst << endl;
system("pause");
return 0;
}
相关文章推荐
- POJ 3468 A Simple Problem with Integers(BIT)
- POJ 3458 && HDU 1683 Colour Sequence(水~)
- 今日学习的知识点罗列
- Android的JSON数据解析
- Maven 让事情变得简单
- 零基础学python-9.3 对象的比较
- 零基础学python-9.3 对象的比较
- POJ 3427 Ecology tax(水~)
- POJ 3386 Halloween Holidays(计算几何)
- 百度竞价点击价格如何计算?百度竞价点击价格的计算公式及方法
- 在 Linux 中使用日志来排错
- POJ 3225 && HDU 2309 ICPC Score Totalizer Software(水~)
- Chef and Prime Divisors (CodeChef CHAPD
- Codeforces Round #318 574C Bear and Elections(脑洞)
- CoreAnimation-04-核心动画必备基础
- django 1.8 官方文档翻译: 2-1-4 Model 类参考
- POJ 3286 How many 0's?(数论)
- 如何搭建一个独立博客——简明Github Pages与Hexo教程
- POJ 3279 Fliptile(反转)
- CodeForces 574C - Bear and Poker