Source:
#include <stdio.h>
#include <stdlib.h>
// binary sort tree node description
typedef int DataType;
typedef struct Node
{
DataType key;
struct Node *lchild, *rchild;
struct Node * parent; // pointer to parent node
} Node, * pNode;
// create a binary tree using interpolation
void insert(pNode *root, DataType key)
{
pNode p = (pNode)malloc(sizeof(Node));
p->key = key;
p->lchild = NULL;
p->rchild = NULL;
p->parent = NULL;
When if ((* root) == NULL) // empty tree directly as root
{
*root = p;
return;
}
IF ((* root) -> lchild == NULL && (* root) -> Key> Key) // into the current node (* root) left children
{
p->parent = (*root);
(*root)->lchild = p;
return;
}
IF ((* root) -> rchild == NULL && (* root) -> Key <Key) // into the current node (* root) of the right child
{
p->parent = (*root);
(*root)->rchild = p;
return;
}
if ((*root)->key > key)
insert(&(*root)->lchild, key);
else if ((*root)->key < key)
insert(&(*root)->rchild, key);
else
return;
}
void create(pNode *root, DataType *keyArray, int length)
{
int i;
for (i = 0; i<length; i++)
INSERT (the root, keyArray [I]); // binary tree node by node insertion
}
// Find element
pNode search(pNode root, DataType key)
{
if (root == NULL)
return NULL;
else if (key > root->key)
return search(root->rchild, key);
else if (key < root->key)
return search(root->lchild, key);
else
return root;
}
void inordertraverse(pNode root)
{
if (root)
{
inordertraverse(root->lchild);
printf("%4d", root->key);
inordertraverse(root->rchild);
}
}
/////////////////////////////////////////////////////////////
// definition of a queue chain
typedef struct LinkQueueNode
{
int data;
struct LinkQueueNode *next;
}LkQueNode;
typedef struct LkQueue
{
LkQueNode *front, *rear;
LkQue};
// LkQue LQ;
// Initialize the queue chain
void InitQueue(LkQue *LQ)
{
LkQueNode *temp;
temp = (LkQueNode *)malloc(sizeof(LkQueNode));
LQ->front = temp;
LQ->rear = temp;
(LQ->front)->next = NULL;
}
// empty queue chain Analyzing
int EmptyQueue(LkQue *LQ)
{
return LQ->front == LQ->rear;
}
Incoming links // queue
void EnQueue(LkQue *LQ, int x)
{
LkQueNode *temp;
temp = (LkQueNode *)malloc(sizeof(LkQueNode));
temp->data = x;
temp->next = NULL;
(LQ->rear)->next = temp;
LQ->rear = temp;
}
// the queue chain
int outQueue(LkQue *LQ)
{
LkQueNode *temp;
if (EmptyQueue(LQ))
{
printf ( "empty queue!");
return 0;
}
else
{
temp = (LQ->front)->next;
(LQ->front)->next = temp->next;
if (temp->next == NULL)
LQ->rear = LQ->front;
free(temp);
return 1;
}
}
// take the first element of the queue
int Gethead(LkQue *LQ)
{
LkQueNode *temp;
if (EmptyQueue(LQ))
return 0;
else
{
temp = (LQ->front)->next;
return temp->data;
}
}
// hierarchical binary sort tree traversal, achieved by means of an array
void levelorder(pNode bt)
{
pNode q[100];
int front = 0, rear = 0;
pNode p;
if (bt == NULL)
return;
q[rear] = bt;
rear = (rear + 1) % 100;
while (front != rear)
{
p = q[front];
front = (front + 1) % 100;
printf("%4d", p->key);
if (p->lchild)
{
q[rear] = p->lchild;
rear = (rear + 1) % 100;
}
if (p->rchild)
{
q[rear] = p->rchild;
rear = (rear + 1) % 100;
}
}
}
/////////////////////////////////////////////////////////////
void main()
{
pNode root = NULL;
DataType nodeArray[11] = { 15,6,18,3,7,17,20,2,4,13,9 };
int i;
printf ( "data to be searched: \ n");
for (i = 0; i<11; i++)
printf("%4d", nodeArray[i]);
create(&root, nodeArray, 11);
printf("\n\n");
printf ( "in order binary value output node traversal \ n");
inordertraverse(root);
printf("\n\n");
// hierarchical binary sort tree traversal, requires the queue implementation
printf ( "hierarchical binary sort tree traversal \ n");
levelorder(root);
printf ( "\ n Please enter the value you want to find the elements:");
DataType locate;
scanf("%d", &locate);
if (search(root, locate) != NULL)
printf ( "% of these numbers have the number you want to find d \ n", search (root, locate) -> key);
else
printf ( "These data do not the number you want to find \ n.");
system("pause");
}
operation result: