0
点赞
收藏
分享

微信扫一扫

二叉树(前中后序遍历) c++的第二章

梅梅的时光 2022-05-05 阅读 11
c++

今日任务:
打一遍二叉树(前中后序遍历)
c++的第二章全部学完
函数,指针复习

1.二叉树的前中后序遍历

#include<stdio.h>

#include<stdlib.h>

typedef struct Bitree

{

char data;

struct Bitree *lchild,*rchild;

}*BiTree,BiTNode;

BiTree t;

BiTree head;

BiTNode *   CreatTree()

{

BiTNode    * T;

char n;

scanf("%c",&n);

if(n==’#’)

T=NULL;

else

{

T=(BiTNode *)malloc(sizeof(BiTNode));

if(headNULL)

head=T;

T->data=n;

T->lchild=CreatTree();

T->rchild=CreatTree();

}

return T;//

}

void PreTree(BiTree T)

{

if(TNULL)

return;

printf("%c",T->data);

PreTree(T->lchild);

PreTree(T->rchild);

}

void InTree(BiTree T)

{

if(TNULL)

return;

InTree(T->lchild);

printf("%c",T->data);

InTree(T->rchild);

}

void PostTree(BiTree T)

{

if(TNULL)

return;

PostTree(T->lchild);

PostTree(T->rchild);

printf("%c",T->data);

}

int main()

{

head=NULL;

t=CreatTree();

PreTree(head);

printf("\n");

InTree(head);

printf("\n");

PostTree(head);

}
**`2.c++的第二章笔记`**`

```cpp
#include<iostream>
using namespace std//C++输入和输出的工具(即使用cin和cout时一定要用iostream

在这里插入图片描述换行可用
cout<<endl;
cout<<"\n";
库函数sqrt()需在#include<cmath>或者#include<math.h>
计算5的8次方用x=pow(5.0,8.0);函数原型为下:

double pow(double,double)

rand()函数在cstdlib或stdlib的头文件中,该函数用于:去随机整数。
原型:`int rand(void)
在这里插入图片描述

举报

相关推荐

0 条评论