二叉搜索树 C++代码实现
暂未发现什么bug,如果发现请指出。
- #include<iostream>
- using namespace std;
- //定义二叉搜索树的结点
- struct Node
- {
- int data;
- Node *lc,*rc,*parent;
- };
- //中序遍历二叉搜索树
- void show(Node *now)
- {
- if(now==NULL) return;
- show(now->lc);
- cout<<now->data<<endl;
- show(now->rc);
- }
- //将值为val的结点插入到二叉搜索树中
- //?为什么要传指向根结点指针的指针,而不是直接传根结点的指针。因为根结点的指针可能为空,这里需要开辟内存。
- Node* insert(Node **rt,int val)
- {
- Node *now=*rt;//用now指针在树上移动
- Node *par=NULL;//记录now指针的父亲
- while(now!=NULL)
- {
- par=now;
- if(val<now->data)//如果val小于当前结点的值说明应该插到左子树中
- now=now->lc;
- else
- now=now->rc; //否则插入到右子树中
- }
- Node *newnode=new Node;//开辟新结点
- newnode->data=val;
- newnode->lc=newnode->rc=NULL;
- if(par==NULL)//当这是一棵空树的时候
- {
- newnode->parent=NULL;
- *rt=newnode;//直接通过指针修改根结点
- }
- else
- {
- if(val<par->data)
- par->lc=newnode;
- else
- par->rc=newnode;
- newnode->parent=par;
- }
- return newnode;
- }
- //在以now为根结点的树上,寻找值为val的结点
- Node* search(Node *now,int val)
- {
- while(now!=NULL&&now->data!=val)
- {
- if(val<now->data)
- now=now->lc;
- else
- now=now->rc;
- }
- return now;
- }
- //在以now为根结点的树上,寻找最大、最小值
- Node* maximun(Node *now)
- {
- while(now->rc!=NULL)
- {
- now=now->rc;
- }
- return now;
- }
- Node* minimun(Node *now)
- {
- while(now->lc!=NULL)
- now=now->lc;
- return now;
- }
- //寻找now结点的前继、后继
- //寻找前继:如果now的左子树非空,则返回左子树的最大值结点。否则如果now是父亲的左孩子则不断向上直到now不再是父亲的左孩子,返回父结点。
- Node* predecessor(Node *now)
- {
- if(now->lc!=NULL)
- return maximun(now->lc);
- Node *par=now->parent;
- while(par!=NULL&&now==par->lc)
- {
- now=par;
- par=par->parent;
- }
- return par;
- }
- Node* successor(Node *now)
- {
- if(now->rc!=NULL)
- return minimun(now->rc);
- Node *par=now->parent;
- while(par!=NULL&&now==par->rc)
- {
- now=par;
- par=par->parent;
- }
- return par;
- }
- //对于一棵以T为根结点的树,用以v为根结点的子树取代以u为根结点的子树,完成u的父亲与v之间的绑定
- void transplant(Node **T,Node *u,Node *v)
- {
- if(u->parent==NULL)
- (*T)=v;
- else if(u==u->parent->lc)
- u->parent->lc=v;
- else if(u==u->parent->rc)
- u->parent->rc=v;
- if(v!=NULL)
- v->parent=u->parent;
- }
- //删除给定结点
- void erase(Node **T,Node *now)
- {
- Node *temp=now;
- if(now->lc==NULL)
- {
- transplant(T,now,now->rc);
- }
- else if(now->rc==NULL)
- {
- transplant(T,now,now->lc);
- }
- else
- {
- Node* nextnode=successor(now);
- if(nextnode!=now->rc)
- {
- transplant(T,nextnode,nextnode->rc);
- nextnode->rc=now->rc;
- now->rc->parent=nextnode;
- }
- transplant(T,now,nextnode);
- nextnode->lc=now->lc;
- now->lc->parent=nextnode;
- }
- delete temp;
- }
- int main()
- {
- Node *root=NULL;
- )
- {
- cout<<"1.插入结点"<<endl;
- cout<<"2.删除结点"<<endl;
- cout<<"3.中序遍历"<<endl;
- cout<<"4.查询"<<endl;
- cout<<"5.查询根"<<endl;
- int p;
- cin>>p;
- )
- {
- cout<<"输入待插元素值:"<<endl;
- int t;
- cin>>t;
- insert(&root,t);
- cout<<"插入成功!"<<endl;
- }
- )
- {
- cout<<"输入待删元素值:"<<endl;
- int t;
- cin>>t;
- Node *q=search(root,t);
- if(q==NULL)cout<<"该值不存在!"<<endl;
- else
- {
- erase(&root,q);
- cout<<"删除成功!"<<endl;
- }
- }
- )
- {
- cout<<"======"<<endl;
- show(root);
- cout<<"======"<<endl;
- }
- )
- {
- cout<<"输入待查询元素值:"<<endl;
- int t;
- cin>>t;
- Node *q=search(root,t);
- if(q==NULL) cout<<"该值不存在!"<<endl;
- else
- {
- Node *a=predecessor(q),*b=successor(q);
- if(a!=NULL)
- cout<<"前继:"<<a->data<<endl;
- if(b!=NULL)
- cout<<"后继:"<<b->data<<endl;
- }
- }
- )
- {
- if(root==NULL) cout<<"根为空!"<<endl;
- else cout<<"根:"<<root->data<<endl;
- }
- }
- ;
- }
二叉搜索树 C++代码实现的更多相关文章
- python 二叉搜索树相关代码
class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None class Ope ...
- 基于visual Studio2013解决算法导论之029二叉搜索树
题目 二叉搜索树 解决代码及点评 #include <stdio.h> #include <malloc.h> #include <stdlib.h> ty ...
- 二叉搜索树(Binary Search Tree)--C语言描述(转)
图解二叉搜索树概念 二叉树呢,其实就是链表的一个二维形式,而二叉搜索树,就是一种特殊的二叉树,这种二叉树有个特点:对任意节点而言,左孩子(当然了,存在的话)的值总是小于本身,而右孩子(存在的话)的值总 ...
- 自己动手实现java数据结构(六)二叉搜索树
1.二叉搜索树介绍 前面我们已经介绍过了向量和链表.有序向量可以以二分查找的方式高效的查找特定元素,而缺点是插入删除的效率较低(需要整体移动内部元素):链表的优点在于插入,删除元素时效率较高,但由于不 ...
- 数据结构 - 二叉搜索树封装 C++
二叉搜索树封装代码 #pragma once #include <iostream> using namespace std; template<class T>class T ...
- 每日一题 - 剑指 Offer 33. 二叉搜索树的后序遍历序列
题目信息 时间: 2019-06-26 题目链接:Leetcode tag:分治算法 递归 难易程度:中等 题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果.如果是则返回 tr ...
- 编程算法 - 二叉搜索树 与 双向链表 代码(C++)
二叉搜索树 与 双向链表 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目:输入一颗二叉搜索树, 将该二叉搜索树转换成一个排序的双向链表. 要求 ...
- 编程算法 - 二叉搜索树(binary search tree) 代码(C)
二叉搜索树(binary search tree) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉搜索树(binary search tree)能 ...
- BinarySearchTree(二叉搜索树)原理及C++代码实现
BST是一类用途极广的数据结构.它有如下性质:设x是二叉搜索树内的一个结点.如果y是x左子树中的一个结点,那么y.key<=x.key.如果y是x右子树中的一个结点,那么y.key>=x. ...
随机推荐
- easyui DataGrid 工具类之 列属性class
public class ColumnVO { /** * 列标题文本 */ private String title; /** * 列字段名称 */ pr ...
- LeetCode算法题解
1.给定两个正整数(二进制形式表示)A和B,问把A变为B需要改变多少位(bit)?也就是说,整数A和B的二进制表示中有多少位是不同的?(181) 解法一:举例说明,为了减少复杂度,就使用八位二进制吧. ...
- Response.Clear()和Response.ClearContent()区别
Response.Clear()方法 Clear方法删除所有缓存中的HTML输出.但此方法只删除Response显示输入信息,不删除Response头信息. Response.ClearContent ...
- NSDate获取当前时区的时间
[NSDate date]获取的是GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题 NSDate *date = [NSDate date]; NSTimeZone *zone = [NS ...
- sublime 自动编译
Tools --> Build System --> New: { "shell_cmd": "cc.bat \"$file\"" ...
- Java类实例化时候的加载顺序
面试试题中经常考到此问题,现在做进一步的总结: public class Student { public Student(String name){ System.out.println(name) ...
- PyCharm配置GitHub
原文出处: https://github.com/wssnail/ws96apt/blob/master/weixin/a.py#L21-21打开file,选择settings,找到Version C ...
- linux 下安装安装rz/sz命令
一.软件安装 root 账号登陆后,依次执行以下命令: cd /tmp wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz http:/ ...
- Python之路 day1 用户登录多次被锁定
编写登陆接口: 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 #Author:ersa import getpass,os,sys #读取账户信息到内存中 try: accounts_fil ...
- angular router-ui
将模块注入到控制器中的方法: 1.export module 2.在router中resolve解决: 2.1 resolve中直接return值 /*ngInject*/ worker : 'hi' ...