二叉搜索树(递归和非递归分别实现)
来源:互联网 发布:武林外传 知乎精华 编辑:程序博客网 时间:2024/05/18 00:29
这是在上一篇博客的基础上进行完善。
1.插入的递归
bool InsertR(const T& key){return _InsertR(_root,key);}
bool _InsertR(Node*& root,const T& key){if(root==NULL){root=new Node(key);return true;}if(root->_key>key){return _InsertR(root->_left ,key);}else if(root->_key <key){return _InsertR(root->_right ,key);}else //这个数存在root->_key==key{return false;}}
2.插入的非递归
bool Insert(const T& key){//1.空树//2.不存在//3.存在//空树if(_root==NULL){_root=new Node(key);return true;}Node* cur=_root;Node* prev=cur;while(cur){if(cur->_key >key){prev=cur;cur=cur->_left ;} else if(cur->_key <key){prev=cur;cur=cur->_right ;}else //if(cur->_key ==key)return false; //存在}//不存在if(prev->_key >key){cur=new Node(key); prev->_left =cur;}else{cur=new Node(key); prev->_right =cur;}return true;}
3.删除的非递归
bool Remove(const T& key){if(_root==NULL)return false;Node* cur=_root;Node* parent=NULL;while(cur){if(cur->_key <key){parent=cur;cur=cur->_right ;}else if(cur->_key >key){parent=cur;cur=cur->_left ;}else //找到key,再删除{if(cur->_left==NULL ) //左为空{if(_root==cur){_root=cur->_right ;}else if(parent->_left ==cur){parent->_left =cur->_right ;}else{parent->_right =cur->_right ;}delete cur;}else if(cur->_right ==NULL) //右为空{if(_root==cur){_root=cur->_left ;}else if(parent->_left ==cur){parent->_left =cur->_left ; }else{parent->_right =cur->_left ;}delete cur;}else //删除的节点左右都不为空{//找右子树的最左节点Node* righttree=cur->_right ;parent=cur;while(righttree->_left ){parent=righttree;righttree=righttree->_left ;}if(parent->_left ==righttree){parent->_left =righttree->_right ;}else{parent->_right =righttree->_right ;}}return true;}}return false;}
bool RemoveR(const T& key){ return _RemoveR(_root,key);}
bool _RemoveR(Node*& root,const T& key)//&这个是重点{if(root==NULL)return false;if(root->_key <key)return _RemoveR(root->_right ,key);else if(root->_key >key) return _RemoveR(root->_left ,key);else //找到,再删除{Node* del=root;if(root->_left ==NULL){root=root->_right ;}else{root=root->_left ;}delete del;}}5.查找的非递归
bool Find(const T& key){Node* cur=_root;while(cur){if(cur->_key <key){cur=cur->_right ;}else if(cur->_key >key){cur=cur->_left ;}else if(cur->_key ==key)return true;}return false;}
6.查找的递归
bool FindR(const T& key){_FindR(_root,key);}
bool _FindR(Node* root,const T& key){if(root==NULL)return false;if(root->_key >key) return _FindR(root->_left ,key);else if(root->_key <key)return _FindR(root->_right ,key);elsereturn true;}
7.中序遍历
void InOrderR(){_InOrderR(_root);cout<<endl;}
void _InOrderR(Node* root){if(root==NULL)return; _InOrderR(root->_left ); cout<<root->_key<<" " ; _InOrderR(root->_right );}
0 0
- 二叉搜索树(递归和非递归分别实现)
- 查找二叉树的实现(查找,删除,插入)分别用递归和非递归
- C++实现二叉搜索树(递归&非递归)
- 递归和非递归分别实现strlen
- 递归和非递归分别实现strlen
- 非递归实现搜索二叉树
- 二叉搜索树的非递归实现
- 二叉树的遍历(非递归和递归实现)
- 二叉树遍历(递归和非递归实现)
- 二叉树及其遍历(递归和非递归实现)
- 二叉搜索树递归&&非递归的基本实现
- 【数据结构】二叉搜索树的递归与非递归实现
- 二叉搜索树的递归遍历和非递归遍历
- 分别用递归和非递归方式实现二叉树先序、中序和后序遍历(java实现)
- 二叉搜索树---递归及非递归
- 二叉搜索树(非递归)
- 二叉搜索树-非递归
- 二叉树深度搜索-递归和非递归的实现(转载)
- 和云计算、虚拟化相关的几个概念的粗浅理解
- 1002. 写出这个数 (20)——stack
- 少走弯路的十条忠告
- Android Socket编程的基类编写
- javascript定义类的三种方法
- 二叉搜索树(递归和非递归分别实现)
- CentOS 7拨号上网(ADSL & PPPoE)
- C# 简单的 get set
- 求最大最小及平均值
- 打印实心菱形
- Intent返回数据给上一个活动
- 基于Linux(或者Mac OS X)的对拍程序
- Tomcat容器启动时,报java.io.EOFException异常
- Junit学习笔记之二:hamcrest简介和TestSuite