二叉搜索树封装代码

#pragma once
#include <iostream>
using namespace std;
template<class T>class TreeNode {
public:
TreeNode(T value)
{
this->val = value;
this->right = NULL;
this->left = NULL;
}
TreeNode() { }
T val;
TreeNode *left;
TreeNode *right;
}; template<class T>class BST {
public:
BST();
void Insert(T value); //插入元素接口
void InOrder(); //打印中序序列接口
int GetSize(); //返回当前二叉树总元素个数
int GetMaxValue(); //返回当前二叉树的最大值
int GetMinValue(); //返回当前二叉树的最小值
void Delete(T value); //删除该元素
private:
TreeNode<T> *insert(TreeNode<T> *root, T value ); //内部插入实现
TreeNode<T> *deleteval(TreeNode<T> *node, T val); //删除某个元素
TreeNode<T> *FindMin(TreeNode<T> *node); //找到最小的元素的结点
TreeNode<T> *FindMax(TreeNode<T> *node); //找到最大的元素的结点
T getmaxvalue(TreeNode<T> *node);
T getminvalue(TreeNode<T> *node);
void inorder(TreeNode<T> *node); //内部中序遍历
TreeNode<T> *Tree;
int size;
};
template<class T>
inline BST<T>::BST()
{
this->size = 0;
this->Tree = NULL;
} //往BST树中插入数据
template<class T>
inline void BST<T>::Insert(T value)
{
this->Tree = insert(Tree, value);
} template<class T>
inline TreeNode<T> * BST<T>::insert(TreeNode<T> *node, T value)
{
if (node == NULL)
{
size++;
return new TreeNode<T>(value);
}
else {
if (value < node->val)
{
node->left = insert(node->left, value);
return node;
}
else if (value > node->val)
{
node->right = insert(node->right, value);
return node;
}
}
} //获取当前二叉树的所有非空结点
template<class T>
inline int BST<T>::GetSize()
{
return this->size;
} template<class T>
inline int BST<T>::GetMaxValue()
{
return this->getmaxvalue(Tree);
} template<class T>
inline int BST<T>::GetMinValue()
{
return this->getminvalue(Tree);
} template<class T>
inline void BST<T>::Delete(T value)
{
Tree = this->deleteval(Tree, value);
this->size--;
} //中序遍历
template<class T>
inline void BST<T>::InOrder()
{
inorder(Tree);
} template<class T>
inline void BST<T>::inorder(TreeNode<T> *node)
{
if (node != NULL)
{
inorder(node->left);
cout << node->val << " ";
inorder(node->right);
}
} template<class T>
inline TreeNode<T> * BST<T>::deleteval(TreeNode<T>* node, T val)
{
if (node == NULL)cout << "未找到此元素" << endl;
TreeNode<T> *tmp = new TreeNode<T>;
if (val < node->val)
node->left = deleteval(node->left, val);
else if(val > node->val)
node->right = deleteval(node->right, val);
else {
if (node->left != NULL && node->right != NULL) //存在左右子树
{
tmp = this->FindMin(node->right);
node->val = tmp->val;
node->right = deleteval(node->right, node->val);
}
else { //叶子结点或者只有一个子节点
tmp = node;
if (!node->left)
node = node->right;
else if (!node->right)
node = node->left;
free(tmp);
}
}
return node;
} template<class T>
inline TreeNode<T>* BST<T>::FindMin(TreeNode<T>* node)
{
if (node->left == NULL)return node;
else return FindMin(node->left);
} template<class T>
inline TreeNode<T>* BST<T>::FindMax(TreeNode<T>* node)
{
if (node->right == NULL)return node;
else return FindMax(node->right);
} template<class T>
inline T BST<T>::getmaxvalue(TreeNode<T> *node)
{
//如果当前结点的左子树为空树,则当前结点就是最大值
if (node->right == NULL)
return node->val;
else return getmaxvalue(node->right);
} template<class T>
inline T BST<T>::getminvalue(TreeNode<T> *node)
{
if (node->left == NULL)
return node->val;
else return getminvalue(node->left);
}

测试代码

#include <istream>
#include "BST.h"
using namespace std; int main()
{
BST<int> bt;
int arr[] = { 9,5,3,4,1,7,2,8,0 };
int n = sizeof(arr) / sizeof(int);
for (int i = 0; i < n; i++)
bt.Insert(arr[i]);
bt.InOrder();
cout << endl << bt.GetSize() << endl;
cout << "这组元素中的最大值为:" <<bt.GetMaxValue() << endl;
cout << "这组元素中的最小值为:" <<bt.GetMinValue() << endl;
cout << "输入你想删除任意一个元素" << endl;
int x = 0;
cin >> x;
bt.Delete(x);
bt.InOrder();
cout << "此时该集合内的元素个数为:" << bt.GetSize() << endl;
system("pause");
return 0;
}

数据结构 - 二叉搜索树封装 C++的更多相关文章

  1. 数据结构-二叉搜索树(BST binary search tree)

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来 ...

  2. 数据结构-二叉搜索树的js实现

    一.树的相关概念 1.基本概念 子树 一个子树由一个节点和它的后代构成. 节点的度 节点所拥有的子树的个数. 树的度 树中各节点度的最大值 节点的深度 节点的深度等于祖先节点的数量 树的高度 树的高度 ...

  3. 数据结构☞二叉搜索树BST

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它可以是一棵空树,也可以是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它 ...

  4. 基本数据结构 —— 二叉搜索树(C++实现)

    目录 什么是二叉搜索树 二叉搜索树如何储存数值 二叉搜索树的操作 插入一个数值 查询是否包含某个数值 删除某个数值 测试代码 参考资料 什么是二叉搜索树 二叉搜索树(英语:Binary Search ...

  5. 数据结构---二叉搜索树BST实现

    1. 二叉查找树 二叉查找树(Binary Search Tree),也称为二叉搜索树.有序二叉树(ordered binary tree)或排序二叉树(sorted binary tree),是指一 ...

  6. 数据结构-二叉搜索树和二叉树排序算法(python实现)

    今天我们要介绍的是一种特殊的二叉树--二叉搜索树,同时我们也会讲到一种排序算法--二叉树排序算法.这两者之间有什么联系呢,我们一起来看一下吧. 开始之前呢,我们先来介绍一下如何创建一颗二叉搜索树. 假 ...

  7. Java数据结构——二叉搜索树

    定义二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若 ...

  8. 数据结构——二叉搜索树(Binary Search Tree)

    二叉树(Binary Tree)的基础下 每个父节点下 左节点小,右节点大. 节点的插入: 若root==NULL则root=newnode 否则不断与节点值比较,较小则向左比较,较大则向右比较. 完 ...

  9. 数据结构-二叉搜索树Java实现

    1,Node.java 生成基础二叉树的结构 package com.cnblogs.mufasa.searchTree; /** * 节点配置父+左+右 */ public class Node{ ...

随机推荐

  1. MySQL实战45讲学习笔记:第二十九讲

    一.引子 我在第25和27篇文章中,和你介绍了主备切换流程.通过这些内容的讲解,你应该已经很清楚了:在一主一备的双 M 架构里,主备切换只需要把客户端流量切到备库:而在一主多从架构里,主备切换除了要把 ...

  2. convert decimal to binary

    public class Solution { public static void main(String[] args) { ; String str = ""; ) { ; ...

  3. Oracle 的 连接

    select a.alert_id, b.alert_id from a inner join b on a.alert_id = b.alert_id ; 上面和下面是一样的,都是 内连接 sele ...

  4. SpringBoot2配置prometheus浏览器访问404

    背景:SpringBoot2的项目要配置 actuator + prometheus的健康检查,按照教程配置好之后再浏览器测试 http://localhost:port/prometheus 后40 ...

  5. Windows环境安装PyQt5

    目录 1. 安装Python 2. 安装Pycharm 3. 安装PyQt5 4. 安装PyQt5-tools 5. 可能出现的问题 1. Qt Designer 程序位置 2. Qt Designe ...

  6. Ubuntu16 安装 wireshark

    添加源 sudo apt-add-repository ppa:wireshark-dev/stable 更新 sudo apt-get update 安装 sudo apt-get install ...

  7. [转帖]k8s 中的服务如何沟通

    k8s 中的服务如何沟通 https://www.jianshu.com/p/9fae09876eb7 本文将介绍 k8s 中的服务如何相互访问,例如后端服务访问数据库,不同类型的服务间的相互访问.并 ...

  8. javascirpt的json.stringify()方法在IE浏览器兼容性模式下出错的原因与解决办法

    今天开机混底薪的时候遇到一个JSON.stringify()在IE浏览器兼容模式下的问题. 问题描述 一个弹窗选择的功能原来好好的,突然就不行了. 想要调试调试不了,报错信息也看不到(一开F12这破I ...

  9. 关于 Task.Run 简单的示例

    1. 关于 Task.Run 简单的示例01 直接贴代码了: public static class TaskDemo01 { public static void Run() { Console.W ...

  10. js计算两经纬度之间的距离

    js如下: // 方法定义 lat,lng function GetDistance( lat1, lng1, lat2, lng2){    var radLat1 = lat1*Math.PI / ...