数据结构 c语言版二叉树(1) 建立一棵含有n个结点的二叉树,采用二叉链表存储;

来源:百度知道 编辑:UC知道 时间:2024/05/10 17:48:46
题目:二叉树操作验证
1.实验目的
(1)掌握二叉树的逻辑结构
(2)掌握二叉树的二叉链表存储结构;
(3)掌握基于二叉链表存储的二叉树的遍历操作的实现。
2.实验内容
(1) 建立一棵含有n个结点的二叉树,采用二叉链表存储;
(2) 前序(或中序、后序)遍历该二叉树。

#include<stdio.h>
#include<stdlib.h>
typedef struct node *tree_pointer;
struct node{
char ch;
tree_pointer left_child,right_child;
};
tree_pointer root=NULL;
tree_pointer create(tree_pointer ptr)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
ptr=NULL;
else{
ptr=(tree_pointer)malloc(sizeof(node));
ptr->ch=ch;
ptr->left_child=create(ptr->left_child);
ptr->right_child=create(ptr->right_child);
}
return ptr;
}
void preorder(tree_pointer ptr)
{
if(ptr){
printf("%c",ptr->ch);
preorder(ptr->left_child);
preorder(ptr->right_child);
}
}
void inorder(tree_pointer ptr)
{
if(ptr){
inorder(ptr->left_child);
printf("%c",ptr->ch);
inorder(ptr->right_child);
}
}
void postorder(tree_pointer pt