当前位置:首页 » 编程语言 » 二叉树的C语言遍历

二叉树的C语言遍历

发布时间: 2022-06-08 11:34:35

① 急求c语言写二叉树的遍历

下面是一个用
递归方法
编的二叉树遍历程序,供lz参考。
#include
<stdio.h>//头文件
#include
<stdlib.h>
#include
<malloc.h>
typedef
struct
bitnode
{
char
data;
struct
bitnode
*lchild,*rchild;
}
bitnode,*bitree;//定义结点类型
bitree
createbitree()//创建树
{
char
p;bitree
t;
scanf("%c",&p);
if(p=='
')
t=null;
else
{
t=(bitnode
*)malloc(sizeof(bitnode));//为结点开辟空间
t->data=p;
t->lchild=createbitree();
t->rchild=createbitree();
}
return
(t);
}
void
preorder(bitree
t)//
先序
{
if(t!=null)
{
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void
inorder(bitree
t)//
中序
{
if(t!=null)
{
inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
void
postorder(bitree
t)//
后序
{
if(t!=null)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}
void
main()//主函数
{
bitree
ta;
ta=createbitree();
printf("先序遍历:");
printf("\n");
preorder(ta);
printf("\n");
printf("中序遍历:");
printf("\n");
inorder(ta);
printf("\n");
printf("后序遍历:");
printf("\n");
postorder(ta);
}

② 求助:在数据结构中,用C语言如何编写二叉树的遍历

#include "dataStructure.h "

typedef struct CharBiTNode {
char data;
struct CharBiTNode *lchild, *rchild;
} CharBiTNode, *CharBiTree;

typedef struct CharBiTQNode {
CharBiTree data;
struct CharBiTQNode *next;
} CharBiTQNode, *CharBiTQueuePtr;

typedef struct CharBiTQueue {
CharBiTQNode *front;
CharBiTQNode *rear;
} CharBiTQueue;

/**************** Char Queue *********************************/
Status initCharBiTQueue(CharBiTQueue& q) {
q.front = q.rear = (CharBiTQNode*)malloc(sizeof(CharBiTQNode));
if (!q.front)
return ERROR;
q.front-> next = NULL;
}

Boolean charBiTQueueEmpty(CharBiTQueue q) {
if (q.front == q.rear)
return TRUE;
else
return FALSE;
}

Status getCharBiTQueueLength(CharBiTQueue q) {

}

Status getCharBiTQueueHead(CharBiTQueue q, CharBiTree& e) {
e = q.front-> next-> data;
return OK;
}

Status enCharBiTQueue(CharBiTQueue& q, CharBiTree e) {

CharBiTQNode *newNode = (CharBiTQNode*)malloc(sizeof(CharBiTQNode));
if (!newNode)
return ERROR;
newNode-> data = e;
newNode-> next = NULL;
q.rear-> next = newNode;
q.rear = newNode;
return OK;
}

Status deCharBiTQueue(CharBiTQueue& q, CharBiTree& e) {

if (charBiTQueueEmpty(q)) {
return ERROR;
}
CharBiTQNode *p = q.front;
e = p-> next-> data;
q.front = q.front-> next;
free(p);
return OK;
}

Status charBiTQueueTraverse(CharBiTQueue q, Status (*visit)(CharBiTree e)) {

if (charBiTQueueEmpty(q)) {
return ERROR;
}
CharBiTQueuePtr p = q.front;
while(p) {
if (visit(p-> data) == ERROR)
return ERROR;
p = p-> next;
}
return OK;
}

/****************************** Char BiTree ****************************/

int depthLeft = 0;
int depthRight = 0;
int depth = 0;

Status initCharBiTree(CharBiTree& t) {
CharBiTNode *root = NULL;
root = (CharBiTNode*)malloc(sizeof(CharBiTNode));
if (!root)
return ERROR;
else
t = root;
t-> lchild = t-> rchild = NULL;
return OK;
}

Status createCharBiTree(CharBiTree& t) {
char ch;
cin > > ch;
if (ch == '# ')
t = NULL;
else {
if ((t = (CharBiTNode*)malloc(sizeof(CharBiTNode))) == ERROR)
return ERROR;
else {
t-> data = ch;
createCharBiTree(t-> lchild);
createCharBiTree(t-> rchild);
}
}
}

Boolean charBiTreeEmpty(CharBiTree t) {

}

Status charPreOrderTraverse(CharBiTree t, Status(*visit)(char e)) {

if (t != NULL){
if (visit(t-> data) == ERROR)
return ERROR;
if (charPreOrderTraverse(t-> lchild, visit) == ERROR)
return ERROR;
if (charPreOrderTraverse(t-> rchild, visit) == ERROR)
return ERROR;
} else
return OK;
}

Status charInOrderTraverse(CharBiTree t, Status(*visit)(char e)) {

if (t != NULL) {
if (charInOrderTraverse(t-> lchild, visit) == ERROR)
return ERROR;
if (visit(t-> data) == ERROR)
return ERROR;
if (charInOrderTraverse(t-> rchild, visit) == ERROR)
return ERROR;
} else
return OK;
}

Status charPostOrderTraverse(CharBiTree t, Status(*visit)(char e)) {

if (t != NULL) {
if (charPostOrderTraverse(t-> lchild, visit) == ERROR)
return ERROR;
if (charPostOrderTraverse(t-> rchild, visit) == ERROR)
return ERROR;
if (visit(t-> data) == ERROR)
return ERROR;
} else
return OK;
}

Status charLevelOrderTraverse(CharBiTree t, Status(*visit)(char e)) {
CharBiTQueue queue;
initCharBiTQueue(queue);
CharBiTree p;

if (t == NULL)
return ERROR;
else
enCharBiTQueue(queue, t);

while(!charBiTQueueEmpty(queue)) {
deCharBiTQueue(queue, p);
visit(p-> data);
if (p-> lchild != NULL) {
enCharBiTQueue(queue, p-> lchild);
}
if (p-> rchild != NULL) {
enCharBiTQueue(queue, p-> rchild);
}
}
}

int getCharBiTNodes(CharBiTree t, int& n) {

if (t != NULL) {
++n;
getCharBiTNodes(t-> lchild, n);
getCharBiTNodes(t-> rchild, n);
} else
return OK;
}

Status getCharBiTLeafs(CharBiTree t, int& n) {
if (t != NULL) {
if ( t-> lchild == NULL && t-> rchild == NULL)
++n;

getCharBiTLeafs(t-> lchild, n);
getCharBiTLeafs(t-> rchild, n);
} else
return OK;
}

int getCharBiTDepth(CharBiTree t) {

if (t == NULL) {
depth = 0;
} else {
depthLeft = getCharBiTDepth(t-> lchild);
depthRight = getCharBiTDepth(t-> rchild);
depth = 1 + (depthLeft > depthRight ? depthLeft:depthRight);
}
return depth;
}

Status printChar(char e) {
cout < <e;
return OK;
}

/* Test of char BiTree */

int main(char args[]) {

CharBiTree tree;
int numOfNode = 0, leafs = 0, depth = 0;
cout < < "请按先序输入二叉树元素 " < <endl;
createCharBiTree(tree);

cout < < "T(pre) = ";
charPreOrderTraverse(tree, printChar);
cout < <endl;

cout < < "T(in) = ";
charInOrderTraverse(tree, printChar);
cout < <endl;

cout < < "T(post) = ";
charPostOrderTraverse(tree, printChar);
cout < <endl;

cout < < "T(level) = ";
charLevelOrderTraverse(tree, printChar);
cout < <endl;

cout < < "The nodes of the tree is ";
getCharBiTNodes(tree, numOfNode);
cout < <numOfNode < <endl;

cout < < "The leafs of the tree is ";
getCharBiTLeafs(tree, leafs);
cout < <leafs < <endl;

cout < < "Tree Depth = ";
cout < <getCharBiTDepth(tree) < <endl;

system( "pause ");
}

③ C语言二叉树的遍历。

二叉树的前中后遍历(递归与非递归)
#include<stdio.h>
#include<stdlib.h>

typedef struct NODE
{
char value;
struct NODE *LChild;
struct NODE *RChild;
}BiTNode,*BiTree; //二叉树数据结构
BiTree root;
typedef struct node
{
BiTNode *pointer;
struct node *link;
}LinkStackNode,*LinkStack; //链栈数据结构
LinkStack S;
int count = 0;
//BiTNode * InitTree(BiTree Tree);
BiTNode *CreateTree(BiTree Tree); //创建二叉树
void PreOrder(BiTree Tree); //递归前序遍历二叉树
void MidOrder(BiTree Tree); //递归中序遍历二叉树
void PostOrder(BiTree Tree); //递归后序遍历二叉树
void NPreOrder(BiTree Tree); //非递归前序遍历二叉树
void NMidOrder(BiTree Tree); //非递归中序遍历二叉树
void NPostOrder(BiTree Tree); //非递归后序遍历二叉树
//---------------------------------------------------
LinkStackNode *InitLinkStack(LinkStack top); //初始化链栈
void Push(LinkStack top,BiTNode *p); //进栈操作
BiTNode * Pop(LinkStack top); //出栈操作
//int IsEmpty(LinkStack S); //判断栈是否为空
void main()
{
//BiTree tree;
//root = InitTree(tree);
root = CreateTree(root);
PreOrder(root);
printf("\n");
MidOrder(root);
printf("\n");
PostOrder(root);
printf("\n");
NPreOrder(root);
printf("\n");
NMidOrder(root);
printf("\n");
NPostOrder(root);
printf("\n");
}

/*BiTNode * InitTree(BiTree Tree)
{
//BiTNode *root;
//root = Tree;
Tree = (BiTNode *)malloc(sizeof(BiTNode));
Tree = NULL;
//Tree->LChild = NULL;
//Tree->RChild = NULL;
return Tree;
}*/

//二叉树的扩展先序遍历的创建
BiTNode * CreateTree(BiTree Tree)
{
char ch;
ch = getchar();
if(ch == '.')
Tree = NULL;
else
{
Tree = (BiTNode *)malloc(sizeof(BiTNode));
if(Tree)
{
Tree->value = ch;
Tree->LChild = CreateTree(Tree->LChild);
Tree->RChild = CreateTree(Tree->RChild);
}
}
return Tree;
}

//递归前序遍历二叉树
void PreOrder(BiTree Tree)
{
if(Tree)
{
printf("%c",Tree->value);
PreOrder(Tree->LChild);
PreOrder(Tree->RChild);
}
}

//递归中序遍历二叉树
void MidOrder(BiTree Tree)
{
if(Tree)
{
MidOrder(Tree->LChild);
printf("%c",Tree->value);
MidOrder(Tree->RChild);
}
}

//递归后序遍历二叉树
void PostOrder(BiTree Tree)
{
if(Tree)
{
PostOrder(Tree->LChild);
PostOrder(Tree->RChild);
printf("%c",Tree->value);
}
}

//非递归前序遍历二叉树
void NPreOrder(BiTree Tree)
{
BiTNode *p;
S = InitLinkStack(S);
p = Tree;
while(p || count != 0)
{
if(p)
{
if(p->RChild)
Push(S,p->RChild);
printf("%c",p->value);
p = p->LChild;
}
else
p = Pop(S);
}
}

//非递归中序遍历二叉树
void NMidOrder(BiTree Tree)
{
//char ch;
BiTNode *p;
S = InitLinkStack(S);
p = Tree;
while(p || count != 0)
{
if(p)
{
Push(S,p);
p = p->LChild;
}
else
{
p = Pop(S);
printf("%c",p->value);
p = p->RChild;
}
}
}

//非递归后序遍历二叉树
void NPostOrder(BiTree Tree)
{
BiTNode *p,*q = NULL;
S = InitLinkStack(S);
p = Tree;
while(p || count != 0)
{
if(p)
{
Push(S,p);
p = p->LChild;
}
else
{
p = S->link->pointer;
if(p->RChild == NULL || p->RChild == q)
{
p = Pop(S);
printf("%c",p->value);
q = p;
p = NULL;
}
else
{
//p = Pop(S);
p = p->RChild;
}
}
}
}
//初始化链栈
LinkStackNode *InitLinkStack(LinkStack top)
{
top = (LinkStackNode *)malloc(sizeof(LinkStackNode));
return top;
}

//进栈操作
void Push(LinkStack top,BiTNode *p)
{
LinkStackNode *temp;
temp = (LinkStackNode *)malloc(sizeof(LinkStackNode));
if(temp)
{
temp->pointer = p;
temp->link = top->link;
top->link = temp;
count++;
}
}

//出栈操作
BiTNode * Pop(LinkStack top)
{
//char ch;
BiTNode *p;
LinkStackNode *temp;
p = (BiTNode *)malloc(sizeof(BiTNode));
temp = top->link;
if(temp)
{
top->link = temp->link;
p = temp->pointer;
free(temp);
count--;
}
return p;
}

④ C语言递归遍历二叉树

voidpreorder(binTreebt){

if(bt!=NULL)//如果到了叶子后(bt=null),就结束函数调用
{
printf("%c",bt->data);//先输出节点内容
preorder(bt->lchild);//调用函数,去遍历当前节点的左子树
preorder(bt->rchild);//上面左树执行完成后,调用函数,去遍历当前节点的右子树
}
}
以上代码,就是递归的特点,和语言无关

⑤ 如何用C语言实现层次遍历二叉树

下面是c语言的前序遍历二叉树的算法,在这里假设的节点元素值假设的为字符型,
说明:算法中用到了结构体,也用到了递归的方法,你看看怎么样,祝你好运!
#include"stdio.h"
typedef
char
elemtype;
typedef
struct
node
//定义链表结构
{
elemtype
data;
//定义节点值
struct
note
*lchild;
//定义左子节点值
struct
note
*rchild;
//定义右节点值
}btree;
preorder(btree
*root)
//前序遍历
{
if(roof!=null)
//如果不是空节点
{
printf("%c\n",root->data);
//输出当前节点
preorder(root->lchild);
//递归前序遍历左子节点
preorder(root->rchild);
//递归前序遍历右子节点
}
return;
//结束
}

⑥ 二叉树遍历(c语言实现)

#include <stdio.h>
#include <malloc.h>

typedef struct node{
int data;
struct node *lchild,*rchild;
}*treetp,tree;
treetp create (treetp t,int c);
void print1(treetp);
void print2(treetp);
void print3(treetp);
int number=0;
void main()
{
treetp t=0,r;
r=create (t,0);
printf("前序排列 :");
print1 (r);
printf("\n中序排列 :");
print2 (r);
printf("\n后序排列 :");
print3 (r);
}

treetp create(treetp t,int c)
{
treetp p,di;
do{
scanf("%d",&c);
if (t==0)
{
t=(treetp)malloc(sizeof(tree));
t->lchild=t->rchild=0;
t->data=c;
}
else
{ p=t;
while(p!=0)
{
di=p;
if(c<(p->data))
p=p->lchild;
else
p=p->rchild;
}
if(c<(di->data))
{
treetp NEWdi=(treetp) malloc(sizeof(tree));
NEWdi->lchild=NEWdi->rchild=0;
NEWdi->data=c;
di->lchild=NEWdi;
}
else
{
treetp NEWdi=(treetp) malloc(sizeof(tree));
NEWdi->lchild=NEWdi->rchild=0;
NEWdi->data=c;
di->rchild=NEWdi;
}
}
++number;
}while(c!=0);
printf("叶子的数量:%d",number);
return t;
}
void print1(treetp t)
{
if (t!=0)
{
printf("%d ",t->data);
print1(t->lchild);
print1(t->rchild);
}
}
void print2(treetp t)
{
if (t!=0)
{
print2(t->lchild);
printf("%d ",t->data);
print2(t->rchild);
}
}
void print3(treetp t)
{
if (t!=0)
{
print3(t->lchild);
print3(t->rchild);
printf("%d ",t->data);
}
}
希望对你有帮助

⑦ C语言二叉树的创建和遍历

我写了一个二叉树 你给看看 一定能行的 我自己用了
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#include "stdlib.h"
#define Max 20 //结点的最大个数
typedef struct BinTNode{
char data;
struct BinTNode *lchild,*rchild;
}BinTNode,*BinTree; //自定义二叉树的结点类型
//定义二叉树的指针
int NodeNum,leaf; //NodeNum为结点数,leaf为叶子数

//==========以广义表显示二叉树==============
void DisTree(BinTree T)
{
if(T)
{
printf("%c",T->data);
if((T->lchild)||(T->rchild))
{
if(T->lchild)
{
printf("%c",'(');
DisTree(T->lchild);
}
if(T->rchild)
{
printf("%c",',');
DisTree(T->rchild);
printf("%c",')');
}
}
}
}
//==========基于先序遍历算法创建二叉树==============
//=====要求输入先序序列,其中加入虚结点"#"以示空指针的位置==========
BinTree CreatBinTree(BinTree T)
{
char ch;
ch=getchar();
if(ch=='#')
T=NULL;
else
{
if(!(T=(BinTNode *)malloc(sizeof(BinTNode))))
printf("Error!");
T->data=ch;
T->lchild=CreatBinTree(T->lchild);
T->rchild=CreatBinTree(T->rchild);
}
return T;
}
//========NLR 先序遍历=============
void Preorder(BinTree T)
{
if(T)
{
printf("%c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
//========LNR 中序遍历===============
void Inorder(BinTree T)
{
if(T){
Inorder(T->lchild);
printf("%c",T->data);
Inorder(T->rchild);
}
}
//==========LRN 后序遍历============
void Postorder(BinTree T)
{
if(T){
Postorder(T->lchild);
Postorder(T->rchild);
printf("%c",T->data);
}
}
//=====采用后序遍历求二叉树的深度、结点数及叶子数的递归算法========
int TreeDepth(BinTree T)
{
int hl,hr,max;
if(T){
hl=TreeDepth(T->lchild); //求左深度
hr=TreeDepth(T->rchild); //求右深度
max=hl>hr? hl:hr; //取左右深度的最大值
NodeNum=NodeNum+1; //求结点数
if(hl==0&&hr==0)
leaf=leaf+1; //若左右深度为0,即为叶子。
return(max+1);
}
else return(0);
}
//====利用"先进先出"(FIFO)队列,按层次遍历二叉树==========
void Levelorder(BinTree T)
{
int front=0,rear=1;
BinTNode *cq[Max],*p; //定义结点的指针数组cq
cq[1]=T; //根入队
while(front!=rear)
{
front=(front+1)%NodeNum;
p=cq[front]; //出队
printf("%c",p->data); //出队,输出结点的值
if(p->lchild!=NULL){
rear=(rear+1)%NodeNum;
cq[rear]=p->lchild; //左子树入队
}
if(p->rchild!=NULL){
rear=(rear+1)%NodeNum;
cq[rear]=p->rchild; //右子树入队
}
}
}
//==========主函数=================
void main()
{
BinTree T,root;
int i,depth;
printf("\n");
printf("输入完全二叉树的先序序列:"); //输入完全二叉树的先序序列,
// 用#代表虚结点,如ABD###CE##F##
root=CreatBinTree(T); //创建二叉树,返回根结点
DisTree(root);
printf("\n");
do //从菜单中选择遍历方式,输入序号。
{
printf("\t********** 菜单 ************\n");
printf("\n");
printf("\t1: 先序遍历\n");
printf("\t2: 中序遍历\n");
printf("\t3: 后序遍历\n");
printf("\t4: 该树的深度,结点数,叶子数\n");
printf("\t5: 层次遍历\n"); //按层次遍历之前,先选择4,求出该树的结点数。
printf("\t0: 退出\n");
printf("\t*******************************\n");
scanf("%d",&i);
//输入菜单序号(0-5)
switch(i)
{
case 1: {printf("Print Bin_tree Preorder: ");
Preorder(root); //先序遍历
}break;
case 2: {printf("Print Bin_Tree Inorder: ");
Inorder(root); //中序遍历
}break;
case 3: {printf("Print Bin_Tree Postorder: ");
Postorder(root); //后序遍历
}break;
case 4: {depth=TreeDepth(root); //求树的深度及叶子数
printf("树深=%d 树总结点数=%d",depth,NodeNum);
printf(" 树叶子数=%d",leaf);
}break;
case 5: {printf("LevePrint Bin_Tree: ");
Levelorder(root); //按层次遍历
}break;
default: exit(1);
}
}while(i>=0&&i<6);
}

兄弟你看看 不懂再往下留言 记得给我的劳动成果一点点奖励哦!!

⑧ 怎么用c语言实现二叉树的遍历

这是用广义表建立二叉树并先序和中序遍历二叉树
#include <stdio.h>
#include <stdlib.h>

#define MaxSize 100

typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}BTNode,*BiTree;

void creategeneralizelist(BiTree *b,char *str)
{
BTNode *St[MaxSize],*p=NULL;
int top=-1,flag,j;
char ch;
for(j=0;(ch=str[j])!='#';j++)
{
switch(ch)
{
case '(':
top++;
St[top]=p;
flag=1;
break;

case ')':
top--;
break;

case ',':
flag=2;
break;

default:
p=(BiTree)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=NULL;
p->rchild=NULL;
if(*b==NULL)
*b=p;
else
{
switch(flag)
{
case 1:
St[top]->lchild=p;
break;

case 2:
St[top]->rchild=p;
break;
}
}
}
}
}

void PreOrder(BiTree T)
{
if(T)
{
printf("%2c",T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}

void InOrder(BiTree T)
{
if(T)
{
InOrder(T->lchild);
printf("%2c",T->data);
InOrder(T->rchild);
}
}

int main(void)
{
BiTree T=NULL;
char str[MaxSize];/*用于保存用户输入的字符*/
printf("please input a string end with #:\n");
scanf("%s",str);
creategeneralize_list(&T,str);
printf("the result ofInOrder BiTree is:\n");
/* PreOrder(T);*/
InOrder(T);
getch();
return 1;

}

⑨ C语言 二叉树遍历

因为没有递归出口,而一开始while(T)会一直为真,进而一直执行 binlitree(T);,会一直递归下去。
//二叉树的先序遍历
int PreOrder(TreeNode *root)
{
if(!root)return 0;
printf("%c",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
return 1;
}

⑩ 求二叉树遍历算法C语言实现的

Status
PreOrderTraverse
(
BiTree
T,
Status
(
*Visit
)
(
TElemType
e
)
)
{
//
采用二叉链表存储结构,Visit
是对数据元素操作的应用函数,先序遍历二叉树
T
的递归算法。
if
(
T
)
{
//

T
不为空
if
(
Visit
(
T->data
)
)
//
调用函数
Visit
if
(
PreOrderTraverse
(
T->lchild,
Visit
)
)
//
递归调用左子树
if
(
PreOrderTraverse
(
T->rchild,
Visit
)
)
return
OK;
//
递归调用右子树
return
ERROR;
}
else
return
OK;
}
//
PreOrderTraverse

热点内容
php成员变量 发布:2024-05-21 16:21:50 浏览:987
眷思量迅雷下载ftp 发布:2024-05-21 16:18:24 浏览:307
斗罗大陆怎么自己建服务器 发布:2024-05-21 16:03:23 浏览:738
河南网通服务器托管云主机 发布:2024-05-21 15:46:00 浏览:170
sqlserver数据库连接数 发布:2024-05-21 15:37:32 浏览:22
安卓一体机如何设置开机直达信源 发布:2024-05-21 15:31:58 浏览:37
纠错码的编译过程 发布:2024-05-21 15:31:56 浏览:240
电脑三千五怎么配置 发布:2024-05-21 15:27:49 浏览:935
买车不能要什么配置 发布:2024-05-21 14:56:20 浏览:427
无锡皮箱密码锁哪里卖 发布:2024-05-21 14:31:03 浏览:472