experimental purposes
1. Grasp the representation and realization of the binary tree.
2, master the definition of binary tree, create, traverse and other basic operations to achieve.
3, familiar with the design and implementation of the recursive algorithm of the binary tree depth. Experimental Content
Problem Description : The known binary tree T, the sequential storage structure, binary chain table storage structure to achieve the depth of the binary tree, and the two-fork tree respectively in the middle sequence traversal.
Requirements:
1, the binary tree, respectively, using sequential or binary linked list storage.
2, the data type convention in the tree is integral type
3, according to the first sequence to create two-fork tree T, using the recursive algorithm to find the depth of the binary tree, and the two-fork tree respectively for the first order, the sequence, the subsequent traversal. test Data
1, input sequence:-+aøø*bøø-cøødøø/eøøføø to create two fork tree;
Output: Depth: 5
Pre-sequence sequence:-+a*b-cd/ef
Sequence of sequences: a+b*c-d-e/f
Sequential sequence: abcd-*+ef/-
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct binode{char data;
struct Binode *lchild,*rchild;
}bitnode,*bitree;
void Initbitree (Bitree T) {t=null;}
void Create (Bitree &t) {char ch[2];
scanf ("%s", &ch[0]);
if (strcmp (ch, "@") ==0) {t=null;
}else{t= (bitree) malloc (sizeof (Binode));
t->data=ch[0];
Create (T->lchild);
Create (T->rchild);
} void Visit (char c) {printf ("%c", c);}
void Preordertraverse (Bitree t) {if (t) {visit (t->data);
Preordertraverse (T->lchild);
Preordertraverse (T->rchild);
} void Inordertraverse (Bitree t) {if (t) {inordertraverse (t->lchild);
Visit (T->data);
Inordertraverse (T->rchild);
} void Postordertraverse (Bitree t) {if (t) {postordertraverse (t->lchild);
Postordertraverse (T->rchild);
Visit (T->data);
an int depth (Bitree T) {int ldeep,rdeep; if (!
T) return 0; else{ldeep=depth (T->lchiLD);
Rdeep=depth (T->rchild);
Return ldeep>rdeep?ldeep+1:rdeep+1;
int main () {Bitree T;
int deep;
Initbitree (T);
printf ("Please enter a sequence \ n");
Create (T);
Deep=depth (T);
printf ("\ n Depth:%d\n Two-tree construction completed", deep);
printf ("\ n first traverse binary tree:");
Preordertraverse (T);
printf ("\ n sequence traversal binary tree:");
Inordertraverse (T);
printf ("\ n subsequent traversal of the two-fork Tree:");
Postordertraverse (T);
return 0;
}