二叉搜索树的基本实现。

 /*
Date: 2014-04-29
purpose: An implementation of MAP using binary search tree.
*/ #ifndef CUSTOMIZED_MAP_H
#define CUSTOMIZED_MAP_H #ifndef NULL
#define NULL 0
#endif class NODE{
public:
NODE(int _key, char * _data):key(_key),data(_data),left(NULL),right(NULL){} public:
int key;
char * data;
NODE * left;
NODE * right;
}; class CUST_MAP{
public:
CUST_MAP():root(NULL){}; // how about construction failed?
~CUST_MAP();
void destructorImp(NODE * root);
bool appendItem(int key, char * data);
bool removeItem(int key);
bool traverse();
bool traverseImp(NODE * root); public:
NODE * root;
}; #endif

实现文件

 #include <iostream>
#include <vector>
#include "Customized_MAP.h" using std::cout;
using std::endl;
using std::string; bool CUST_MAP::appendItem(int key, char * data){
enum Direction{LEFT, RIGHT}; //append which direction of the pended item
Direction direction = LEFT; //routine check.
NODE * root = this->root;
if (root == NULL){
this->root = new NODE(key, data);
if (this->root)
return true;
else return false;
} NODE * image = root;//record the history of root // find inserting point
while (root){
image = root;
if (root->key > key){
root = root->left;
direction = LEFT;
}
else if (root->key < key){
root = root->right;
direction = RIGHT;
}
else return false; // duplicated key==> abort appending operation
} // append the fresh node
NODE * newNode = new NODE(key, data);
if (direction == LEFT){
image->left = newNode;
}else if (direction == RIGHT){
image->right = newNode;
} return true;
} bool CUST_MAP::removeItem(int key){
enum Direction{LEFT, RIGHT}; //append which direction of the new item
Direction direction = LEFT; NODE * root = this->root;
if (root == NULL) // no node deleted. return false
return false; NODE * parent = NULL; while(root){
if (root->key > key){
parent = root;
root = root->left;
direction = LEFT;
}
else if (root->key < key){
parent = root;
root = root->right;
direction = RIGHT;
}
else if (root->key == key){
if (root->left && root->right){//要删除的节点有两个孩子节点
if (root->right->left == NULL){
//要删除节点的右孩子结点没有左孩子节点
//1:copy data, root->right ===> root.
//2: record pointer
//3: delete root->right
//4: finish pointer association
//how to delete root->data;
root->data = root->right->data;
root->key = root->right->key;
NODE * right = root->right->right; delete root->right; root->right = right;
}else{
//要删除节点的右孩子结点有左孩子节点
//root: 要删除的节点
//找到root右孩子的 最左方没左孩子的节点: tParent
NODE * t = root->right;
NODE * tParent = NULL;
while (t->left){
tParent = t;
t = t->left;
}
root->key = t->key;
root->data = t->data;
NODE * right = t->right; delete t;
t = NULL; tParent->left = right;
} }else if (root->left){//要删除的节点只有左孩子节点
if (direction == LEFT)
parent->left = root->left;
else if(direction == RIGHT)
parent->right = root->left; delete root;
root = NULL;
}else if(root->right){//要删除的节点只有右孩子节点
if (direction == LEFT)
parent->left = root->right;
else if(direction == RIGHT)
parent->right = root->right; delete root;
root = NULL;
}else if (root->left==NULL && root->right==NULL){//要删除的节点没有孩子节点
if (direction == LEFT)
parent->left = NULL;
else if(direction == RIGHT)
parent->right = NULL; delete root;
root = NULL;
} return true;
}
} return false;
} void CUST_MAP::destructorImp(NODE * root){
if (root){
destructorImp(root->left);
destructorImp(root->right); delete root;
root = NULL;
}
} CUST_MAP::~CUST_MAP(){
destructorImp(root);
} bool CUST_MAP::traverseImp(NODE * root){
if (root){
traverseImp(root->left);
cout<<root->data<<endl;
traverseImp(root->right);
} return true;
} bool CUST_MAP::traverse(){
traverseImp(root); return true;
} int main(){
CUST_MAP mapTest;
mapTest.appendItem(, "1 one");
mapTest.appendItem(, "28 twenty-eight");
mapTest.appendItem(, "16 sixteen");
mapTest.appendItem(, "12 twelf");
mapTest.appendItem(, "24 twety-four");
mapTest.appendItem(, "56 fifty-six");
mapTest.appendItem(, "36 thirty-six");
mapTest.appendItem(, "66 sixty-six");
mapTest.appendItem(, "46 fourty-six"); cout<<"New:"<<endl;
mapTest.traverse();
cout<<endl<<endl; mapTest.removeItem();
cout<<"New:"<<endl;
mapTest.traverse(); cout<<"over"<<endl; return ;
}

来幅图片

done.

二叉搜索树实现MAP的更多相关文章

  1. 使用二叉搜索树实现一个简单的Map

    之前看了刘新宇大大的<算法新解>有了点收获,闲来无事,便写了一个二叉搜索树实现的Map类. java的Map接口有很多不想要的方法,自己定义了一个 public interface IMa ...

  2. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  3. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  4. 二叉搜索树的两种实现(数组模拟,STL)

    书上实现: 二叉搜索数的特点:高效实现 插入一个数值,查询是否包含某个数值,删除某一个数值. 所有的节点都满足左子树上的所有节点都比自己的小,而右子树上的所有节点都比自己大的特点. 查询:如果当前数值 ...

  5. [Swift]LeetCode98. 验证二叉搜索树 | Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. (PAT)L2-004 这是二叉搜索树吗?(数据结构)

    题目链接:https://www.patest.cn/contests/gplt/L2-004 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的 ...

  7. PAT L3-016 二叉搜索树的结构

    https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...

  8. [数据结构]P2.1 二叉搜索树

    二叉树就是每个节点最多有两个分叉的树.这里我们写一写一个典型的例子二叉搜索树,它存在的实际意义是什么呢? 在P1.1链表中,我们清楚了链表的优势是善于删除添加节点,但是其取值很慢:数组的优势是善于取值 ...

  9. 自己动手实现java数据结构(六)二叉搜索树

    1.二叉搜索树介绍 前面我们已经介绍过了向量和链表.有序向量可以以二分查找的方式高效的查找特定元素,而缺点是插入删除的效率较低(需要整体移动内部元素):链表的优点在于插入,删除元素时效率较高,但由于不 ...

随机推荐

  1. nginx编译报错

    [root@vm_0_2_centos nginx-1.12.2]# ./configure --prefix=/application/nginx-1.12.2 --user=www --group ...

  2. Linux 系统排错

    ##root用户密码的修改 重启时按上下键打断引导,按“e”进入内核引导 在linux16行删除至途中所示位置,并进行修改: 修改完毕后按“ctrl+x”启动 进入系统切换到真实环境,开始修改root ...

  3. 关于web安全需要在编程时注意的

    公司用绿盟科技的远程安全评估系统扫描了项目,发现一些安全隐患,记录下来,以规避以后编程或者发布时犯同样的错误. 1. 目标web应用表单存在口令猜测攻击 风险:登录密码易被暴力破解,暴力破解是一种常见 ...

  4. Linux进程控制理论及几种常见进程间通信机制

    1. Linux进程控制理论 ① 进程是一个具有一定独立功能的程序的一次运行活动(动态性.并发性.独立性.异步性). 进程的四要素: (1)有一段程序供其执行(不一定是一个进程所专有的),就像一场戏必 ...

  5. 1 Groovy

    1.1  什么是Groovy? groovy 是一个弱类型,动态语言,并且运行在JVM之上.它与java联系紧密.它是一个功能丰富和友好的java语言. Groovy源代码,通过Groovy编译器编译 ...

  6. 内置函数_eval

    eval功能:将字符串str当成有效的表达式来求值并返回计算结果. 语法: eval(source[, globals[, locals]]) -> value 参数说明: expression ...

  7. 汉诺塔问题hdu 2065——找规律

    这类题目就是纸上模拟,找规律. 问题描述:在一块铜板上有三根杆,目的是将最左边杆上的盘全部移到右边的杆上,条件是不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允 ...

  8. appium手机自动化环境搭建

    在robotframework环境安装完成的基础上进行如下安装,如果没有安装rfs环境,请先参考robotframework安装文章:Robot Framework的环境搭建 文件下载地址:链接:ht ...

  9. 使用media query 来实现响应式设计

    你的网页在手机上显示效果可以在电脑上一样好看.完成这个任务的奥秘被称为响应式设计,媒体查询(media query)是实现网页响应的关键. 在电脑上一个例子: <div class=" ...

  10. linux C判断文件是否存在

    access函数 功能描述: 检查调用进程是否可以对指定的文件执行某种操作.   用法: #include <unistd.h> #include <fcntl.h> int ...