Data structure binary tree recursive algorithm, pre-order, middle-order, sequential traversal (C language Implementation) __c#

Source: Internet
Author: User
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;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.