二叉搜索树
来源:互联网 发布:sql 查找not exist 编辑:程序博客网 时间:2024/06/15 09:19
http://paste.ubuntu.com/ 一个粘代码的好地方
#include <iostream>#include <algorithm>#include <cstdio>#include <cstdlib>#include <cstring>#include <stack>#include <queue>#include <vector>using namespace std;struct node { int num; struct node *left; struct node *right;};struct node *CreatNode(int n) { struct node *tmp = (struct node*)malloc(sizeof(struct node)); tmp->num = n; tmp->left = tmp->right = NULL; return tmp;};struct node *Insert(struct node *root, int n) { if(root == NULL) root = CreatNode(n); else if(n < root->num) root->left = Insert(root->left, n); else if(n > root->num) root->right = Insert(root->right, n); return root;}void FirVisited(struct node *root) { if(root) { printf("%d ", root->num); FirVisited(root->left); FirVisited(root->right); }}void bfs(struct node *root) { if(!root) { printf("The tree is empty\n"); return ; } queue<struct node*> q; q.push(root); while(!q.empty()) { struct node *t = q.front(); printf("%d ", t->num); q.pop(); if(t->left) q.push(t->left); if(t->right) q.push(t->right); } printf("\n");}void PrintLeaf(struct node *root) { if(!root) { printf("The tree is empty\n"); return ; } queue<struct node*> q; q.push(root); while(!q.empty()) { struct node *t = q.front(); q.pop(); if(t->left) q.push(t->left); if(t->right) q.push(t->right); if(t->left == NULL && t->right == NULL) printf("%d ", t->num); } printf("\n");}int GetHeight(struct node *root) { if(root == NULL) return 0; int hl = GetHeight(root->left); int hr = GetHeight(root->right); return 1 + max(hl, hr);}struct node *FindMin(struct node *root) { if(!root) return root; struct node *tmp = root; //不能直接改root while(tmp->left) { tmp = tmp->left; } return tmp;}struct node *Delete(struct node *root, int x) { //※ if(!root) return NULL; if(x < root->num) root->left = Delete(root->left, x); else if(x > root->num) root->right = Delete(root->right, x); else { struct node *tmp = root; //初始化tmp只是为了避免在free时编译器报错好烦 if(root->left && root->right) { tmp = FindMin(root->right); root->num = tmp->num; root->right = Delete(root->right, tmp->num); } else { if(root->left == NULL) root = root->right; else if(root->right == NULL) root = root->left; else free(tmp); } } return root;};int main() { struct node *root = NULL; int n, t; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &t); root = Insert(root, t); } printf("firvis: "); FirVisited(root); printf("\n"); printf("bfs:"); bfs(root); printf("Height = %d\n", GetHeight(root)); struct node *Min = FindMin(root); if(Min) printf("Min = %d\n", Min->num); else printf("The tree is empty\n"); printf("Leaf: "); PrintLeaf(root); printf("Input the number you want to delete\n"); scanf("%d", &t); Delete(root, t); bfs(root); return 0;}
0 0
- 【二叉搜索数】HDU3791二叉搜索树
- 二叉树--二叉搜索树
- 【二叉树】二叉搜索树
- 二叉树- 二叉搜索树
- 【搜索树】二叉搜索树
- 二叉搜索树BSTree
- 二叉搜索树
- 二叉搜索树
- 二叉搜索树
- 最优二叉搜索树
- 二叉搜索树
- 二叉搜索树
- HDOJ3791 二叉搜索树
- 二叉查找树搜索
- 二叉搜索树
- 二叉搜索树
- BST 二叉搜索树
- 二叉搜索树
- 471_圆形ImageView
- PowerShell/PowerShellでメールを送信するサンプルコード
- 论文Deep Learning for Identifying Metastatic Breast Cancer论文心得
- 472_无法滚动的ViewPager
- 7.集合1
- 二叉搜索树
- @RequestMapping注解的用法
- Number对象,Math对象,Array对象
- 473_设置全局Application
- python复习笔记[2]——数学运算
- Servlet 处理中文乱码
- REST方式的CXF WebService实现
- T3 字符串替换
- 多媒体(视频播放器-拍照-录像)