二叉搜索树的基本实现。

 /*
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.

最新文章

  1. 大数据实践-数据同步篇tungsten-relicator(mysql-&gt;mongo)
  2. Ado.net中简单的DBHelper类(增删改查)
  3. 收到的电邮附件为Winmail.dat?
  4. 用Visual Studio 2012+Xamarin搭建C#开发Andriod的环境
  5. 解析八大O2O典范:他们都做了什么?
  6. 移动端1px细线的处理
  7. Android 多种方式正确的加载图像,有效避免oom
  8. Android Studio常用插件
  9. 利用flashBack恢复误删除(delete)的表数据
  10. sqlserver修改增删改字段
  11. checkpoint NGFW VM安装
  12. EntityFramework中对象的状态管理(笔记)
  13. Codeforces899D Shovel Sale(思路)
  14. oracle服务端与客户端字符集不同导致中文乱码解决方案
  15. TOJ1698/POJ3264Balanced Lineup (线段树 or RMQ-ST)
  16. Android Socket
  17. JDK7新特性&lt;八&gt;异步io/AIO
  18. asiHttpRequst 学习地址
  19. 【转】ExtJS获取父子、兄弟容器元素方法
  20. SQL SERVER数据库新认识的一些基础知识

热门文章

  1. 查询多列得到map与查询得到po对象
  2. spring boot CrossOrigin不生效?
  3. http 协议的简单学习 虽然有点老但是 还不错
  4. c++中代理类的学习
  5. AUTO Uninstaller 下载 (maya/3dsmax/cad/Inventor/Revit uninstall tool 卸载修复工具)
  6. LeetCode 110.平衡二叉树(C++)
  7. css实现背景透明文字不透明
  8. httpd编译安装php
  9. Oracle错误——ORA-03113:通信通道的文件结尾
  10. BZOJ3261: 最大异或和(可持久化trie树)