求样板:二叉树建立,显示出创建的结果,先序遍历,并显示遍历的结果。
求样板:二叉树建立,显示出创建的结果,先序遍历,并显示遍历的结果。在线等!
数据结构的初学者,请有经验兄弟指点一把,实在想不明白了…………
谢谢!
------解决方案--------------------
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct btnode
{
char data;
struct btnode *lchild,*rchild;
}btnode,*BiTree;
BiTree PreCreateBiTree(BiTree &root) /*构造二叉链表表示的二叉树T,按先序次序输入二叉树中结点的值(一个字符),?字符表示空树*/
{
char ch;
printf ( "please enter the BiTree in the preorder: ");
fflush(stdin);
scanf( "%c ",&ch);
if (ch== '? ')
{
root=NULL;
return (root);
}
else
{
root=(BiTree)malloc(sizeof(btnode));
root-> data=ch;
PreCreateBiTree(root-> lchild);
PreCreateBiTree(root-> rchild);
return (root);
}
}
void preorder(BiTree root) /*先序遍历二叉树*/
{
if (root!=NULL)
{
printf ( "%c ",root-> data);
preorder(root-> lchild);
preorder(root-> rchild);
}
}
void inorder(BiTree root) /*中序遍历的递归算法*/
{
if (root!=NULL)
{
inorder (root-> lchild);
printf ( "%c ",root-> data);
inorder (root-> rchild);
}
}
void postorder(BiTree root) /*后序遍历递归算法*/
{
if (root!=NULL)
{
postorder (root-> lchild);
postorder (root-> rchild);
printf ( "%c ",root-> data);
}
}
void levelorder(BiTree root) /*层次遍历二叉树的非递归算法*/
{
BiTree queue[MAXSIZE];
BiTree p=root;
int front=0,rear=0;
if (p!=NULL)
{
queue [++rear]=p;
while (front <rear)
{
p=queue[++front];
printf ( "%c ",p-> data);
if (p-> lchild!=NULL)
queue[++rear]=p-> lchild;
if (p-> rchild!=NULL)
queue[++rear]=p-> rchild;
}
}
}
int search_depth(BiTree root) /* 求二叉树的深度*/
{
BiTree queue[MAXSIZE];
BiTree p;
int front=0,rear=0,depth=0,level;
if (root!=NULL)
{
queue[++rear]=root;
level=rear;
while (front <rear)
{
p=queue[++front];
if (p-> lchild!=NULL)
queue[++rear]=p-> lchild;
if (p-> rchild!=NULL)
queue[++rear]=p-> rchild;
if (front==level)
{
depth++;
level=rear;
}
}
}
return (depth);
}
int search_nodenum(BiTree root) /*求二叉树结点数目*/
{
int num=0,top;
BiTree stack[MAXSIZE];
BiTree p;
if (root!=NULL)
{
top=1;
stack[top]=root;
while (top> 0)
{
p=stack[top--];
num++;
if (p-> rchild!=NULL)
stack[++top]=p-> rchild;
if (p-> lchild!=NULL)
stack[++top]=p-> rchild;
}
}
return (num);
}
int search_leaves(BiTree root) /*求二叉树叶子结点数目*/
{
int num=0;
BiTree queue[MAXSIZE];
BiTree p=root;
int front=0,rear=0;
if (p!=NULL)
{
queue [++rear]=p;
while (front <rear)
{
p=queue[++front];
if (p-> lchild==NULL&&p-> rchild==NULL)
num++;
if (p-> lchild!=NULL)
queue[++rear]=p-> lchild;
if (p-> rchild!=NULL)
queue [++rear]=p-> rchild;
}
}
return (num);
}
void main ()
{
int choice,depth,nodenumber,leavesnum;
BiTree root=(BiTree)malloc(sizeof(btnode));
root-> lchild=root-> rchild=0;
printf ( "0 Exit\n ");
printf ( "1 Pre Create a BiTree\n ");
printf ( "2 Printf the preorder\n ");
数据结构的初学者,请有经验兄弟指点一把,实在想不明白了…………
谢谢!
------解决方案--------------------
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct btnode
{
char data;
struct btnode *lchild,*rchild;
}btnode,*BiTree;
BiTree PreCreateBiTree(BiTree &root) /*构造二叉链表表示的二叉树T,按先序次序输入二叉树中结点的值(一个字符),?字符表示空树*/
{
char ch;
printf ( "please enter the BiTree in the preorder: ");
fflush(stdin);
scanf( "%c ",&ch);
if (ch== '? ')
{
root=NULL;
return (root);
}
else
{
root=(BiTree)malloc(sizeof(btnode));
root-> data=ch;
PreCreateBiTree(root-> lchild);
PreCreateBiTree(root-> rchild);
return (root);
}
}
void preorder(BiTree root) /*先序遍历二叉树*/
{
if (root!=NULL)
{
printf ( "%c ",root-> data);
preorder(root-> lchild);
preorder(root-> rchild);
}
}
void inorder(BiTree root) /*中序遍历的递归算法*/
{
if (root!=NULL)
{
inorder (root-> lchild);
printf ( "%c ",root-> data);
inorder (root-> rchild);
}
}
void postorder(BiTree root) /*后序遍历递归算法*/
{
if (root!=NULL)
{
postorder (root-> lchild);
postorder (root-> rchild);
printf ( "%c ",root-> data);
}
}
void levelorder(BiTree root) /*层次遍历二叉树的非递归算法*/
{
BiTree queue[MAXSIZE];
BiTree p=root;
int front=0,rear=0;
if (p!=NULL)
{
queue [++rear]=p;
while (front <rear)
{
p=queue[++front];
printf ( "%c ",p-> data);
if (p-> lchild!=NULL)
queue[++rear]=p-> lchild;
if (p-> rchild!=NULL)
queue[++rear]=p-> rchild;
}
}
}
int search_depth(BiTree root) /* 求二叉树的深度*/
{
BiTree queue[MAXSIZE];
BiTree p;
int front=0,rear=0,depth=0,level;
if (root!=NULL)
{
queue[++rear]=root;
level=rear;
while (front <rear)
{
p=queue[++front];
if (p-> lchild!=NULL)
queue[++rear]=p-> lchild;
if (p-> rchild!=NULL)
queue[++rear]=p-> rchild;
if (front==level)
{
depth++;
level=rear;
}
}
}
return (depth);
}
int search_nodenum(BiTree root) /*求二叉树结点数目*/
{
int num=0,top;
BiTree stack[MAXSIZE];
BiTree p;
if (root!=NULL)
{
top=1;
stack[top]=root;
while (top> 0)
{
p=stack[top--];
num++;
if (p-> rchild!=NULL)
stack[++top]=p-> rchild;
if (p-> lchild!=NULL)
stack[++top]=p-> rchild;
}
}
return (num);
}
int search_leaves(BiTree root) /*求二叉树叶子结点数目*/
{
int num=0;
BiTree queue[MAXSIZE];
BiTree p=root;
int front=0,rear=0;
if (p!=NULL)
{
queue [++rear]=p;
while (front <rear)
{
p=queue[++front];
if (p-> lchild==NULL&&p-> rchild==NULL)
num++;
if (p-> lchild!=NULL)
queue[++rear]=p-> lchild;
if (p-> rchild!=NULL)
queue [++rear]=p-> rchild;
}
}
return (num);
}
void main ()
{
int choice,depth,nodenumber,leavesnum;
BiTree root=(BiTree)malloc(sizeof(btnode));
root-> lchild=root-> rchild=0;
printf ( "0 Exit\n ");
printf ( "1 Pre Create a BiTree\n ");
printf ( "2 Printf the preorder\n ");