A complete binary tree is a two-fork tree. For a depth of k, there are n nodes of the two-tree, when and only if each of its nodes with a depth of K of the full two-fork tree numbered from 1 to n nodes one by one is called a complete binary tree.
For example, the tree below is a completely binary tree.
Through the picture we can see that it is created from the top to the bottom, from left to right , that is, it is a layer of a layer established.
A complete binary tree has the following basic properties:
For a complete binary tree with n nodes, its arbitrary node I (1<=i<=n), if i = 1, then node i is the root of the two fork tree, with no parents; If I>1, then its parent parent (i) is the node I/2.
If 2i>n; Then node I has no left child (node I is leaf node); Otherwise its left child lchild (i) is node 2i.
If 2i+1>n, then node I no right child; Otherwise the right child rchild (i) is the node 2i+1.
To establish a completely binary tree, the key to deal with the relationship between parents, I made the following summary:
one is to look for a relationship while creating a new node and point it out to the father, and one is to give birth to a child in search of a relationship, that is, to all the nodes in the tree, and then to point out the parent-child relationship between the nodes.
To determine this relationship between trees, we need to use a secondary storage space, such as an array or a stack or a queue, where I implement a sequential queue.
After using the order of the husband and child to find the relationship to generate this binary tree, sir into all the nodes in the tree and put it into the queue, and then point out the father-son relationship, the picture is described as follows:
The specific implementation code is as follows:
#include <stdio.h> #include <stdlib.h> typedef struct BITREE {int data;
struct Bitree *lchild,*rchild;
}bitnode;
function declaration bitnode* createbitree (int *,int);
void Preodertraverse (Bitnode *);
void Destorybitree (Bitnode *);
int main () {Bitnode *t; int a[10] = {1,4,6,7,2,5,3,9,8};
Data for nodes in the tree T = createbitree (a,10);
Preodertraverse (t);
Destorybitree (t);
return 0;
}///////////////////creates a two-fork tree bitnode* createbitree (int *a,int n) {Bitnode *root,*p; Bitnode *bitqueue[n]; Defines a queue to hold a complete binary tree int front=1,rear=1; Initialize Queue////////////////////////////////////////////////Build node merge team for (int i=0; i<n; i++) {if (* (a+i) = = 0
) p = NULL;
else {p = (Bitnode *) malloc (sizeof (Bitnode));
P->data = * (a+i);
P->lchild = P->rchild = NULL; } bitqueue[rear++] = p; //P node, team tail pointer plus 1 if (i==0) root = p; Initialize root node}/////////////////////////////////////////////find the parent-child relationship between nodes while (front! = rear)/ /If the queue is not empty {p = Bitqueue[front];
The node that is currently looking for a relationship if (p! = NULL) {if (2*front <= n) p->lchild = Bitqueue[2*front]; Depending on the full binary tree nature, if 2i<n, (n is the sum of points in the two-fork tree), then the node has a left child of 2i if (2*front+1 <= n) p->rchild = Bitque
UE[2*FRONT+1]; If 2i<n, (n is the sum of the points in the two-fork tree), then the node has a left child of 2i+1} front++;
Team head pointer plus 1, point to next node} return root; }//////////////////pre-order traversal void Preodertraverse (Bitnode *t) {if (t)//If the tree is not empty {printf ("%d", T->da
TA);
Preodertraverse (T->lchild);
Preodertraverse (T->rchild); }}//////////////////destroys the binary tree void Destorybitree (Bitnode *t) {if (t) {if (t->lchild) DestorybItree (T->lchild);
if (t->rchild) Destorybitree (t->rchild);
Free (t);
t = NULL;
}
}