C++ 二叉树的基本操作
来源:互联网 发布:淘宝宝贝描述下载 编辑:程序博客网 时间:2024/04/26 15:56
C++实现二叉树的基本操作
包括 添加节点、删除节点、前序遍历、中序遍历、后续遍历、层序遍历、最大值、最小值、二叉树的高度
//Tree.h 头文件
#include <stdio.h>
class Tree
{
private :
//节点元素类型为结构体
struct LinkNode
{
int data;
LinkNode *left;
LinkNode *right;
LinkNode(const int& dat,LinkNode *l,LinkNode *r):data(dat),left(l),right(r){}
};
LinkNode *head;//表头节点
//添加节点
void AddTreeNode(LinkNode *node,LinkNode *newNode);
//显示中序排列
void ShowCLR(LinkNode *root);
//显示前序排列
void ShowLCR(LinkNode *root);
//显示右序排列
void ShowLRC(LinkNode *root);
//高度
int Height(LinkNode *root);
public :
//添加节点
bool AddTreeNode(int data);
//显示中序排列
bool ShowCLR();
//显示前序排列
bool ShowLCR();
//显示右序排列
bool ShowLRC();
//前序排列
bool Floor();
//最小值
bool Min(int** minValue);
//最大值
bool Max(int** maxValue);
//是否是空树
bool Empty();
//高度
void Height(int** height);
~Tree()
{
delete[] head;
}
Tree()
{
head=new LinkNode(-1,NULL,NULL);
}
};
//实现文件Tree.cpp
#include "stdafx.h"
#include <stdio.h>
#include<iostream>
using namespace std ;
#include "Tree.h";
#include <queue>
}else if(node->data<newNode->data) } //添加节点 return true; //中序遍历 if(root->right!=NULL) //中序遍历 if(root!=NULL){ //前序遍历 //后序遍历 if(root->right!=NULL) if(root!=NULL){ //后序遍历 //最小值 return true; //最大值 return true; //判断树是否为空 //用队列实现二叉树层序遍历 if(cur->left!=NULL){ } //求两个数中较大的一个 //递归求二叉树的高度 //用指向指针的指针接受二叉树的高度
//添加节点
void Tree::AddTreeNode(LinkNode *node,LinkNode *newNode)
{
if(node->data>newNode->data)
{
if(node->left==NULL)
{
node->left=newNode;
}else{
AddTreeNode(node->left,newNode);
}
{
if(node->right==NULL)
{
node->right=newNode;
}else{
AddTreeNode(node->right,newNode);
}
}
bool Tree::AddTreeNode(int data)
{
LinkNode *node=new LinkNode(data,NULL,NULL);
if(head->left==NULL)
{
head->left=node;
}
AddTreeNode(head->left,node);
}
void Tree::ShowCLR(LinkNode *root)
{
if(root!=NULL){
cout<<root->data<<" ";
}
if(root->left!=NULL)
{
ShowCLR(root->left);
}
{
ShowCLR(root->right);
}
}
bool Tree::ShowCLR()
{
if(Empty())
{
return false;
}
ShowCLR(head->left);
return true;
}
//前序遍历
void Tree::ShowLCR(LinkNode *root)
{
if(root->left!=NULL)
{
ShowLCR(root->left);
}
cout<<root->data<<" ";
}
if(root->right!=NULL)
{
ShowLCR(root->right);
}
}
bool Tree::ShowLCR()
{
if(Empty())
{
return false;
}
ShowLCR(head->left);
return true;
}
void Tree::ShowLRC(LinkNode *root)
{
if(root->left!=NULL)
{
ShowLRC(root->left);
}
{
ShowLRC(root->right);
}
cout<<root->data<<" ";
}
}
bool Tree::ShowLRC()
{
if(Empty())
{
return false;
}
ShowLRC(head->left);
return true;
}
bool Tree::Min(int** minValue)
{
if(Empty())
{
return false;
}
LinkNode *tmp=head->left;
while(tmp!=NULL && tmp->left!=NULL)
{
tmp=tmp->left;
}
**minValue= tmp->data;
}
bool Tree::Max(int** maxValue)
{
if(Empty())
{
return false;
}
LinkNode *tmp=head->left;
while(tmp!=NULL && tmp->right!=NULL)
{
tmp=tmp->right;
}
**maxValue= tmp->data;
}
bool Tree::Empty()
{
return head->left==NULL;
}
//1:添加根节点
//2:打印根节点的数据,添加根节点的子节点,弹出根节点
//3:循环第二步
bool Tree::Floor()
{
queue<LinkNode*> q;
LinkNode* cur=head->left;
q.push(head->left);
while(!q.empty())
{
cur=q.front();
cout<<cur->data<<" ";
q.push(cur->left);
}
if(cur->right!=NULL)
{
q.push(cur->right);
}
q.pop();
}
return true;
int max(int a,int b)
{
if(a>b)
{
return a;
}else
{
return b;
}
}
//二叉树的高度就是左子树和右子树中较大一颗二叉树的高度
int Tree::Height(LinkNode *root)
{
if(root==NULL)
{
return 0;
}
return 1+max(Height(root->left),Height(root->right));
}
void Tree::Height(int** height)
{
**height=Height(head->left);
}
#include <stdio.h>
#include <iostream>
using namespace std;
#include "Tree.h"
void TreeTest() ;
int main()
{
TreeTest();
int a=0;
cin>>a;
return 0;
}
//指针类型调用属性和方法用“->” 对象类型用“.”
//变量在不需要使用的时候就要释放掉它的内存
void TreeTest()
{
Tree tree;
int a[]={1,3,6,7,8,2,4,9,10,5};
for(int i=0;i<10;i++){
tree.AddTreeNode(a[i]);
}
cout<<"-----------原始数组----------"<<endl;
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<endl;
cout<<"-----------中序排列----------"<<endl;
tree.ShowCLR();
cout<<endl;cout<<endl;
cout<<"-----------前序排列----------"<<endl;
tree.ShowLCR();
cout<<endl;cout<<endl;
cout<<"-----------后序排列----------"<<endl;
tree.ShowLRC();
cout<<endl;
cout<<endl;
cout<<"-----------层序排列----------"<<endl;
cout<<endl;
tree.Floor();
cout<<endl;
int min=-1;
int *pmin=&min;
tree.Max(&pmin);
cout<<endl;
cout<<"最大值:"<<min<<endl;
cout<<endl;
tree.Min(&pmin);
cout<<"最小值:"<<min<<endl;
cout<<endl;
int h=-1;
int *height=&h;
tree.Height(&height);
cout<<"高度:"<<h<<endl;
}
- 二叉树的基本操作(C++)
- C语言二叉树的基本操作
- 二叉树的基本操作C源代码
- 【C++】二叉树的基本操作
- 二叉树的基本操作 C语言
- c 二叉树基本操作
- 二叉搜索树的基本操作(C代码实现)
- 数据结构.二叉树的基本操作(C语言实现)
- C语言实现二叉树的基本操作
- c语言实现二叉树的基本操作
- C语言实现二叉树的基本操作
- C语言实现二叉树的基本操作
- 二叉树的基本操作(C语言版)
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- Windows Live Writer的几种代码插件比较
- c编程小技巧
- python中如何判断一个变量的数据类型
- 使用开源软件必须注意GPL,LGPL和BSD等协议
- uva 10110 - Light, more light
- C++ 二叉树的基本操作
- 韩国政府网络受“僵尸”围攻,防治Bot网络需多方合作
- JavaFX离线部署和运行,客户端浏览器可以自动检测并下载安装指定服务器的JRE
- 查询的端口 -解决端口被占用的问题
- local function definitions are illegal
- OpenCV实例:对实时视频做人脸检测
- 版本控制常用工具
- gnome 菜单
- “QT学习视频、QT资料和源代码”小菜特别推荐。