二叉树的C++源码
来源:互联网 发布:最近网络融资骗局 编辑:程序博客网 时间:2024/04/30 11:05
参考着课件,写了一个简单的二叉树,用法很简单,方法也很少,要是谁有兴趣的话,加上一个线索二叉树的方法,就更好了。
KaKaBTree btree("-(+(a,*(b,-(c,d))),/(e,f))");//用广义表,表示法输入
btree.Preorder();//前序遍历
btree.Inorder();//中序遍历
btree.Postorder();//后序遍历
cout<<btree.Depth();//深度
KaKaBTree.h
#pragma once
#include <string>
#include <vector>
#include <iostream>
using std::string;
typedef struct Note{
char Date;
Note *Left;
Note *Right;
Note(char char_v){
Date=char_v;
Left=0;
Right=0;
}
}*PNote;
class KaKaBTree
{
public:
KaKaBTree(void);
KaKaBTree(string s);//用广义表,表示法输入,KaKaBTree btree("-(+(a,*(b,-(c,d))),/(e,f))")
private:
KaKaBTree(const KaKaBTree& bt);
KaKaBTree operator=(const KaKaBTree& bt);
public:
void Init(string s);//用广义表,表示法输入,KaKaBTree btree("-(+(a,*(b,-(c,d))),/(e,f))")
void Preorder(void);//前序遍历
void Inorder(void);//中序遍历
void Postorder(void);//后序遍历
int Depth(void);//深度
public:
~KaKaBTree(void);
private:
PNote BT;
void Clear(void);
private:
void Create(string s);
void Iner_Clear(PNote PN);
void Iner_Preorder(PNote pn);
void Iner_Inorder(PNote pn);
void Iner_Postorder(PNote pn);
int Iner_Depth(PNote pn);
};
#include <string>
#include <vector>
#include <iostream>
using std::string;
typedef struct Note{
char Date;
Note *Left;
Note *Right;
Note(char char_v){
Date=char_v;
Left=0;
Right=0;
}
}*PNote;
class KaKaBTree
{
public:
KaKaBTree(void);
KaKaBTree(string s);//用广义表,表示法输入,KaKaBTree btree("-(+(a,*(b,-(c,d))),/(e,f))")
private:
KaKaBTree(const KaKaBTree& bt);
KaKaBTree operator=(const KaKaBTree& bt);
public:
void Init(string s);//用广义表,表示法输入,KaKaBTree btree("-(+(a,*(b,-(c,d))),/(e,f))")
void Preorder(void);//前序遍历
void Inorder(void);//中序遍历
void Postorder(void);//后序遍历
int Depth(void);//深度
public:
~KaKaBTree(void);
private:
PNote BT;
void Clear(void);
private:
void Create(string s);
void Iner_Clear(PNote PN);
void Iner_Preorder(PNote pn);
void Iner_Inorder(PNote pn);
void Iner_Postorder(PNote pn);
int Iner_Depth(PNote pn);
};
KaKaBTree.cpp
#include "KaKaBTree.h"
#define MAX(a,b) ((a)>(b)?(a):(b))
using std::vector;
using std::string;
using std::cout;
using std::endl;
KaKaBTree::KaKaBTree(void):BT(0)
{
}
KaKaBTree::KaKaBTree(string s):BT(0)
{
this->Create(s);
}
KaKaBTree::~KaKaBTree(void)
{
this->Clear();
}
void KaKaBTree::Init(string s)
{
this->Clear();
this->Create(s);
}
void KaKaBTree::Clear(void)
{
this->Iner_Clear(BT);
BT=0;
}
void KaKaBTree::Iner_Clear(PNote PN)
{
if(PN){
Iner_Clear(PN->Left);
Iner_Clear(PN->Right);
delete PN;
}
}
void KaKaBTree::Create(string s)
{
//用广义表,表示法输入
if(s.empty()) return;
vector<Note*> StackNote;
enum LR{L,R};
LR lr=L;
BT=new Note(s[0]);
PNote t_p=BT;
for(string::size_type i=1;i!=s.size();++i){
char ch=s[i];
switch(ch){
case '(':
StackNote.push_back(t_p);
lr=L;
break;
case ',':
lr=R;
break;
case ')':
StackNote.pop_back();
break;
default:
if(lr==L){
t_p= StackNote[StackNote.size()-1]->Left=new Note(ch);
}else{
t_p= StackNote[StackNote.size()-1]->Right=new Note(ch);
}
}
}
}
void KaKaBTree::Preorder(void)
{
cout<<endl<<"===================Preorder==================="<<endl;
this->Iner_Preorder(BT);
cout<<endl<<"==============================================="<<endl;
}
void KaKaBTree::Iner_Preorder(PNote pn)
{
//前序遍历
if(pn){
cout<<pn->Date<<" ";
this->Iner_Preorder(pn->Left);
this->Iner_Preorder(pn->Right);
}
}
void KaKaBTree::Inorder(void)
{
cout<<endl<<"==================Inorder======================"<<endl;
this->Iner_Inorder(BT);
cout<<endl<<"==============================================="<<endl;
}
void KaKaBTree::Iner_Inorder(PNote pn)
{
//中序遍历
if(pn){
this->Iner_Inorder(pn->Left);
cout<<pn->Date<<" ";
this->Iner_Inorder(pn->Right);
}
}
void KaKaBTree::Postorder(void)
{
cout<<endl<<"=============Postorder========================="<<endl;
this->Iner_Postorder(BT);
cout<<endl<<"==============================================="<<endl;
}
void KaKaBTree::Iner_Postorder(PNote pn)
{
//后序遍历
if(pn){
this->Iner_Postorder(pn->Left);
this->Iner_Postorder(pn->Right);
cout<<pn->Date<<" ";
}
}
int KaKaBTree::Depth(void)
{
return Iner_Depth(BT);
}
int KaKaBTree::Iner_Depth(PNote pn)
{
//深度
if(!pn){
return 0;
}else{
int dl,dr;
dl=Iner_Depth(pn->Left);
dr=Iner_Depth(pn->Right);
return MAX(dl,dr)+1;
}
}
#define MAX(a,b) ((a)>(b)?(a):(b))
using std::vector;
using std::string;
using std::cout;
using std::endl;
KaKaBTree::KaKaBTree(void):BT(0)
{
}
KaKaBTree::KaKaBTree(string s):BT(0)
{
this->Create(s);
}
KaKaBTree::~KaKaBTree(void)
{
this->Clear();
}
void KaKaBTree::Init(string s)
{
this->Clear();
this->Create(s);
}
void KaKaBTree::Clear(void)
{
this->Iner_Clear(BT);
BT=0;
}
void KaKaBTree::Iner_Clear(PNote PN)
{
if(PN){
Iner_Clear(PN->Left);
Iner_Clear(PN->Right);
delete PN;
}
}
void KaKaBTree::Create(string s)
{
//用广义表,表示法输入
if(s.empty()) return;
vector<Note*> StackNote;
enum LR{L,R};
LR lr=L;
BT=new Note(s[0]);
PNote t_p=BT;
for(string::size_type i=1;i!=s.size();++i){
char ch=s[i];
switch(ch){
case '(':
StackNote.push_back(t_p);
lr=L;
break;
case ',':
lr=R;
break;
case ')':
StackNote.pop_back();
break;
default:
if(lr==L){
t_p= StackNote[StackNote.size()-1]->Left=new Note(ch);
}else{
t_p= StackNote[StackNote.size()-1]->Right=new Note(ch);
}
}
}
}
void KaKaBTree::Preorder(void)
{
cout<<endl<<"===================Preorder==================="<<endl;
this->Iner_Preorder(BT);
cout<<endl<<"==============================================="<<endl;
}
void KaKaBTree::Iner_Preorder(PNote pn)
{
//前序遍历
if(pn){
cout<<pn->Date<<" ";
this->Iner_Preorder(pn->Left);
this->Iner_Preorder(pn->Right);
}
}
void KaKaBTree::Inorder(void)
{
cout<<endl<<"==================Inorder======================"<<endl;
this->Iner_Inorder(BT);
cout<<endl<<"==============================================="<<endl;
}
void KaKaBTree::Iner_Inorder(PNote pn)
{
//中序遍历
if(pn){
this->Iner_Inorder(pn->Left);
cout<<pn->Date<<" ";
this->Iner_Inorder(pn->Right);
}
}
void KaKaBTree::Postorder(void)
{
cout<<endl<<"=============Postorder========================="<<endl;
this->Iner_Postorder(BT);
cout<<endl<<"==============================================="<<endl;
}
void KaKaBTree::Iner_Postorder(PNote pn)
{
//后序遍历
if(pn){
this->Iner_Postorder(pn->Left);
this->Iner_Postorder(pn->Right);
cout<<pn->Date<<" ";
}
}
int KaKaBTree::Depth(void)
{
return Iner_Depth(BT);
}
int KaKaBTree::Iner_Depth(PNote pn)
{
//深度
if(!pn){
return 0;
}else{
int dl,dr;
dl=Iner_Depth(pn->Left);
dr=Iner_Depth(pn->Right);
return MAX(dl,dr)+1;
}
}
- 二叉树的递归与非递归遍历源码(C++)
- 二叉查找树定义与C源码
- 平衡二叉树,插入,删除,C源码
- 平衡二叉树,插入,删除,C源码
- 二叉树的C++源码
- c的二叉树
- 树和二叉树的一些操作的C/C++源码
- [原创] 二叉平衡树AVL的插入和删除的C实现源码
- 二叉平衡树AVL的插入和删除的C实现源码
- 二叉平衡树AVL的插入和删除的C实现源码
- 关于C++类库KYLib: 用C语言实现平衡二叉树(AVL tree)的源码
- C版本的二叉树
- 二叉查找树的各种操作源码
- 哈夫曼二叉树源码
- java-二叉树源码
- 二叉树/二叉搜索树的操作汇集(c++)
- 纯C创建二叉树及二叉树的遍历
- c语言二叉树和二叉搜索树的实现
- Google搜索引擎原理(转自:http://zz.itjj.net/seach/20070820/250119.html)
- 用VC++制作QQ自动登陆软件(转载)
- 有关XP与ubuntu双系统及grub引导
- 网络协议扫盲
- 批处理编程- -介绍(从别人哪里弄的,自己学习了一下)
- 二叉树的C++源码
- windows XP 的命令大全(转载)
- 牛仔裤不会一直等你
- 《pro Spring》学习笔记之Quartz的cronTrigger使用
- 上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片
- jmf摄像头applet
- 什么是交叉编译-嵌入式基础系列(网络查找)
- windows变量前缀总结(原创)
- How the 80/20 rule can help you be more effective.