Sequence and post-order construction of two-fork tree, post-order and middle sequence construction of two-fork tree

Source: Internet
Author: User
Tags printf

1. Pre-order and post-construction two-fork tree

#include <stdio.h> typedef struct binode{int data;
struct Binode *lchild, *rchild;

}bitree;
int g_index = 0;
/* Preorder for left dial hand tree inorder is the total number of right subtree num for the Saozi right subtree of the current node.

	*/bitree* Create (int *preorder, int *inorder, int num) {if (num = = 0) return NULL; 
	int i = 0,index;
			for (i = 0; i < num; i++) {if (preorder[0] = = Inorder[i]) {index = i;
		Break
	}} bitree *node = (bitree*) malloc (sizeof (bitree));	
	Node->data = preorder[0];
	int lnum = index;

	int rnum = num-index-1;
	if (!lnum) node->lchild = NULL;
	else{Node->lchild = Create (& (Preorder[1]), & (Inorder[0]), lnum);
	} if (!rnum) node->rchild = NULL;
	else{Node->rchild = Create (&preorder[index+1], & (Inorder[index+1]), rnum);	
} return node;
        } void Preorder_traverse (Bitree *tree) {if (Tree! = NULL) {preorder_traverse (tree->lchild);
        Preorder_traverse (Tree->rchild);
    printf ("data=%d\n", tree->data); }} void Main (int argc, Char**ARGV) {int pre[8]={1,2,4,7,3,5,6,8};

    int in[8]={4,7,2,1,5,3,8,6};
    Bitree *tree = Create (&pre, &in, 8);
Preorder_traverse (tree); }

2. Post-order and middle sequence construction two fork tree

#include <stdio.h> typedef struct binode{int data;
struct Binode *lchild, *rchild;


}bitree;

	bitree* Create (int *lastorder, int *inorder, int num) {if (num = = 0) return NULL;
	int i = 0,index;
		for (i = 0; i < num; i++) {//if (inorder[i] = = Lastorder[num-1]) {//points to the last element.
			if (inorder[i] = = *lastorder) {index = i;
		Break
	}} bitree *node = (bitree*) malloc (sizeof (bitree));
	Node->data = *lastorder;
	int lnum = index; 	

	int rnum = num-index-1;
	printf ("data=%d,l=%d,r=%d\n", node->data,lnum,rnum);
	if (Lnum = = 0) Node->lchild = NULL;
	else{Node->lchild = Create (lastorder-rnum-1, & (Inorder[0]), lnum);
	} if (rnum = = 0) Node->rchild = NULL;
	else{Node->rchild = Create (--lastorder, & (Inorder[index+1]), rnum);
} return node;	
		} void Preorder_traverse (Bitree *tree) {if (Tree! = NULL) {printf ("data=%d\n", tree->data);
		Preorder_traverse (Tree->lchild);
	Preorder_traverse (Tree->rchild); }} void Main(int argc, char **argv)
	{//int pre[8]={1,2,4,7,3,5,6,8};
	int last[8]={7,4,2,5,8,6,3,1};

	int in[8]={4,7,2,1,5,3,8,6};
	Bitree *tree = Create (&last[7], &in, 8);
Preorder_traverse (tree); }


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.