二叉搜索树(递归)
来源:互联网 发布:中信出版社 知乎 编辑:程序博客网 时间:2024/05/20 08:24
二叉排序树也叫二叉搜索树和二叉查找树,是比较常见的一种二叉树
下面是二叉树的简单递归建立删除与遍历及清除,旋转操作方法:
#include<iostream>using namespace std;class binary { private: struct node { int data; node* left; node* right; node(int t= 0, node* ll= NULL, node* rr= NULL): data(t), left(ll), right(rr) {} }; node* root; public: binary(): root(NULL) {} ~binary() { clear(root); } void prevtravel() { prevtravel(root);} void prevtravel(node* tree) { if (tree== NULL) return; cout<< " "<< tree->data; prevtravel(tree->left); prevtravel(tree->right); } void intravel() { intravel(root);} void intravel(node* tree) { if (tree== NULL) return; intravel(tree->left); cout<< " "<< tree->data; intravel(tree->right); } void posttravel() { posttravel(root);} void posttravel(node* tree) { if (tree== NULL) return; posttravel(tree->left); posttravel(tree->right); cout<< " "<< tree->data; } void insert(int t) { insert(root, t); } void insert(node*& tree, int t) { if (tree== NULL) tree= new node(t); else if (t<= tree->data) insert(tree->left, t); else insert(tree->right, t); } void remove(int t) { remove(t, root);}void remove(int t, node*& r) {if (r== NULL) return;if (t< r->data) remove(t, r->left);else if (r->data< t) remove(t, r->right);else if (r->left!= NULL&&r->right!= NULL) {node* temp= r->right;while (temp->left!= NULL) temp= temp->left;r->data= temp->data;remove(r->data, r->right);} else {node* n= r;r= (r->left!= NULL)? r->left: r->right;delete n;}}void reverse() { queue<Node*> q; if (root!= NULL) q.push(root); while (!q.empty()) { Node *t_root= q.front(); q.pop(); if (t_root->left!= NULL||t_root->right!= NULL) { if (t_root->left== NULL) { t_root->left= t_root->right; t_root->right= NULL; } else if (t_root->right== NULL) { t_root->right= t_root->left; t_root->left= NULL; } else if (t_root->left!= NULL&&t_root->right!= NULL) { Node* temp= t_root->left; t_root->left= t_root->right; t_root->right= temp; } Node *l= t_root->left; Node *r= t_root->right; if (l!= NULL&&(l->left!= NULL||l->right!= NULL)) q.push(l); if (r!= NULL&&(r->left!= NULL||r->right!= NULL)) q.push(r);} } } void clear(node* tree) { if (tree== NULL) return; clear(tree->left); clear(tree->right); delete tree; }};int main() { int n; binary tree; cin>> n; for (int i= 0; i< n; i++) { tree.insert(i); } cout<< "Inorder:"; tree.intravel(); cout<< "\n"; cout<< "Preorder:"; tree.prevtravel(); cout<< "\n"; cout<< "Postorder:"; tree.posttravel(); cout<< "\n"; for (int i= 0; i< n; i+= 2) // 删除偶数 tree.remove(i); cout<< "Inorder:"; tree.intravel(); cout<< "\n"; cout<< "Preorder:"; tree.prevtravel(); cout<< "\n"; cout<< "Postorder:"; tree.posttravel(); cout<< "\n"; return 0;}
例子:
0 0
- 二叉搜索树(递归)
- 二叉树搜索 递归
- 二叉搜索树(非递归)
- 二叉搜索树(递归和非递归分别实现)
- C++实现二叉搜索树(递归&非递归)
- 二叉搜索树-非递归
- 递归建立二叉搜索树
- 二叉搜索树之递归
- 二叉搜索树---递归及非递归
- C++数据结构: 二叉搜索树 (非递归)
- NYOJ 202 红黑树(二叉树的递归搜索)
- 递归实现二叉搜索树建立
- 非递归实现搜索二叉树
- 数据结构(一)二叉搜索树-递归实现
- 二叉搜索树的非递归实现
- 二叉搜索树之非递归
- 二叉搜索树 (java递归版)
- 二叉搜索树 (c++递归版)
- 结构体数组应用,C++课本P209页例题7.2,投票选举情况
- 清除浮动的子元素
- Gson的使用介绍
- BestCoder Round #83
- C语言宏条件编译详解
- 二叉搜索树(递归)
- Hibernate 调用mysql
- 对objc_msgSend的分析
- JSTL 及 tablibs 的简单介绍和配置方法
- 友元类
- 第一章 JAVA入门(什么是android)
- SpringMVC拦截器实现登录认证
- [LeetCode] Integer Break
- 二分查找/折中查找