二叉搜索树的实现和前序遍历
来源:互联网 发布:sql 链接查询结果 编辑:程序博客网 时间:2024/05/16 10:36
#include <algorithm> #include <iostream> #include <iterator> #include <sstream> #include <fstream> #include <istream> #include <ostream> #include <complex> #include <cstring> #include <utility> #include <cstdlib> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <ctime> #include <cmath> #include <queue> #include <stack> #include <list> #include <new> #include <set> #include <map> using namespace std; typedef pair<int, int> pii; typedef long long int LL; const int INF = 0x3f3f3f3f; const int maxn = 10005; struct Node{ int value; Node *lc, *rc; }; Node* Insert(Node *root, int x){//把x插入到搜索树中 if (!root){ Node *p = new Node(); p->value = x; p->lc = p->rc = NULL; return p; }else{ if (x < root->value) root->lc = Insert(root->lc, x); else root->rc = Insert(root->rc, x); return root; } } bool Find(Node *root, int x){//判断x是否在搜索树中 if (!root) return false; else if (x == root->value) return true; else if (x > root->value) return Find(root->rc, x); else return Find(root->lc, x); } Node* Delete(Node *root, int x){//从搜索树中删除x if (!root) return NULL; if (x > root->value) root->rc = Delete(root->rc, x); else if (x < root->value) root->lc = Delete(root->lc, x); else if (root->lc == NULL){//如果左儿子为空,就把右儿子提上去 Node *p = root->rc; delete root; return p; } else if (root->lc->rc == NULL){//如果左儿子没有右儿子,就把左儿子提上去 Node *p = root->lc; p->rc = root->rc; delete root; return p; } else{//在左儿子的后代中找到一个最大的节点,把它提上去当父亲 Node *p; for (p = root->lc; p->rc->rc != NULL; p = p->rc); Node *r = p->rc; p->rc = r->lc; r->lc = root->lc; r->rc = root->rc; delete root; return r; } return root; } Node* Build_BST(int *a, int n){//输入数组,建立二叉搜索树 srand((unsigned)time(0));//如果数组是有序的,搜索树就会退化成一个链表,所以我们先随机打乱数组顺序 int r = rand() % n; swap(a[0], a[r]); Node *root = NULL; for (int i = 0; i < n; i++) root = Insert(root, a[i]); return root; } void Inorder_view(Node *root){ if (root){ if (root->lc) Inorder_view(root->lc); printf("%d\n", root->value); if (root->rc) Inorder_view(root->rc); } } int main() { //freopen("1.txt", "r", stdin); int a[maxn], n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; Node *root = Build_BST(a, n); root = Delete(root, 5); Inorder_view(root); root = Delete(root, 6); Inorder_view(root); return 0; }
0 0
- 二叉搜索树的实现和前序遍历
- 二叉搜索树的前序遍历
- 二叉搜索树的后序遍历序列和二叉搜索树的前序遍历序列
- 二叉树和二叉搜索树的前序、中序和后序遍历
- 编程实现二叉树的建立,前序遍历,中序遍历和后续遍历
- 二叉搜索树的中序 前序 后序遍历JAVA实现
- 判断二叉搜索树的前序遍历序列
- 二叉搜索树的前序遍历序列
- java写的二叉搜索树(生成和前序遍历)
- 二叉树搜索树遍历--前序、中序、后序、深度优先、广度优先遍历的简单实现
- 二叉树的遍历--用递归 和栈 实现 前序、中序、后序遍历
- 根据前序遍历和中序遍历重建二叉树的Java实现
- 二叉搜索树+前序遍历 -> 后序遍历
- 一直二叉树的前序遍历和中序遍历的结果,重建该二叉树 Java代码实现
- 已知二叉树的前序遍历和中序遍历求后序遍历(二叉树)
- 二叉树系列:已知二叉树的中序遍历和前序遍历,求后序遍历
- 3.14(c程序实现)已知二叉树的前序遍历和中序遍历,求后序遍历
- 已知二叉树的前序遍历和中序遍历重建二叉树(二叉树)
- [Python] Python基础笔记
- iOS 获取当前设备具体型号(当前支持到iPhone7/iPhone7P )、系统版本、电量、序列号、设备名称、应用相关信息获取等
- solution of POJ:3190.Stall Reservations
- 机械设备用油液监测方法
- Android Material风格的应用(一)--AppBar TabLayout
- 二叉搜索树的实现和前序遍历
- sass_混合声明和调用
- WSO2 ESB(1)
- Android Material风格的应用(二)--RecyclerView
- 优雅编码框架简介一
- eclipse32位和64位的区别
- Android Material风格的应用(三)--DrawerLayout
- Fiddler2抓包中文乱码解决方法
- Android Material风格的应用(四)--FloatActionButton