二叉搜索树的基本实现。

 /*
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. Linux系统lvm管理

    pv:   物理卷,被pv命令处理过的物理分区vg:物理卷组        被组装到一起的物理卷pe: 物理扩展        lvm设备的最小存储单元    lvm是pe的整数倍lvm:逻辑卷    ...

  2. 在开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误的解决办法

    问题描述:在开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误,程序调试运行,发现程序在打开数据库时候报错,也就是Connection.Open()处. 但是 ...

  3. vue入门----------scss的配置使用

    1.安装相应的依赖 cnpm install sass-loader --save-dev cnpm install node-sass --save-dev 2.在build文件下的webpack. ...

  4. 查看linux系统各种参数配置的命令

    查看linux系统各种参数配置的命令 last |grep shutdown                             //查看上次关机时间 last |grep reboot     ...

  5. The Falling Leaves UVA - 699

    题目链接:https://vjudge.net/problem/UVA-699 题目大意:给一颗二叉树,每个结点都有一个水平位置 :左子节点在它左边的1个单位,右子结点在它右边1个单位.从左向右输出每 ...

  6. MATLAB循环和函数定义,调用

    格式不要括号,最后有end for 循环变量 = 表达式1:表 2:表 3 表1:初值     表2:步长      表3:终值 求圆周率:π/4=1 - 1/3 + 1/5 -1/7+...+(-1 ...

  7. webkit技术--网页渲染原理

    Webkit渲染 Webkit 是苹果发起的一个开源项目,后来谷歌用这个项目以 webkit 创建了一个新的项目 Chromium,我们平常用的 Chrome 浏览器一般都是基于 Chromium 开 ...

  8. 通过进入单用户模式解决linux中的rc.local修改后无法启动的问题

    问题:本想将teamviewer这个软件随linux自启动,所以将其启动命令放在rc.local中,但是重启后发现linux启动不起来了,系统前面都是正常启动的,就是无法进入帐户登陆界面,无法输入ro ...

  9. pat05-图2. Saving James Bond - Easy Version (25)

    05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...

  10. oracle简单命令

    1.cmd 中 sqlplus /nolog 2.SQL> conn sys/password as sysdba