C++实现简单的二叉树与二叉排序树
2018-03-15 09:35
447 查看
二叉树的实现#include<iostream>
using namespace std;
struct node{
node *left = NULL,*right = NULL;
char data;
};
node* insert(string str,int &x){
if(str[x] == '#'){
x++;
return NULL;
}else{
node *t = new node;
t->data = str[x];
x++;
t->left = insert(str,x);
t->right = insert(str,x);
return t;
}
}
void bl(node *root){
if(root != NULL){
bl(root->left);
cout<<root->data;
bl(root->right);
}
}
int main(){
string str;
node * root;
while(cin>>str){
int x = 0;
root = insert(str,x);
bl(root);
}
}二叉排序树的实现#include<iostream>
using namespace std;
struct node{
node *left = NULL,*right = NULL;
int data;
};
void insert(int x,node *&root){
if(root == NULL){
root = new node;
root->data = x;
}else{
if(root->data > x){
insert(x,root->left);
}else{
insert(x,root->right);
}
}
}
void bl(node *root){
if(root != NULL){
bl(root->left);
cout<<root->data;
bl(root->right);
}
}
int main(){
node *root = NULL;
int a[5] = {2,1,5,3,4};
for(int i = 0;i < 5;i++){
insert(a[i],root);
}
bl(root);
}
using namespace std;
struct node{
node *left = NULL,*right = NULL;
char data;
};
node* insert(string str,int &x){
if(str[x] == '#'){
x++;
return NULL;
}else{
node *t = new node;
t->data = str[x];
x++;
t->left = insert(str,x);
t->right = insert(str,x);
return t;
}
}
void bl(node *root){
if(root != NULL){
bl(root->left);
cout<<root->data;
bl(root->right);
}
}
int main(){
string str;
node * root;
while(cin>>str){
int x = 0;
root = insert(str,x);
bl(root);
}
}二叉排序树的实现#include<iostream>
using namespace std;
struct node{
node *left = NULL,*right = NULL;
int data;
};
void insert(int x,node *&root){
if(root == NULL){
root = new node;
root->data = x;
}else{
if(root->data > x){
insert(x,root->left);
}else{
insert(x,root->right);
}
}
}
void bl(node *root){
if(root != NULL){
bl(root->left);
cout<<root->data;
bl(root->right);
}
}
int main(){
node *root = NULL;
int a[5] = {2,1,5,3,4};
for(int i = 0;i < 5;i++){
insert(a[i],root);
}
bl(root);
}
相关文章推荐
- 二叉树的简单操作(c++实现)
- 简单数据结构之二叉树(C++实现)
- 二叉树的C++简单实现
- 一个简单二叉树的C++实现(一)
- C++数据结构之 --二叉树简单实现和4种遍历
- C++ 简单二叉树实现
- C++对二叉树的简单实现。
- C++二叉树的简单实现
- 二叉排序树::c++模板实现::泛型类,只要能排序,我就可以排的二叉树!
- C++实现简单的二叉树
- 使用C++实现简单二叉树
- 【c++】简单实现二叉树
- 简单链式二叉树(C++模版技术实现)
- C++实现简单二叉树
- 各类经典搜索算法(Search Algorithms)的简单介绍和C++实现
- 超级玛丽游戏 C++简单实现
- (六)数据结构之二叉树的简单实现
- C++实现二叉树的递归与非递归遍历
- C++实现二叉树的非递归遍历(层,前,中,后序)
- 标准纯C++实现简单的词法分析器(一)