C language implemented by binary tree sequential structure

Source: Internet
Author: User

#include "stdio.h"
#include "Stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 100/* Storage space Initial allocation */
#define MAX_TREE_SIZE 100/* Two fork tree maximum nodes */

typedef int STATUS;/* Status is the type of function whose value is the function result status code, such as OK, etc. */
typedef int TELEMTYPE; /* The data type of the tree node is currently tentatively integer */
typedef telemtype SQBITREE[MAX_TREE_SIZE]; /* Unit NO. 0 Storage root node */

typedef struct
{
int level,order; /* Layer of the node, this level ordinal (calculated as full two fork tree) */
}position;

Telemtype nil=0; /* Set integral type to 0 empty */

Status visit (telemtype c)
{
printf ("%d", c);
return OK;
}

/* Constructs an empty binary tree T. Because T is a fixed array and does not change, it does not need & * *
Status Initbitree (Sqbitree T)
{
int i;
for (i=0;i<max_tree_size;i++)
T[i]=nil; /* Initial value is empty */
return OK;
}

/* Enter the value (character or integer) of the node in the binary tree in sequence order, constructing a sequential stored two-tree T */
Status Createbitree (Sqbitree T)
{
int i=0;
printf ("Enter the value of the node by the sequence (integer), 0 for the empty node, and 999 for the end.") Knot Point ≤%d:\n ", max_tree_size);
while (I<10)
{
t[i]=i+1;

if (i!=0&&t[(i+1)/2-1]==nil&&t[i]!=nil)/* This node (not empty) has no parents and is not a root */
{
printf ("Non-root node%d\n with no parents", t[i]);
Exit (ERROR);
}
i++;
}
while (i<max_tree_size)
{
T[i]=nil; /* Assign NULL to the node at the back of T */
i++;
}

return OK;
}

#define Clearbitree initbitree/* In a sequential storage structure, the two functions are exactly the same */

/* Initial condition: two fork tree T exists */
/* Operation Result: Returns True if T is an empty binary tree, otherwise false */
Status bitreeempty (Sqbitree T)
{
if (T[0]==nil)/* Root node is empty, the tree is empty */
return TRUE;
Else
return FALSE;
}

/* Initial condition: two fork tree T exists. Operation Result: Returns the depth of T */
int bitreedepth (Sqbitree T)
{
int i,j=-1;
for (i=max_tree_size-1;i>=0;i--)/* Find last Node */
if (T[i]!=nil)
Break
i++;
Do
j + +;
while (I>=powl (2,J));/* Calculates the power of J for 2. */
Return J;
}

/* Initial condition: two fork tree T exists */
/* Operation Result: When T is not empty, return the root of T with E, return OK; otherwise return error,e undefined */
Status Root (sqbitree t,telemtype *e)
{
if (Bitreeempty (T))/* T empty */
return ERROR;
Else
{
*E=T[0];
return OK;
}
}

/* Initial condition: two fork tree T exists, E is a node in T (position) */
/* Operation Result: Returns the value of the node at position e (layer, this level ordinal) */
Telemtype Value (Sqbitree t,position e)
{
return t[(int) powl (2,E.LEVEL-1) +e.order-2];
}

/* Initial condition: two fork tree T exists, E is a node in T (position) */
/* Operation Result: Assigns a new value to the node at position e (layer, this level number) value */
Status Assign (sqbitree t,position e,telemtype value)
{
int i= (int) powl (2,e.level-1) +e.order-2; /* Convert layer, this layer ordinal to the sequence number of the matrix */
if (value!=nil&&t[(i+1)/2-1]==nil)/* Assign a non-null value to the leaf but the parents are empty */
return ERROR;
else if (value==nil&& (t[i*2+1]!=nil| | T[I*2+2]!=NIL)/* Empty values for parents but leaves (not empty) */
return ERROR;
T[i]=value;
return OK;
}

/* Initial condition: two fork tree T exists, E is a node in T */
/* Operation Result: If E is a non-root node of T, return its parent, otherwise return "empty" */
Telemtype Parent (Sqbitree t,telemtype e)
{
int i;
if (t[0]==nil)/* Empty tree */
return Nil;
for (i=1;i<=max_tree_size-1;i++)
if (t[i]==e)/* Find E */
Return t[(i+1)/2-1];
return Nil; /* Not found E */
}


/* Initial condition: two fork tree T exists, E is a node in T */
/* Operation Result: Returns the left child of E. If E has no left child, return "EMPTY" */
Telemtype leftchild (Sqbitree t,telemtype e)
{
int i;
if (t[0]==nil)/* Empty tree */
return Nil;
for (i=0;i<=max_tree_size-1;i++)
if (t[i]==e)/* Find E */
return t[i*2+1];
return Nil; /* Not found E */
}

/* Initial condition: two fork tree T exists, E is a node in T */
/* Operation Result: Returns the right child of E. If E has no right child, then return "EMPTY" */
Telemtype rightchild (Sqbitree t,telemtype e)
{
int i;
if (t[0]==nil)/* Empty tree */
return Nil;
for (i=0;i<=max_tree_size-1;i++)
if (t[i]==e)/* Find E */
return t[i*2+2];
return Nil; /* Not found E */
}

/* Initial condition: two fork tree T exists, E is a node in T */
/* Operation Result: Returns the left sibling of E. If E is the left child or no left sibling of T, then return "EMPTY" */
Telemtype leftsibling (Sqbitree t,telemtype e)
{
int i;
if (t[0]==nil)/* Empty tree */
return Nil;
for (i=1;i<=max_tree_size-1;i++)
if (t[i]==e&&i%2==0)/* Find E and its ordinal number is even (right child) */
return t[i-1];
return Nil; /* Not found E */
}

/* Initial condition: two fork tree T exists, E is a node in T */
/* Operation Result: Returns the right sibling of E. If E is the right child or no right sibling of T, then return "EMPTY" */
Telemtype rightsibling (Sqbitree t,telemtype e)
{
int i;
if (t[0]==nil)/* Empty tree */
return Nil;
for (i=1;i<=max_tree_size-1;i++)
if (t[i]==e&&i%2)/* Find E and its ordinal number is odd (is left child) */
return t[i+1];
return Nil; /* Not found E */
}

/* Preordertraverse () call */
void Pretraverse (Sqbitree t,int e)
{
Visit (T[e]);
if (t[2*e+1]!=nil)/* Left dial hand tree not empty */
Pretraverse (t,2*e+1);
if (t[2*e+2]!=nil)/* Right subtree is not empty */
Pretraverse (t,2*e+2);
}

/* Initial condition: two fork tree exists */
/* Operation Result: First order traversal T. */
Status Preordertraverse (Sqbitree T)
{
if (! Bitreeempty (T)/* Tree not empty */
Pretraverse (t,0);
printf ("\ n");
return OK;
}

/* Inordertraverse () call */
void Intraverse (Sqbitree t,int e)
{
if (t[2*e+1]!=nil)/* Left dial hand tree not empty */
Intraverse (t,2*e+1);
Visit (T[e]);
if (t[2*e+2]!=nil)/* Right subtree is not empty */
Intraverse (t,2*e+2);
}

/* Initial condition: two fork tree exists */
/* Operation result: Middle sequence traversal T. */
Status Inordertraverse (Sqbitree T)
{
if (! Bitreeempty (T)/* Tree not empty */
Intraverse (t,0);
printf ("\ n");
return OK;
}

/* Postordertraverse () call */
void Posttraverse (Sqbitree t,int e)
{
if (t[2*e+1]!=nil)/* Left dial hand tree not empty */
Posttraverse (t,2*e+1);
if (t[2*e+2]!=nil)/* Right subtree is not empty */
Posttraverse (t,2*e+2);
Visit (T[e]);
}

/* Initial condition: two fork tree T exists */
/* Operation Result: post-post traversal T. */
Status Postordertraverse (Sqbitree T)
{
if (! Bitreeempty (T)/* Tree not empty */
Posttraverse (t,0);
printf ("\ n");
return OK;
}

/* Sequence Traversal binary tree */
void Levelordertraverse (Sqbitree T)
{
int i=max_tree_size-1,j;
while (T[i]==nil)
i--; /* Find the serial number of the last non-empty node */
for (j=0;j<=i;j++)/* From the root node, traverse the binary tree by sequence.
if (T[j]!=nil)
Visit (T[j]); /* Only the non-empty nodes are traversed */
printf ("\ n");
}

/* level by layer, by this layer ordinal output binary tree */
void Print (Sqbitree T)
{
int j,k;
Position p;
Telemtype e;
For (J=1;j<=bitreedepth (T); j + +)
{
printf ("Layer%d:", j);
For (K=1;k<=powl (2,j-1); k++)
{
P.level=j;
P.order=k;
E=value (T,P);
if (E!=nil)
printf ("%d:%d", k,e);
}
printf ("\ n");
}
}


int main ()
{
Status i;
Position p;
Telemtype e;
Sqbitree T;
Initbitree (T);
Createbitree (T);
printf ("After setting up a two-fork tree, is the tree empty?") %d (1: Yes 0: NO) tree depth =%d\n ", Bitreeempty (t), bitreedepth (t));
I=root (t,&e);
if (i)
printf ("Binary tree root:%d\n", e);
Else
printf ("Tree empty, no root \ n");
printf ("Sequence traversal binary tree: \ n");
Levelordertraverse (T);
printf ("Pre-order traversal binary tree: \ n");
Preordertraverse (T);
printf ("middle order traversal binary tree: \ n");
Inordertraverse (T);
printf ("Post-secondary traversal of binary tree: \ n");
Postordertraverse (T);
printf ("Modify the layer number of the node 3 this level ordinal 2. ");
p.level=3;
p.order=2;
E=value (T,P);
printf ("The original value of the node to be modified is%d Please enter a new value:", e);
e=50;
Assign (t,p,e);
printf ("Pre-order traversal binary tree: \ n");
Preordertraverse (T);
printf ("Node%d's parents are%d, left and right children respectively", E,parent (t,e));
printf ("%d,%d, left and right brothers respectively", Leftchild (T,e), Rightchild (t,e));
printf ("%d,%d\n", Leftsibling (T,e), rightsibling (t,e));
Clearbitree (T);
printf ("After clearing the binary tree, is the tree empty?") %d (1: Yes 0: NO) tree depth =%d\n ", Bitreeempty (t), bitreedepth (t));
I=root (t,&e);
if (i)
printf ("Binary tree root:%d\n", e);
Else
printf ("Tree empty, no root \ n");

return 0;
}

C language implemented by binary tree sequential structure

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.