C language implements maximum binary tree and traverses by layer

Source: Internet
Author: User

Title: Given an array of integers with no repeating elements. A maximum binary tree constructed with this array is defined as follows:

    1. The root of a binary tree is the largest element in an array.
    2. Zuozi is the largest binary tree constructed from the left part of the largest value in the array.
    3. The right subtree is the largest binary tree constructed from the right part of the largest value in the array.

Builds the maximum binary tree with the given array and outputs the root node of the tree.

Example 1:

Input: [3,2,1,6,0,5] Input: Returns the root node of this tree:      6    /      3     5    \    /      2  0                  1
 
Analysis: (1) through the examining know through the first order to create a two-tree creation, that is, the creation of the root node, the left subtree, the right number of children. So select the recursive method to create.
Visit (T);
Createlefttree (T->left);
Creatterighttree (t->right);
(2) finds the maximum value in the array, similar to a binary lookup, and uses three cursor low,high,temp to track the position of the low, high, and maximum values in the array to find.
Temp root node
[Low, temp-1] left subtree
[temp+1, High] right number of children

(3) According to the test instructions, the output needs to be the two-fork tree output, that is, by layer traversal, using the queue implementation, the characteristics of the queue FIFO. (1) root node into the queue-"(2) the root node out of the queue and output-" (3) left dial hand tree into the queue-"(4) right sub-number into the queue-" (3)

To define a binary tree node:
typedef struct treenode{
int Val;
struct TreeNode *left;
struct TreeNode *right;
}treenode;
To define a queue link table node:
typedef struct qnode{
Queueptr Val;
Qnode *next;
}qnode,*queueptr;
To define a queue:
typedef struct queue{
Queueptr Front;
Queueptr Rear;
}queue;
Q.front points to the head node of the queue list;
Q.rear points to the tail node of the queue list;
Add nodes after q.rear in queue, and at Q.front Delete point when out of queue
Source:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
int Val;
struct TreeNode *left;
struct TreeNode *right;

};
To define a linked list queue
typedef struct QNODE {
struct TreeNode *val;
struct Qnode *next;
}qnode, *queueptr;
typedef struct QUEUE {
QUEUEPTR Rear; Queue tail pointer
Queueptr Front; Queue header pointer
}queue;
Find the largest number in the array
int searchmaxnum (int *nums, int low, int. high, int *location) {
int max =-1;
int i;
i = low;
while (I <=high) {
if (max< (* (nums + i))
{
Max = * (nums + i);
*location = i;
}
i++;

}
return Max;

}
void Createtree (struct treenode** p, int *count, int * nums, int low, int *location, int.) {
struct treenode* q;
int T,max, t_right, t_left;;

if (Low > High)
{
Return
}
if (count = = 0)
Return
max = Searchmaxnum (nums, Low, high, location);
if (max = =-1)
Return
*p = q = (struct treenode*) malloc (sizeof (struct TreeNode));
(*p)->val = max;
(*p)->left = NULL;
(*p)->right = NULL;
(*count)--;
T_left = (*location)-1;
T_right = (*location) + 1;
Createtree (& (*p)->left), Count, Nums, Low, location, t_left);
Createtree (& (*p)->right), Count, Nums, T_right, location, high);

}

struct treenode* constructmaximumbinarytree (int* nums, int numssize) {
int Maxleft, location,t, T_right, T_left;
int count = Numssize;
int low = 0;
int high = numsSize-1;
struct treenode* p = NULL;
p = (struct treenode*) malloc (sizeof (struct TreeNode));
P->val = Searchmaxnum (nums, Low, High, &location);
count--;
(p)->left = NULL;
(p)->right = NULL;
T_left = location-1;
T_right = location + 1;

Createtree (&p, Searchmaxnum (nums, Low, High, &location), &count, Nums, &low, &location, &high );
Createtree (& (P->left), &count, Nums, Low, &location, t_left);
Createtree (& (P->right), &count, Nums, T_right, &location, High);
return p;
}
Into the queue
void Enqueue (Queue *s, struct TreeNode *t) {
Qnode *p;

p = (Qnode *) malloc (sizeof (Qnode));
S->rear->next = p;

(s)->rear = p;

(s)->rear->val = t;
(s)->rear->next= NULL;

}
void Dequeue (Queue *s, struct TreeNode **t) {
Qnode *p;
(*t) = (s)->front->next->val;
p = (s)->front;
(s)->front= (s)->front->next;
Free (p);
}
void Main () {
int num[8] = {3,2,1,6,0,5,10,20};
struct treenode* p,*q,*t; Defining tree nodes
int count = 8;
Queue sq; Define a queue
t = NULL;
struct treenode* temp = NULL;
Create a maximum binary tree
p = constructmaximumbinarytree (num, count);
printf ("%d", p->val);
Q = p;
HEAD=SQ = NULL;
Sq->head = sq->rear;
Creates a queue header node, which is an empty node.
Sq.front = (queueptr) malloc (sizeof (Qnode));
Sq.rear = Sq.front;
Sq.rear->next = NULL;
root node into queue
if (P! = NULL)
{
Enqueue (&AMP;SQ, p);
}
Count = 8;
Traversing a binary tree by layer
while ((sq.front)! = (sq.rear) && count!=0)
{
Dequeue (&sq,&t);

if (t! = temp)
{
printf ("%d", (t)->val);
if ((t)->left! = NULL)
{
Enqueue (&AMP;SQ, (t)->left);//left child into queue
}
Else
Enqueue (&AMP;SQ, temp);
if ((t)->right! = NULL)
{
Enqueue (&AMP;SQ, (t)->right);
}
Else
Enqueue (&AMP;SQ, temp);
count--;
}
Else
printf ("null");

}
}





















C language implements maximum binary tree and traverses by layer

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.