Some of the initialization code that is commonly used in the two-fork tree structure
#include #include typedef struct node{int data;
Node *leftchild;
Node *rightchild;
}node;
/* Initializes a binary tree sort tree.
*/void Initbinarytree (Node**root,int elem) {*root= (node*) malloc (sizeof (Node)); if (!) (
*root)) {printf ("Memory Allocation for Root failed.\n");
Return
} (*root)->data=elem;
(*root)->leftchild=null;
(*root)->rightchild=null;
/* Inserts a node into the binary tree sort tree.
*/void Insertnode (node *root,int elem) {node *newnode=null;
Node *p=root,*last_p=null;
Newnode= (node*) malloc (sizeof (Node));
if (!newnode) {printf ("Memory Allocation for NewNode failed.\n");
Return
} newnode->data=elem;
newnode->leftchild=null;
newnode->rightchild=null;
while (null!=p) {last_p=p;
if (newnode->datadata) {p=p->leftchild;
else if (newnode->data>p->data) {p=p->rightchild;
else {printf ("Node to is inserted has existed.\n");
Free (newnode);
Return
}} p=last_p;
if (newnode->datadata) {p->leftchild=newnode; else {P->righTchild=newnode;
}/* Create a binary tree sort tree.
*/void Creatbinarysearchtree (Node **root,int data[],int num) {int i;
For (I=0;i {if (null==*root) {initbinarytree (root,data[i));
else {Insertnode (*root,data[i]);
}
}
}
Constructing binary tree based on sequence sequence and middle sequence
function definition
BT Rebuildtree (Char *pre, char *in, int len);
Parameters:
* Pre: A string array of the results of the pre-sequence traversal
* In: Sequence traversal of the result string array
Len: The length of the tree
For example:
Pre-sequence traversal result: a b c d e F g h
Middle sequence traversal Result: c B e D f a G H
Algorithm idea
- Recursive thought, the recursive termination condition is the length of the tree Len = = 0
- Finds the first character of the previous ordinal group in the array in the sequence traversal, the position of the index in the ordinal group in. If it cannot be found, the sequence traversal array and the sequence traversal array are problematic, prompting for error messages, and exiting the program; After finding index, create a new binary tree node T,t->item = * Pre, and then recursively begged T's left child and have children
- Recursive left child: void Rebuildtree (pre + 1, in, index)
- Recursive right Child: void Rebuildtree (pre + (index + 1), in + (index + 1), Len-(index + 1))
Implementation code (c language Edition)
/**
* Description: Construct binary tree/
bt Rebuildtree (Char *pre, char *in, int len)
{
BT t
according to the preface and middle order; if (len <= 0)
{
//recursive terminating
t = NULL;
} else
{
///recursive principal
int index = 0;
while (Index < len && * (PRE)!= * (in + index))
{
index + +;
}
if (index >= len)
{
printf ("forward traversal or sequence traversal array problematic!\n");
Exit ( -1);
}
t = (struct Bintree *) malloc (sizeof (struct bintree));
T->item = *pre;
T->lchild = Rebuildtree (pre + 1, in, index);
T->rchild = Rebuildtree (pre + (index + 1), in + (index + 1), Len-(index + 1));
return t;
}
constructing binary tree based on sequence sequence and sequential order
function definition
/**
* in sequence, sequential sequence construction of binary tree
/btree* rebuildtree (char *order, char *post, int len);
Algorithm idea
Sequence of sequences: C, B, E, D, F, A, H, G, J, I
Sequential sequence: C, E, F, D, B, H, J, I, G, A
Recursive thinking:
- According to the characteristics of the subsequent traversal, know that the next step to traverse the last node as the root node, that is a
- In the observed sequence traversal, a left CBEDF is a left child tree node, a posterior hgji is a right subtree node
- Then recursively constructs a Saozi subtree.
Implementation code (c code)
/** * According to the order and sequence of sequential construction of binary tree * (PS: Last night to participate in Ali written examination, until the last said that can be free of written test direct interview, today is estimated to be based on school screening, Haha, in order to this * also have to participate in Ali written examination, can not let
Nclude <stdio.h> #include <stdlib.h> #include <string.h> int n;
typedef struct BTREE {struct btree *lchild;
struct Btree *rchild;
char data;
} btree;
/** * in sequence, sequential sequence construction of binary tree/btree* rebuildtree (char *order, char *post, int len) {btree *t;
if (len <= 0) {return NULL;
else {int index = 0;
while (Index < len && * (post + len-1)!= * (Order + index)) {index + +;
} t = (Btree *) malloc (sizeof (btree));
T->data = * (order + index);
T->lchild = Rebuildtree (order, post, index);
T->rchild = Rebuildtree (order + index + 1, post + index, Len-(index + 1));
} return t;
/** * Pre-order traversal binary tree/void Pretraverse (Btree *t) {if (t) {printf ("%c", t->data);
Pretraverse (T->lchild); Pretraverse (T->rchild);
int main (void) {int i;
Char *post, *order;
Btree *t;
while (scanf ("%d", &n)!= EOF) {post = (char *) malloc (n);
Order = (char *) malloc (n);
GetChar ();
for (i = 0; i < n; i + +) scanf ('%c ', order + i);
GetChar ();
for (i = 0; i < n; i + +) scanf ("%c", post + i);
t = rebuildtree (order, post, N);
Pretraverse (t);
printf ("\ n");
Free (post);
Free (order);
return 0;
}