c++-C++二叉树类的创建与函数实现

问题描述

C++二叉树类的创建与函数实现

编写二叉树类的成员函数,分别实现以下功能:
①交换二叉树中所有节点的左右子树。(将结果输出至文本文件中)
②按层次顺序遍历二叉树:首先访问根节点,然后是它的两个孩子节点,然后是孙子节点,依此类推。(将结果输出至屏幕)
③求二叉树的宽度,即同一层次上最多的节点数。(将结果输出至屏幕)
首先我的程序不对……不知道如何成功创建一个二叉树?
并且将结果输出至文本也求教

 #include<iostream>
#include<stdio.h>
#include<stack>
#include<queue>
using namespace std;
struct binaryTreeNode{
    int element;
    binaryTreeNode *leftChild, *rightChild;
};
class binaryTree{
public:
    binaryTreeNode *root;
    binaryTree(){
        root = NULL;
    }
    binaryTreeNode* createBinaryTree();
    void preOrder(binaryTreeNode *);
    void inOrder(binaryTreeNode *);
    void postOrder(binaryTreeNode *);
    void swapChild(binaryTreeNode**);
    void leverOrder(binaryTreeNode*);
    void display(){
        leverOrder(root);
        cout << endl;
    }
    int widthOfTheTree(binaryTreeNode*);
};

binaryTreeNode* binaryTree::createBinaryTree()
{
    binaryTreeNode* current = 0;
    int val = 0;
    cin >> val;
    if (val == -1)
        return NULL;
    else
    {
        current = new binaryTreeNode;
        current->element = val;
        current->leftChild = createBinaryTree();
        current->rightChild = createBinaryTree();
        return current;
    }

}
void binaryTree::preOrder(binaryTreeNode *temp){
    if (temp != NULL)
    {
        cout << temp->element << " ";
        preOrder(temp->leftChild);
        preOrder(temp->rightChild);
    }
}
void binaryTree::inOrder(binaryTreeNode *temp){
    if (temp != NULL)
    {
        inOrder(temp->leftChild);
        cout << temp->element << " ";
        inOrder(temp->rightChild);
    }
}
void binaryTree::postOrder(binaryTreeNode *temp){
    if (temp != NULL)
    {
        postOrder(temp->leftChild);
        postOrder(temp->rightChild);
        cout << temp->element << " ";
    }
}
void binaryTree::swapChild(binaryTreeNode**p){
    binaryTreeNode*temp;
    if ((*p)){
        temp = (*p)->leftChild;
        (*p)->leftChild = (*p)->rightChild;
        (*p)->rightChild = temp;
        swapChild(&((*p)->leftChild));
        swapChild(&((*p)->rightChild));
    }
}
void binaryTree::leverOrder(binaryTreeNode*t)
{
    queue<binaryTreeNode*> q;
    if (t != NULL)
        q.push(t);
    binaryTreeNode *b;
    while (!q.empty())
    {
        b = q.front();
        cout << b->element << ' ';
        q.pop();
        if (b->leftChild)
            q.push(b->leftChild);
        if (b->rightChild)
            q.push(b->rightChild);
    }
}
int binaryTree::widthOfTheTree(binaryTreeNode*p)
{
    if (p == NULL)
        return 0;
    queue<binaryTreeNode*> q;
    q.push(p);
    int width = 1;
    int curwidth = 1;
    int nextwidth = 0;
    while (!q.empty())
    {
        while (curwidth != 0)
        {
            binaryTreeNode *temp = q.front();
            q.pop();
            curwidth--;
            if (temp->leftChild != NULL)
            {
                q.push(temp->leftChild);
                nextwidth++;
            }
            if (temp->rightChild != NULL)
            {
                q.push(temp->rightChild);
                nextwidth++;
            }
        }
        if (nextwidth>width)
            width = nextwidth;
        curwidth = nextwidth;
        nextwidth = 0;
    }
    return width;
    cout << "width=" << width << endl;
}

void main()
{
    binaryTree a;
    a.createBinaryTree();
    a.widthOfTheTree(a.root);
    a.display();
}

解决方案

C++二叉树类实现
二叉树的创建和遍历C++实现

解决方案二:

#include
#include
#include

typedef struct BITTREE
{
int data;
struct BITTREE *lchild, *rchild;
}BitTree;

BitTree CreateBitTree()
{
BitTree *root = NULL;
int temp;
if (scanf_s("%d", &temp))
{
root = (BitTree
)malloc(sizeof(BitTree));
root->data = temp;
getchar();
printf("请输入%d的左孩子:",temp);
root->lchild = CreateBitTree();
getchar();
printf("请输入%d的右孩子:", temp);
root->rchild = CreateBitTree();
}
return root;
}
//遍历
`void print(BitTree *root)
{
if (root)
{
print(root->lchild);
printf("%d ", root->data);
print(root->rchild);

}
}
void cc(BitTree *root)//层次遍历
{
queue q;
BitTree *temp = NULL;
if (root)
q.push(root);
while (!q.empty())
{
temp = q.front();
q.pop();
printf("%d ",temp->data);
if (temp->lchild)
q.push(temp->lchild);
if (temp->rchild)
q.push(temp->rchild);
}
}
int GetDeep(BitTree *root)
{
int x, y;
if (root == NULL)
return 0;
x = GetDeep(root->lchild);
y = GetDeep(root->rchild);
return x > y ? x + 1 : y + 1;
}
void jx(BitTree *root)//求树的镜像
{
BitTree *temp = NULL;
if (root != NULL)
{
jx(root->lchild);
jx(root->rchild);
temp = root->lchild;
root->lchild = root->rchild;
root->rchild = temp;
}
}
typedef struct TREE
{
int data;
struct TREE *Firstcshild,*NextBother;
}Tree;//孩子兄弟表示法
int GetTreeDeep(Tree *root)
{
int max = 0,x;
Tree *p = NULL;
if (root == NULL)
return 0;
for (p = root->Firstchild; p != NULL; p = p->NextBother)
{
x = GetTreeDeep(p);
if (max < x)
max = x;
}
return max + 1;
}

void main()
{
BitTree *root=CreateBitTree();
print(root);
puts("");
jx(root);
print(root);
puts("");
}

这是我当初学c的时候写的,你可以参考一下,要换的话就是把结构体换成类就可以了,没有什么难度。。。

时间: 2024-08-04 04:11:06

c++-C++二叉树类的创建与函数实现的相关文章

[OBJECT-C语言随笔之三] 类的创建和实例化以及函数的添加和调用!

本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-object/396.html 上一小节的随笔写了常用的打印以及很基础的数据类型的定义方式,今天就来一起学习下如何创建类与函数的一些随笔: 首先类的创建:在Xcode下,菜单File-New File,然后出现选择class模板,如下图(图1) 图1 class 模版选择界面   这里不需要手动选择什么,,默认是Objectiv

如何在派生类中的隐藏基类的虚拟重载函数

我创建了一个类,基类中有虚拟重载函数.我想在派生类中改写基类中的虚拟重载函数.代码如下: #include <iostream.h>class B {private: int nNumber;public: virtual void test() { cout << "B::test()\n"; } virtual void test(int x) { nNumber = x; // 将传入的参数赋值给私有成员 cout << "B::te

java-类是非静态的,而类中的主函数是静态的,静态方法为什么可以调用非静态类的内部类的非静态方法

问题描述 类是非静态的,而类中的主函数是静态的,静态方法为什么可以调用非静态类的内部类的非静态方法 代码如下所示,TestEX是非静态类,而它的主方法是静态的,TestEX类中含有两个内部类,两个内部类也是非静态的,内部类中的方法同样也是非静态的. 问题是:在静态的主方法中,实例化了内部类,为什么就可以调用非静态了呢? 哪个圈绕不出来了,求帮助! public class TestEX{ public class IntegerException extends Exception { Stri

c++-在windows下mingw对类中含有模板函数报错

问题描述 在windows下mingw对类中含有模板函数报错 在windows下用mingw 进行编译报错,说解析模板参数列表报错.在linux下编译正确. 代码我已经简化: #include <iostream> using namespace std; class Point{ public: //实现setPoint函数 void setPoint(int x, int y){ xPos = x; yPos = y; } //实现printPoint函数 void printPoint(

python类:class创建、数据方法属性及访问控制详解_python

在Python中,可以通过class关键字定义自己的类,然后通过自定义的类对象类创建实例对象. python中创建类 创建一个Student的类,并且实现了这个类的初始化函数"__init__": class Student(object):     count = 0     books = []     def __init__(self, name):         self.name = name 接下来就通过上面的Student类来看看Python中类的相关内容. 类构造和

[CodeComplete]创建一个函数需要理由吗

以下为<<代码大全2>>[第七章高质量的子程序]的摘录 编程中什么是标准,相信大家都没有办法给出一套成系统的理论,而<代码大全>的作者就是在为我们描述从设计到实现诸多大家或意识到而没有深究,又或者还没有意识到的问题,通过系统的方式为大家展开了软件开发中诸多细节.希望对大家能都所帮助! 本章探讨了以下问题:    创建子程序的正当理由    在子程序层上展开设计    起个好名字    子程序可以写多长    如何使用子程序的参数    使用函数时要特别考虑的问题    

c++问题-C++的类中怎么在一个函数中引用上一层的函数

问题描述 C++的类中怎么在一个函数中引用上一层的函数 在同一个类中怎么引用先定义的函数来定义函数?比如在一个时期类的定义中,一个日期加1的函数中怎么调用先定义的一个求是否闰年的函数. 解决方案 C++定义隐式转换函数将类转换为内部的一个成员变量c++函数默认参数是一个好的设计吗? 解决方案二: 直接在函数内调用就可以了.不知道你说的上一层是什么意思.是基类的函数还是集合类所属的对象的函数,前者直接调用,或者用 基类类名::函数名后者用构造函数传对象指针 解决方案三: 同一个类中的函数都是通过t

利用transitions类轻松创建图片过渡效果

创建 利用transitions类轻松创建图片过渡效果点击查看Flash:http://space.flash8.net/bbs/attachment.php?aid=311487 主场景第一帧上:myMovieClip.swapDepths(myMovieClip0); var i:Number = 0; function tween(obj) {         i++;         myMovieClip0.gotoAndStop(i-1);         obj.gotoAndSt

MFC中常用类、宏、函数的简单介绍

闲来无事,整理了一下MFC中常用的类.宏.函数. 常用类 CRect:用来表示矩形的类,拥有四个成员变量:top left bottom right.分别表是左上角和右下角的坐标.可以通过以下的方法构造: CRect( int l, int t, int r, int b ); 指明四个坐标 CRect( const RECT& srcRect ); 由RECT结构构造 CRect( LPCRECT lpSrcRect ); 由RECT结构构造 CRect( POINT point, SIZE