c++-有哪位哥哥姐姐可以帮我写下程序(关于二叉树的 或者栈)

问题描述

有哪位哥哥姐姐可以帮我写下程序(关于二叉树的 或者栈)

那个运用c++编写两个程序,其中一个是实现二叉树(或者栈),另一个是运用二叉树(或者栈)解决实际问题。谢谢啦,实在是没搞懂。

解决方案

/**

  • <!--
  • File : stack.h
  • Author : fancy
  • Email : fancydeepin@yeah.net
  • Date : 2013-02-03
  • --!>
    */
    #include
    #include
    #include
    #define Element char
    #define INIT_SIZE 10
    #define INCREMENT_SIZE INIT_SIZE / 2

typedef struct TStack {
Element *base;
Element *top;
int size;
} *Stack;

//栈的构造器,创建空栈
void stackConstructor(Stack &stack){
stack->base = (Element *)malloc(INIT_SIZE * sizeof(Element));
if(!stack->base){
printf("n为栈分配内存空间失败!n");
exit(0);
}
stack->top = stack->base; //空栈 top == base
stack->size = INIT_SIZE;
}

//是否为空栈
bool isEmpty(Stack stack){
if(stack->top == stack->base){
return true;
}
return false;
}

//压栈
bool push(Stack &stack, Element e){
if(stack->top - stack->base >= stack->size){ //栈满
stack->base = (Element *)realloc(stack->base, (stack->size + INCREMENT_SIZE) * sizeof(Element));
if(!stack->base){
printf("n为栈扩展内存空间失败!n");
return false;
}
stack->top = stack->base + stack->size;
stack->size += INCREMENT_SIZE;
}
*stack->top++ = e;
return true;
}

//弹栈
Element pop(Stack stack){
if(isEmpty(stack)){
printf("n栈为空,弹栈操作失败!n");
return ' ';
}
return *--stack->top;
}

/**

  • <!--
  • File : Stack.cpp
  • Author : fancy
  • Email : fancydeepin@yeah.net
  • Date : 2013-02-03
  • --!>
    */
    #include "stack.h"

int main() {

Stack stack;
stackConstructor(stack);
push(stack, 'f');
push(stack, 'a');
push(stack, 'n');
push(stack, 'c');
push(stack, 'y');
printf("%c", pop(stack));
printf("%c", pop(stack));
printf("%c", pop(stack));
printf("%c", pop(stack));
printf("%c", pop(stack));
//output[result]: ycnaf
return 0;

}

解决方案二:

C++实现二叉树的基本操作

包括 添加节点、删除节点、前序遍历、中序遍历、后续遍历、层序遍历、最大值、最小值、二叉树的高度

//Tree.h 头文件

#include

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
#include
using namespace std ;
#include "Tree.h";
#include

//添加节点
void Tree::AddTreeNode(LinkNode *node,LinkNode *newNode)
{
if(node->data>newNode->data)
{
if(node->left==NULL)
{
node->left=newNode;
}else{
AddTreeNode(node->left,newNode);
}

}else if(node->datadata)
{
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);

return true;
}

//中序遍历
void Tree::ShowCLR(LinkNode *root)
{
if(root!=NULL){
cout<data<<" ";
}

if(root->left!=NULL)
{
ShowCLR(root->left);
}

if(root->right!=NULL)
{
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);
}

if(root!=NULL){
cout<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);
}

if(root->right!=NULL)
{
ShowLRC(root->right);
}

if(root!=NULL){
cout<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;

return true;
}

//最大值
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;

return true;
}

//判断树是否为空
bool Tree::Empty()
{
return head->left==NULL;
}

//用队列实现二叉树层序遍历
//1:添加根节点
//2:打印根节点的数据,添加根节点的子节点,弹出根节点
//3:循环第二步
bool Tree::Floor()
{
queue q;
LinkNode* cur=head->left;
q.push(head->left);
while(!q.empty())
{
cur=q.front();
cout<data<<" ";

if(cur->left!=NULL){
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 "stdafx.h"
#include
#include
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;
}

时间: 2024-08-07 22:32:44

c++-有哪位哥哥姐姐可以帮我写下程序(关于二叉树的 或者栈)的相关文章

哪位大神可以帮我写下在Oracle数据库中查询出来的数据在jsp页面中进行分页显示,我已经能显示了,但是没有分页,直接改我的代码,我初学,没做过分页

问题描述 这是JSP页面代码:<%@pagelanguage="java"contentType="text/html;charset=gbk"pageEncoding="GBk"%><%@pageimport="com.etc.sky.entity.Record"%><%@pageimport="java.util.*"%><!DOCTYPEHTMLPUBLIC&

下面的代码使帮我写下程序啊

问题描述 namespaceMyCryptoHelp{///<summary>///异常处理类///</summary>publicclassCryptoHelpException:ApplicationException{publicCryptoHelpException(stringmsg):base(msg){}}///<summary>///CryptHelp///</summary>publicclassCryptoHelp{privatecons

讲解求-求大神讲解P、V操作。。。求哥哥姐姐忙帮

问题描述 求大神讲解P.V操作...求哥哥姐姐忙帮 求各位哥哥姐姐讲解P.V操作,初学入门菜鸟上课没听讲现在弄不懂啊....小弟在此拜谢了~~~ 解决方案 无非就是说的进程/线程的同步中的信号量.你可以用红绿灯来比喻.某个资源,只能同时由一个进程/线程访问,这就好比一个路口,要么这个方向的车通过,要么那个方向的通过.一起通过就会出事故. 怎么办呢?用红绿灯,只有绿灯的方向,车辆才能通行,别的方向,车辆必须等待.信号量其实就是一样的意思.

变换-麻烦各位帮我改下程序吧,输入#include &amp;amp;quot;stdafx.h&amp;amp;quot;在VC++上面不能运行呀~~

问题描述 麻烦各位帮我改下程序吧,输入#include "stdafx.h"在VC++上面不能运行呀~~ #include ""stdafx.h""#include #include #include #include #include #define N 1000 /*定义复数类型*/ typedef struct { double real; double img; }complex; void fft(); /*快速傅里叶变换*/ void

python-大神帮我改一下程序吧。去掉第一个逗号后内容重复的行。只保留一条

问题描述 大神帮我改一下程序吧.去掉第一个逗号后内容重复的行.只保留一条 #!/usr/bin/python -*- coding: utf-8 -*- 原文本里边有重复的,去重复行 import requchong = open('G:07txtchongfuhang.txt''r').readlines()quchonghou = open('G:07txtchongfuhanghou.txt''w') unique_quchong = []for each_line in quchong:

jsp-JSP传给action的是字符串类型,转换数据类型,让getlist()接收,求大神帮帮忙写下代码

问题描述 JSP传给action的是字符串类型,转换数据类型,让getlist()接收,求大神帮帮忙写下代码 JSP传给action的是字符串类型,怎么转换数据类型,然后让getlist()接收,求各位大神帮帮忙写下代码~ 如果能够给解释一下,那就千恩万谢啦 解决方案 可以通过强制转换在前面加上int 解决方案二: gongWenLeiBieList = dao.getList(Integer.parseInt(mingCheng));

python-去掉第一个逗号后内容重复的行,只保留一行。我是新手,大神帮我写一下程序。非常谢谢

问题描述 去掉第一个逗号后内容重复的行,只保留一行.我是新手,大神帮我写一下程序.非常谢谢 3798 a3 1-14 16:52,西部城区,受到部分商圈打折促销影响,阜成路.复兴路三环以外出城一线交通压力较大, 3799 a3 1-14 16:53,西部城区,受到部分商圈打折促销影响,阜成路.复兴路三环以外出城一线交通压力较大, 3800 a3 1-14 16:58,东北二环内环一线交通压力较大,车辆选择平安大街.东外小街.朝外小街等道路行驶. 我的思路是: time, content = re

c++-我调用opencv中的函数计算出的hu矩没有旋转、缩放不变性,请大家帮我看下程序哪出问题了。

问题描述 我调用opencv中的函数计算出的hu矩没有旋转.缩放不变性,请大家帮我看下程序哪出问题了. #include #include #include using namespace std; using namespace cv; int main(int argc, char *argv[]) {//读入图片预处理 Mat image=imread("F:vs2010 project21.jpg"); //image.create(480, 640, CV_8UC1); na

python 大神帮我看下程序,怎么前边时间加载错误

问题描述 python 大神帮我看下程序,怎么前边时间加载错误 解决方案 你其实可以直接判断在不在unique_quchong中后,如果不在,直接把当前行一整行写入文件就可以了.