二叉树创建及遍历(递归遍历)
2010-03-20 22:02
330 查看
#include <stdio.h>
#include <stdlib.h>
/*--------------------------------------------
//二叉树节点定义,
data:数据
left:左指针
right:右指针
--------------------------------------------*/
typedef struct binNode
{
int data;
binNode* left;
binNode* right;
}*bintree, bintreeNode;
binNode* createtree(binNode*);
void destroytree(binNode*);
binNode* preordertravel(binNode*,int);
void inordertravel(binNode*);
void postordertravel(binNode*);
void leveltravel(binNode* p);
bintree find(binNode*, int item);
void main()
{
bintree root;
root = 0;
root = createtree(root);
printf("前序:/n");
bintree temp = preordertravel(root, 8);
if (temp == 0)
{
printf("no item/n");
}
else
printf("/n%d/n", temp->data);
printf("/n中序:/n");
inordertravel(root);
printf("/n后序:/n");
postordertravel(root);
printf("/n后序:/n");
leveltravel(root);
destroytree(root);
}
//创建二叉树
binNode* createtree(bintree p)
{
int c;
bintree temp;
bintree di;
bintree t;
do
{
scanf("%d", &c);
temp = (bintree)malloc(sizeof(bintreeNode));
if (temp == 0)
{
printf("memory error");
exit(1);
}
temp->left = 0;
temp->right = 0;
temp->data = c;
if (p == 0)
{
p = temp;
}
else
{
di = p;
while (di != 0)
{
t = di;
if (c < di->data)
{
di = di->left;
}
else
{
di = di->right;
}
}
if (c < t->data)
{
t->left = temp;
}
else
{
t->right = temp;
}
}
} while(c != 0);
return p;
}
//删除二叉树
void destroytree(binNode* root)
{
bintree temp =root;
bintree t;
if (temp != 0)
{
destroytree(temp->left);
destroytree(temp->right);
free(temp);
temp = NULL;
}
}
//查找item,返回节点值为item的指针
bintree find(binNode*p, int item)
{
bintree temp = p;
if (p == NULL)
{
printf("empty tree/n");
exit(0);
}
while ( temp != NULL )
{
if ( temp->data == item)
{
return temp;
break;
}
}
return NULL;
}
//前序遍历(中、左、右),在前序遍历过程中查找某个值是否存在
bintree preordertravel(bintree p, int item)
{
bintree temp = 0;
if (p != 0)
{
if (p->data == item)
{
temp = p;
}
printf("%d ",p->data);
preordertravel(p->left, item);
preordertravel(p->right,item);
}
return temp;
}
//中序遍历(左、中、右)
void inordertravel(bintree p)
{
if (p != 0)
{
inordertravel(p->left);
printf("%d ", p->data);
inordertravel(p->right);
}
}
//后续遍历(左、右、中)
void postordertravel(bintree p)
{
if (p != 0)
{
postordertravel(p->left);
postordertravel(p->right);
printf("%d ", p->data);
}
}
//层序遍历
void leveltravel(binNode* p)
{
binNode* t[20];
int rear = 0;
int front = 0;
if ( p )
{
++rear;
t[rear] = p;
}
while ( front != rear )
{
front++;
printf("%d ",t[front]->data);
if (t[front]->left != NULL)
{
rear++;
t[rear] = t[front]->left;
}
if (t[front]->right != NULL)
{
rear++;
t[rear] = t[front]->right;
}
}
}
binNode* initree(binNode* root, int a[], int n)
{
int i;
for ( i = 0; i < n; i++)
{
binNode* temp;
temp = (binNode*)malloc(sizeof(binNode));
if (temp == NULL)
{
return NULL;
}
temp->data = a[i];
temp->left = NULL;
temp->right = NULL;
}
}
#include <stdlib.h>
/*--------------------------------------------
//二叉树节点定义,
data:数据
left:左指针
right:右指针
--------------------------------------------*/
typedef struct binNode
{
int data;
binNode* left;
binNode* right;
}*bintree, bintreeNode;
binNode* createtree(binNode*);
void destroytree(binNode*);
binNode* preordertravel(binNode*,int);
void inordertravel(binNode*);
void postordertravel(binNode*);
void leveltravel(binNode* p);
bintree find(binNode*, int item);
void main()
{
bintree root;
root = 0;
root = createtree(root);
printf("前序:/n");
bintree temp = preordertravel(root, 8);
if (temp == 0)
{
printf("no item/n");
}
else
printf("/n%d/n", temp->data);
printf("/n中序:/n");
inordertravel(root);
printf("/n后序:/n");
postordertravel(root);
printf("/n后序:/n");
leveltravel(root);
destroytree(root);
}
//创建二叉树
binNode* createtree(bintree p)
{
int c;
bintree temp;
bintree di;
bintree t;
do
{
scanf("%d", &c);
temp = (bintree)malloc(sizeof(bintreeNode));
if (temp == 0)
{
printf("memory error");
exit(1);
}
temp->left = 0;
temp->right = 0;
temp->data = c;
if (p == 0)
{
p = temp;
}
else
{
di = p;
while (di != 0)
{
t = di;
if (c < di->data)
{
di = di->left;
}
else
{
di = di->right;
}
}
if (c < t->data)
{
t->left = temp;
}
else
{
t->right = temp;
}
}
} while(c != 0);
return p;
}
//删除二叉树
void destroytree(binNode* root)
{
bintree temp =root;
bintree t;
if (temp != 0)
{
destroytree(temp->left);
destroytree(temp->right);
free(temp);
temp = NULL;
}
}
//查找item,返回节点值为item的指针
bintree find(binNode*p, int item)
{
bintree temp = p;
if (p == NULL)
{
printf("empty tree/n");
exit(0);
}
while ( temp != NULL )
{
if ( temp->data == item)
{
return temp;
break;
}
}
return NULL;
}
//前序遍历(中、左、右),在前序遍历过程中查找某个值是否存在
bintree preordertravel(bintree p, int item)
{
bintree temp = 0;
if (p != 0)
{
if (p->data == item)
{
temp = p;
}
printf("%d ",p->data);
preordertravel(p->left, item);
preordertravel(p->right,item);
}
return temp;
}
//中序遍历(左、中、右)
void inordertravel(bintree p)
{
if (p != 0)
{
inordertravel(p->left);
printf("%d ", p->data);
inordertravel(p->right);
}
}
//后续遍历(左、右、中)
void postordertravel(bintree p)
{
if (p != 0)
{
postordertravel(p->left);
postordertravel(p->right);
printf("%d ", p->data);
}
}
//层序遍历
void leveltravel(binNode* p)
{
binNode* t[20];
int rear = 0;
int front = 0;
if ( p )
{
++rear;
t[rear] = p;
}
while ( front != rear )
{
front++;
printf("%d ",t[front]->data);
if (t[front]->left != NULL)
{
rear++;
t[rear] = t[front]->left;
}
if (t[front]->right != NULL)
{
rear++;
t[rear] = t[front]->right;
}
}
}
binNode* initree(binNode* root, int a[], int n)
{
int i;
for ( i = 0; i < n; i++)
{
binNode* temp;
temp = (binNode*)malloc(sizeof(binNode));
if (temp == NULL)
{
return NULL;
}
temp->data = a[i];
temp->left = NULL;
temp->right = NULL;
}
}
相关文章推荐
- 二叉树的递归创建和遍历
- 数据结构与算法分析学习笔记--第四章(二叉树:创建、递归遍历、非递归遍历、根据数据删除结点等)
- 二叉树(2)二叉树创建的3种方法,二叉树的递归遍历,二叉树的销毁
- 二叉树的先序递归创建和遍历
- 二叉树的创建和递归遍历
- 二叉树 前序遍历的非递归实现 中序遍历的非递归实现 后序遍历的非递归实现 创建二叉树
- 二叉树的创建遍历-递归与非递归
- 二叉树创建、遍历的递归和非递归实现
- 数据结构复习:链式二叉树创建和递归遍历
- 二叉树的递归创建,先序(中序、后序)递归遍历二叉树
- 递归方法实现二叉树的创建,遍历
- 不疯魔,不成活!——二叉树的创建、遍历(递归实现)等操作。
- 二叉树的递归创建和递归遍历3.1
- 二叉树创建及遍历算法(递归及非递归)
- 二叉树的创建与前序中序后序遍历的递归实现
- 二叉树的创建与遍历(递归版本)
- 二叉树与其基本递归操作:创建、遍历、特征量计算等
- 九度OJ 1184 二叉树递归创建遍历
- 二叉树的创建与遍历(递归)
- 二叉树(2)二叉树创建的3种方法,二叉树的递归遍历,二叉树的销毁