二叉搜索树实现MAP
二叉搜索树的基本实现。
/*
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的更多相关文章
- 使用二叉搜索树实现一个简单的Map
之前看了刘新宇大大的<算法新解>有了点收获,闲来无事,便写了一个二叉搜索树实现的Map类. java的Map接口有很多不想要的方法,自己定义了一个 public interface IMa ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- 二叉搜索树的两种实现(数组模拟,STL)
书上实现: 二叉搜索数的特点:高效实现 插入一个数值,查询是否包含某个数值,删除某一个数值. 所有的节点都满足左子树上的所有节点都比自己的小,而右子树上的所有节点都比自己大的特点. 查询:如果当前数值 ...
- [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 ...
- (PAT)L2-004 这是二叉搜索树吗?(数据结构)
题目链接:https://www.patest.cn/contests/gplt/L2-004 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的 ...
- PAT L3-016 二叉搜索树的结构
https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...
- [数据结构]P2.1 二叉搜索树
二叉树就是每个节点最多有两个分叉的树.这里我们写一写一个典型的例子二叉搜索树,它存在的实际意义是什么呢? 在P1.1链表中,我们清楚了链表的优势是善于删除添加节点,但是其取值很慢:数组的优势是善于取值 ...
- 自己动手实现java数据结构(六)二叉搜索树
1.二叉搜索树介绍 前面我们已经介绍过了向量和链表.有序向量可以以二分查找的方式高效的查找特定元素,而缺点是插入删除的效率较低(需要整体移动内部元素):链表的优点在于插入,删除元素时效率较高,但由于不 ...
随机推荐
- SQL Connect By 的例子
看到一个较为通俗易懂的connect by的例子,是百度知道的答案,稍微整理了一下.我自己这样理解:connect by prior "id" = "p_id" ...
- sql的几种常用锁简述
比较全的文章地址保存下:http://www.cnblogs.com/knowledgesea/p/3714417.html SELECT * FROM dbo.AASELECT * FROM dbo ...
- UML箭头
继承(泛化):用实线空心三角箭头表示 实现(接口):用虚线空心三角形箭头标示 依赖:虚线箭头,类A指向类B 方法参数需要传入另一个类的对象,就表示依赖这个类 关联:实线箭头,类A指向类B 一个类的全局 ...
- 转 Python 多进程multiprocessing.Process之satrt()和join()
1. https://blog.csdn.net/wonengguwozai/article/details/80325745 今天项目中涉及到了使用多进程处理数据,在廖雪峰的python教程上学习了 ...
- 加解密---Base64
1.算法实现: 1.1 JDK提供: package com.exiuge.mytest; import sun.misc.BASE64Decoder; import sun.misc.BASE64E ...
- javascrip基础学习
JS是一种解释性脚本语言,在网页开发用经常用到(HTML CSS),用于控制网页的行为.现在RTT的柿饼UI也是用JS来开发的,所以很有必要学习一下. 注释:// ./* */ 语句分行: 折行\ ...
- Boxes in a Line UVA - 12657 (双向链表)
题目链接:https://vjudge.net/problem/UVA-12657 题目大意:输入n,m 代表有n个盒子 每个盒子最开始按1~n排成一行 m个操作, 1 x y :把盒子x放到y ...
- leetcode 196. Delete Duplicate Emails 配合查询的delete
https://leetcode.com/problems/delete-duplicate-emails/description/ 题意要对原来的数据表进行删除,不删除不行,它每次只输出原来那个表. ...
- 深入学习webpack(三)
在前面两篇博客中,主要讲了webpack的使用和webpack的核心概念,他们都是非常重要的,在这篇博客中,讲主要讨论webpack配置相关问题. 参考文章:https://webpack.js.or ...
- OAuth2.0和企业内部统一登录,token验证方式,OAuth2.0的 Authorization code grant 和 Implicit grant区别
统一登录是个很多应用系统都要考虑的问题,多个项目的话最好前期进行统一设计,否则后面改造兼容很麻烦: cas认证的方式:新公司都是老项目,用的是cas认证的方式,比较重而且依赖较多,winform的项目 ...