二叉排序树实现(C++封装)
设计思路
设计一个类,根结点只可读取,具备构造二叉树、插入结点、删除结点、查找、 查找最大值、查找最小值、查找指定结点的前驱和后继等功能接口。
二叉排序树概念
它或者是一棵空树;或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树。
二叉排序树的各种操作
插入新节点
这是一个递归操作,递归设计时要找到最源头,才能得到最简设计。一种设计是判断叶子节点,把新节点作为叶子节点的孩子插入;一种是永远当作根进行插入,插入节点永远是当前子树的根!看代码:
//root为二级指针的原因是,如果树为空,需要将根修改反馈回来
bool BinaryTree::InsertNode(pNode * cuRoot, int data, pNode self)
{ //递归设计时找到最源头,才能得到最简设计
if (*cuRoot == nullptr){
pNode node = new Node;
if (node == nullptr)
return false;
node->data = data;
node->lChild = node->rChild = node->parent = nullptr;
(*cuRoot) = node;
node->parent = self;
return true;
}
if (data > (*cuRoot)->data)
InsertNode(&(*cuRoot)->rChild, data, *cuRoot);
else
InsertNode(&(*cuRoot)->lChild, data, *cuRoot);
return true;
}
构造函数
一共两个重载函数:一个无参,一个接受数组利用插入函数直接构造二叉排序树。
BinaryTree::BinaryTree(int * datum, int len)
{
root = nullptr;
for (int i = ; i < len; i++)
InsertNode(&root, datum[i], root);
} BinaryTree::BinaryTree()
{
root = nullptr;
}
查找函数
这也是一个递归操作,为了对外隐藏root(根节点),因此编写了一个私有函数,进行真正的查找操作。
//真正的查找函数
BinaryTree::pNode BinaryTree::_searchKey(pNode root, int key){
if (root == nullptr)
return nullptr;
if (root->data == key) //找到了
return root;
else if (root->data > key)//值偏小,到左子树找
return _searchKey(root->lChild, key);
else //值偏大,到右子树找
return _searchKey(root->rChild, key);
}
//对外接口
BinaryTree::pNode BinaryTree::SearchKey(int key){
return _searchKey(root, key);
}
找前驱节点
要么为左子树中最大者,要么一直追溯其父节点链,第一个是其父节点的右孩子的父节点,即为所求。
BinaryTree::pNode BinaryTree::SearchPredecessor(pNode node){
if (node == nullptr)
return nullptr;
else if (node->lChild != nullptr)
return SearchMaxNode(node->lChild);
else
{
if (node->parent == nullptr)
return nullptr;
while (node)
{
if (node->parent->rChild == node)
break;
node = node->parent;
}
return node->parent;
}
}
找后继节点
与找前驱节点基本相似。 要么为右子树中最小者,要么一直追溯其父节点链,第一个是其父节点的左孩子的父节点,即为所求。
BinaryTree::pNode BinaryTree::SearchSuccessor(pNode node){
if (node == nullptr)
return nullptr;
else if (node->rChild != nullptr)
return SearchMinNode(node->rChild);
else
{
if (node->parent == nullptr)
return nullptr;
while (node)
{
if (node->parent->lChild == node)
break;
node = node->parent;
}
return node->parent;
}
}
找最小值
BinaryTree::pNode BinaryTree::SearchMinNode(pNode curNode){
if (curNode == nullptr)
return nullptr;
//一直找到左子树为空的节点,即为最小值
while (curNode->lChild != nullptr)
curNode = curNode->lChild;
return curNode;
}
找最大值
BinaryTree::pNode BinaryTree::SearchMaxNode(pNode curNode){
if (curNode == nullptr)
return nullptr;
//一直找到右子树为空的节点,即为最大值
while (curNode->rChild != nullptr)
curNode = curNode->rChild;
return curNode;
}
中序遍历
void BinaryTree::_visitMiddle(pNode root){
if (root != nullptr){
_visitMiddle(root->lChild);
printf("%d;", root->data);
_visitMiddle(root->rChild);
}
}
void BinaryTree::VisitMiddle(){
_visitMiddle(root);
}
删除节点
这个是最麻烦的操作,分四种情况分别处理,最麻烦的是被删节点左右子树都存在的情况,这时将被删节点内容换成其后继内容,删除其后继(递归)。
bool BinaryTree::DeleteNode(int key){
//return _deleteNode(root, key);
pNode node = SearchKey(key);
if (!node)
return false;
//被删节点为叶子结点
if (node->lChild == nullptr && node->rChild == nullptr){
if (node->parent == nullptr){
root = nullptr;
}
else
{
if (node->parent->lChild == node)
node->parent->lChild = nullptr;
else
node->parent->rChild = nullptr;
}
delete node;
}
//被删节点只有左子树
else if (node->lChild != nullptr && node->rChild == nullptr){
//将左孩子的父亲指向被删节点的父亲
node->lChild->parent = node->parent;
//被删节点为根,修改根节点
if (node->parent == nullptr)
root = node->lChild;
else if(node->parent->lChild == node)
node->parent->lChild = node->lChild;
else
node->parent->rChild = node->lChild;
delete node;
}
//被删节点只有右子树
else if (node->lChild == nullptr && node->rChild != nullptr){
//将右孩子的父亲指向被删节点的父亲
node->rChild->parent = node->parent;
//被删节点为根,修改根节点
if (node->parent == nullptr)
root = node->rChild;
else if (node->parent->lChild == node)
node->parent->lChild = node->rChild;
else
node->parent->rChild = node->rChild;
delete node;
}
//被删节点左、右子树都有
else { //用后继节点取代删除节点,并删除后继
pNode successor = SearchSuccessor(node);
int temp = successor->data;
DeleteNode(temp);
node->data = temp;
}
}
柝构函数
函数超出作用域范围时,清理占用内存。
BinaryTree::~BinaryTree()
{
_delAllNode(root);
}
void BinaryTree::_delAllNode(pNode root){
if (root != nullptr && root!=NULL){
_delAllNode(root->lChild);
_delAllNode(root->rChild);
DeleteNode(root->data);
}
}
类的定义(头文件)
#pragma once #include<stdio.h>
#include<stdlib.h> class BinaryTree
{
private:
typedef struct Node{
struct Node * parent;
struct Node * lChild;
struct Node * rChild;
int data;
}*pNode;
pNode root;
void _visitMiddle(pNode root);
pNode _searchKey(pNode root, int key);
void _delAllNode(pNode root);
public:
BinaryTree();
BinaryTree(int * datum, int len);
pNode SearchMaxNode(pNode node);
pNode SearchMinNode(pNode node);
pNode GetRoot();
pNode SearchKey(int key);
bool DeleteNode(int key);
pNode SearchPredecessor(pNode node);
pNode SearchSuccessor(pNode node);
void VisitMiddle();
bool InsertNode(pNode * cuRoot, int data, pNode self);
~BinaryTree();
};
调用示例
#include <conio.h>
#include "BinaryTree.h" int main()
{
int arrs[] = { , , , , , , , , , ,, };
int len = sizeof(arrs) / sizeof(arrs[]);
BinaryTree bTree(arrs,len);
bTree.DeleteNode();
bTree.VisitMiddle();
getch();
return ;
}
二叉排序树实现(C++封装)的更多相关文章
- POJ 2418 各种二叉排序树
题意很明确,统计各个字符串所占总串数的百分比,暴力的话肯定超时,看了书上的题解后发现这题主要是用二叉排序树来做,下面附上n种树的代码. 简单的二叉排序树,不作任何优化(C语言版的): #include ...
- 算法与数据结构(十) 二叉排序树的查找、插入与删除(Swift版)
在上一篇博客中,我们主要介绍了四种查找的方法,包括顺序查找.折半查找.插入查找以及Fibonacci查找.上面这几种查找方式都是基于线性表的查找方式,今天博客中我们来介绍一下基于二叉树结构的查找,也就 ...
- [C#] 简单的 Helper 封装 -- RegularExpressionHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- iOS开发之App间账号共享与SDK封装
上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...
- Ajax实现原理,代码封装
都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...
- 用C语言封装OC对象(耐心阅读,非常重要)
用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...
- 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~
一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...
- 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...
- 封装集合(Encapsulate Collection)
封装就是将相关的方法或者属性抽象成为一个对象. 封装的意义: 对外隐藏内部实现,接口不变,内部实现自由修改. 只返回需要的数据和方法. 提供一种方式防止数据被修改. 更好的代码复用. 当一个类的属性类 ...
随机推荐
- selenium测试 - open Firefox
环境:Python2.7+selenium3+Firefox47 问题1: 在打开火狐浏览器时报错:‘geckodriver‘ executable needs to be in PATH fro ...
- Linux环境编译动态库和静态库总结
对Linux环境动态库和静态库的一些基础知识做一些总结, 首先总结静态库的编译步骤. 1 先基于.cpp或者.c文件生成对应的.o文件 2将几个.o文件 使用ar -cr命令 生成libname.a文 ...
- Codeforces 864E dp
题意: 房间着火了,里面有n件物品,每件物品有营救需要的时间t,被烧坏的最晚时间d,他的价值p,问能得到的最大价值,并且输出营救出来的物品编号 代码: //必然是先救存活时间短的即d小的,所以先排个序 ...
- mysql 给用户赋值权限
解决办法 grant all privileges on *.* to joe@localhost identified by '1'; flush privileges; 拿 joe 1 登陆 附: ...
- python使用pwd和grp操作unix用户及用户组
1.pwd模块 pwd模块提供了一个unix密码数据库即/etc/passwd的操作接口,这个数据库包含本地机器用户帐户信息 常用操作如下: pwd.getpwuid(uid):返回对应uid的示例信 ...
- Vue.js随笔一(Webpack + Vue.js开发准备,含VNM、NPM、Node、Webpack等相关工具)
想入门工具是必须的,这一章将向大家带来vue.js相关的程序安装步骤. ①首先你需要有一个NVM(一个非常好用的Node版本管理器): 1.NVM下载地址:https://github.com/cor ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
- CentOS 怎么设置某个目录包括子目录的写入权限 777呢
chmod -R 777 文件夹例如:chmod -R 777 /var var的权限就变成777,var下的所有子目录和文件权限都将变成777
- 【BZOJ】3572: [Hnoi2014]世界树 虚树+倍增
[题意]给定n个点的树,m次询问,每次给定ki个特殊点,一个点会被最近的特殊点控制,询问每个特殊点控制多少点.n,m,Σki<=300000. [算法]虚树+倍增 [题解]★参考:thy_asd ...
- Java八种基本类型
boolean 二进制位: true ,false byte 二进制位:8 -128 - 127 -2的7次方到2的7次方-1 char 二进制位:16 0 - 65535 short 二 ...