Topic One:
Enter a two-dollar Tree to print each node of the tree from top to bottom, and the same layer to print in left-to-right order.
Input Sample:
Output Sample:
Copy Code code as follows:
Thinking Analysis:
Abstract a binary tree into three nodes: the root node, the left node, and the right node.
First-order traversal can be achieved by row output.
For Zuozi, the entire left subtree is saved as long as the root node is saved. (same as right subtree)
For two subtrees outside the root node, the root node of the left subtree is always accessed first, and then the root node of the right subtree is accessed.
Therefore, you can use queue storage.
Code implementation (GCC compilation passed):
#include "stdio.h" #include "stdlib.h"//Two fork tree node #define SIZE 7//Two fork tree node defines the typedef struct nodes {int data;
struct node *left;
struct node *right;
}btree;
int PrintLine (btree * root);
Btree * Creattree (int a[],int n);
int main (void) {int array[size] = {8,6,10,5,7,9,11};
Btree * ROOT;
Root = Creattree (array,size);
PrintLine (root);
printf ("\ n");
return 0;
int PrintLine (Btree * root) {Btree * queue[size], *p;
int front,rear;
Front = rear = 0;
Rear = (rear+1)%size;
Queue[rear] = root;
Loop end for queue is null while (front!= rear) {//root out queue front = (front + 1)%size;
p = Queue[front];
printf ("%3d", p->data);
The left child is not empty, the team is dissatisfied. if (P->left && ((rear+1)%size!= front)) {rear = (rear+1)%size;
Queue[rear] = p->left; //Right child not empty, team dissatisfied join if (p->right && ((rear+1)%size!= front)) {rear = (rear+1)%size
;
Queue[rear] = p->right; }//Team full, the error if ((rear+1)%size = = Front) {printf ("Insufficient queue space, error ... \ n");
return 0;
} return 1;
Btree * Creattree (int a[],int n) {btree * root, *P,*CU,*PA, based on array creation two-fork sort tree;
int i;
Root = (Btree *) malloc (sizeof (btree));
Root->data = a[0];
Root->left = Root->right =null;
for (i=1;i<n;i++) {p = (Btree *) malloc (sizeof (btree));
P->data = A[i];
P->left = P->right =null;
Cu = root;
while (CU) {pa = cu;
if (Cu->data > P->data) cu = cu->left;
else Cu = cu->right;
} if (Pa->data > P->data) pa->left = p;
else pa->right = p;
return root;
}
Topic Two:
Enter an array of integers to determine if the array is the result of a subsequent traversal of a two-dollar lookup tree.
Returns False if it returns true.
For example, enter 5, 7, 6, 9, 11, 10, 8, because this sequence of integers is a sequential traversal of the following tree:
So returns True.
If you enter 7, 4, 6, 5, no tree's subsequent traversal of the result is this sequence, so return false.
Ideas:
The characteristics of the binary lookup: Zuozi Each value is small root, the right subtree of each value is greater than the
Subsequent traversal characteristics: The last one is the root, convenient order, left and right. Recursion
Well, the summary can be obtained:
The last one is the root, the first consecutive number of small root is the node of the Zuozi, then several consecutive root of the right subtree of the node (the left and the subtree may be empty), and then recursive description.
The code is described as follows (GCC compiles through):
#include "stdio.h"
#include "stdlib.h"
int ispostorderresult (int a[],int n);
int helper (int a[],int s,int e);
int main (void)
{
int a[7] = {5,7,6,9,11,10,8};
int b[4] = {7,4,6,5};
int tmp;
TMP = Ispostorderresult (a,7);
printf ("%d", TMP);
return 0;
}
int ispostorderresult (int a[],int N)
{return
helper (a,0,n-1);
}
int helper (int a[],int s,int e)
{
int i,j,root;
if (s = = e) return
1;
For (i=0;i<e && a[i]<a[e];i++);
if (i!= 0 && helper (a,s,i-1) = = 0) return
0;
For (j=i;j<e && a[j]>a[e];j++);
if (j==e && helper (a,i,j-1) = = 1) return
1;
else return
0;
}
Topic Three:
Enter a two-yuan lookup tree that converts the tree to its mirror image, in which the node of the Zuozi is greater than the node of the right subtree in the converted two-bit lookup tree.
The recursive and cyclic methods are used to complete the mirroring transformation of the tree.
For example, enter:
Output:
Analysis:
Recursive programming is relatively simple
Access a node, as long as it is not empty, exchange the left and right child, and then recursively to the left and the child tree respectively.
Non-recursive essence is the need for us to manually complete the stack, the idea is consistent
The code is as follows (GCC compiles through):
#include "stdio.h" #include "stdlib.h" #define MAXSIZE 8 typedef struct Node {int data;
struct node * left;
struct node * right;
}btree; void Swap (btree * * * x,btree y);/swap left and right child void mirror (btree * root);//recursive implementation function declaration void mirroriteratively (Btree * root);//Not recursive The present function declares Btree * creattree (int a[],int n);//Create two fork tree (produce two fork sort tree) void Iorder (btree * root);//in sequence traversal view result int main (void) {int Arra
Y[maxsize] = {5,3,8,7,2,4,1,9};
Btree * ROOT;
Root = Creattree (array,maxsize);
printf ("Before: \ n");
Iorder (root);
printf ("\ n transformed: \ n");//two times transform, consistent mirror (root);
mirroriteratively (root);
Iorder (root);
printf ("\ n");
return 0;
} void Swap (btree * * x,btree * * y) {btree * t = * x;
* x = * y;
* y = t;
} void Mirror (Btree * root) {if (root = NULL)/end condition return; Swap (& (Root->left),& (root->right));//Exchange Mirror (ROOT->LEFT);//Zuozi recursive Mirror (root->right);//
Right subtree recursive} void mirroriteratively (Btree * root) {int top = 0;
Btree * t; BTree * stack[maxsize+1];
if (root = = NULL) return;
Manual pressure stack, stack stack[top++] = root;
while (top!= 0) {t = Stack[--top];
Swap (& (T->left),& (t->right));
if (t->left!= NULL) stack[top++] = t->left;
if (t->right!= NULL) stack[top++] = t->right;
}///produce two fork sort tree btree * creattree (int a[],int n) {btree * root, *p,*cu,*pa;
int i;
Root = (Btree *) malloc (sizeof (btree));
Root->data = a[0];
Root->left = Root->right =null;
for (i=1;i<n;i++) {p = (Btree *) malloc (sizeof (btree));
P->data = A[i];
P->left = P->right =null;
Cu = root;
while (CU) {pa = cu;
if (Cu->data > P->data) cu = cu->left;
else Cu = cu->right;
} if (Pa->data > P->data) pa->left = p;
else pa->right = p;
return root; }//in-order traversal void Iorder (Btree * root) {if (root) {Iorder (root->left);
printf ("%3d", root->data);
Iorder (Root->right);
}
}