Binary tree sequential storage//Here use circular queue to store data//Xin Yang # include <stdio.h> #include <math.h> #include <stdlib.h> #include < String.h> #define MAXQSIZE 5//Maximum queue Length (for circular queues, maximum queue length minus 1) #define MAX_TREE_SIZE 100//two fork Tree maximum node #define Clearbitree Init Bitree//In the sequential storage structure, the two functions are exactly the same typedef char TELEMTYPE;TYPEDEF Telemtype sqbitree[max_tree_size]; Unit No. 0 Storage root node typedef int QELEMTYPE; Telemtype Nil = '; A space-character typedef struct{int level;//node Layer intorder;//This layer ordinal (calculated as full two fork tree)}position;typedef Struct{qelemtype *base; The initialized dynamic allocation storage space is equivalent to an array of int front; The head pointer, if the queue is not empty, points to the queue header element, equivalent to an array subscript int rear; The tail pointer, if the queue is not empty, points to the next position of the tail element of the queue//equivalent to an array subscript}sqqueue;//constructs an empty binary tree T. Because T is a fixed array and does not change, it does not need to & int initbitree (Sqbitree T) {int i;for (i=0;i<max_tree_size;i++) T[i]=nil;//initial value is empty return 1;} void Destroybitree () {///Because Sqbitree is a fixed-length type and cannot be destroyed}//the value (character or integer) of the node in the binary tree in sequence order, construct the order stored two-tree T int createbitree (Sqbitree t) {int i = 0, L;char s[max_tree_size];p rintf ("Please enter the value of the node by the sequence (character), space represents the empty node, node ≤%d:\n", max_tree_size);p rintf ("For example: abcefgh\n"); Gets (s);//input string L = StrlEn (s);//The length of the string for (; i<l;i++)//Assign a string to T {t[i]=s[i];//This node (not empty) no parent and not root, t[(i+1)/2-1] = nil means t[i] no parent if (i!=0 & & t[(i+1)/2-1] = nil && t[i]! = nil) {printf ("non-root nodes with no parents%c\n", T[i]); exit (0);} for (i=l;i<max_tree_size;i++)//assign Null to the node at the back of T T[i]=nil;return 1;} If T is an empty binary tree, then 1 is returned, otherwise 0 int bitreeempty (sqbitree T) {if (T[0]==nil)//root node is empty, then the tree is empty return 1;elsereturn 0;} 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++ ; For ease of calculation doj++;while (I>=pow (2,j))//i > Pow (2, depth-1) && i <= pow (2, depth) return j;//j = depth;} When T is not empty, return the root of T with E, return 1; 0,e no definition int root (sqbitree t,telemtype *e) {if (Bitreeempty (T))//t null return 0;else{*e=t[0];return 1 ;}} Returns the value of the node at position e (layer, this level ordinal) telemtype value (Sqbitree t,position e) {//The sequence number of the layer, this layer is converted to a matrix return t[((int) POW (2,E.LEVEL-1)-1) + (E . Order-1)]; ((int) POW (2,E.LEVEL-1)-1) is the number of nodes of the E.level,//(E.ORDER-1) is the position of this layer}//give a new value to the node at position e (layer, this level), value int Assign (Sqbitree T, Position E,telEmtype value) {//Convert layer, this layer ordinal to matrix ordinal int i = (int) pow (2,E.LEVEL-1) + e.order-2;if (value = nil && t[(i+1)/2-1] = = Nil )//leaves non-null value but both parents are empty return 0;else if (value = = Nil && (t[i*2+1]! = Nil | | T[I*2+2] = Nil)///parents null but with leaves (not empty) return 0; T[i]=value;return 1;} If E is a non-root node of T, return its parent, otherwise return "null" 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; The left child of E}//returned E was not found. If E has no left child, then 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; Did not find e}//return E to the right child. 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; E}//was not found to return E's left brother. If E is the left child of T or no left sibling, 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; No e}//returned to the right brother 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; I didn't find e}//. Move the subtree starting from the J node of Q to a subtree starting from the I node of T//Insertchild () with void Move (Sqbitree q,int j,sqbitree t,int i) {if (q[2*j+1]! = Nil)/ /q The left subtree is not empty Move (Q, (2*j+1), T, (2*i+1)); The Zuozi of J node of Q is moved to the left subtree of the I node of T (q[2*j+2]! = Nil)//Q The right subtree is not empty move (Q, (2*j+2), T, (2*i+2)); The right sub-tree of J node of Q is moved to the right subtree of the I node of T t[i]=q[j]; The J node of Q is shifted to the I node of T Q[j]=nil; Place the J junction of Q}//the left or right subtree of p nodes in T, according to LR 0 or 1. The original left or//right subtree of p node becomes C's right subtree int insertchild (sqbitree t,telemtype p,int lr,sqbitree c) {int j,k,i=0;for (j=0;j< (int) POW (2, Bitreedepth (T)) -1;j++)//Find P ordinal if (t[j]==p)//J for P ordinal break;k=2*j+1+lr; K for P's left or right child's ordinal if (t[k]! = Nil)//p original left or right child not empty Move (t,k,t,2*k+2); Move the subtree starting from the K-node of T to the subtree Move (c,i,t,k) starting from the right subtree of the K-node; Move the subtree starting from the I node of C to the subtree return starting from the K node of T1;} Constructs an empty queue Qint Initqueue (Sqqueue *q) {(*q). base= (Qelemtype *) malloc (maxqsize*sizeof (Qelemtype));//allocate a fixed length of space, equivalent to an array if (! (*q). Base)//Storage allocation failure exit (0);(*q). front= (*q). rear=0;//Initialize subscript return 1;} Insert element E for Q's new team tail element int EnQueue (Sqqueue *q,qelemtype e) {if (*q). rear>=maxqsize) {//Queue full, add a storage unit (*Q). base= (Qelemtype *) ReAlloc ((*q). Base, ((*q). rear+1) *sizeof (Qelemtype)); *Q). Base)//Add unit failed return 0;} * ((*Q). base+ (*Q) rear) =e; (*q). Rear++;return 1;} If the queue is not empty, delete the team header element of Q, return its value with E, and return 1, otherwise return 0 int DeQueue (sqqueue *q,qelemtype *e) {if (*q). front== (*Q). rear)//queue empty return 0;*e= (*q). base[(*Q). Front];(*q). front= (*Q). Front+1;return 1;} Delete the left or right subtree int deletechild (sqbitree t,position p,int LR) {int i;int k=1;//Queue not empty flag Sqqueue q;initqueue (&A) according to LR 1 or 0 MP;Q); Initializes the queue to hold the node i= (int) pow (2,P.LEVEL-1) +p.order-2 to be deleted; The sequence number of the layer, this layer to the matrix if (T[i]==nil)//This node empty return 0;I=I*2+1+LR; The root node of the subtree to be deleted is ordinal in the matrix while (k) {if (T[2*i+1]!=nil)//left node is not empty EnQueue (&q,2*i+1);//The sequence number of the left node of the queue if (T[2*i+2]!=nil)//Right node is not empty EnQ Ueue (&q,2*i+2); The ordinal t[i]=nil of the right node of the queue; Delete this node k=dequeue (&q,&i); Queue not empty}return 1;} Int (*visitfunc) (Telemtype); function variable void pretraverse (Sqbitree t,int e) {//Preordertraverse () Call Visitfunc (T[e]);//Call function Visitfunc first to process root if (t[2*e+1]!= Nil)//left dial hand tree not empty pretraverse (t,2*e+1);//Then handle left subtree if (t[2*e+2]!=nil)//Right subtree not empty pretraverse (t,2*e+2);} The first sequence iterates through T, visit once and only once for each node invocation function. int Preordertraverse (Sqbitree t,int (*visit) (Telemtype)) {visitfunc=visit;if (! Bitreeempty (T))///Tree not empty pretraverse (t,0);p rintf ("\ n"); return 1;} Inordertraverse () calls void Intraverse (Sqbitree t,int e) {if (T[2*e+1]!=nil)//left dial hand tree not empty intraverse (t,2*e+1); Visitfunc (T[e]); if (T[2*e+2]!=nil)//Right subtree not empty intraverse (t,2*e+2);} The middle sequence traverses T, calling the function visit once and only once for each node. int Inordertraverse (Sqbitree t,int (*visit) (Telemtype)) {visitfunc=visit;if (! Bitreeempty (T))///Tree not empty intraverse (t,0);p rintf ("\ n"); return 1;} Postordertraverse () calls 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 not empty posttraverse (t,2*e+2); Visitfunc (T[e]);} //The post-order traversal of T visit once and only once for each node invocation function. int Postordertraverse (Sqbitree t,int (*visit) (Telemtype)) {Visitfunc = Visit;if (! Bitreeempty (T))///Tree not empty posttraverse (t,0);p rintf ("\ n"); return 1;} Sequence traversal binary tree void Levelordertraverse (Sqbitree t,int (*visit) (Telemtype)) {int I=max_tree_size-1,j;while (t[i] = = Nil) i--; /Find the sequence number for the last non-empty node for (j=0;j<=i;j++)//From the root node, traverse the binary tree if (t[j]! = Nil) Visit (T[j]) in sequence; Only the non-empty node printf ("\ n") is traversed;} Output binary tree void Print (Sqbitree T) {int j,k;position p by layer, by this layer ordinal; Telemtype e;for (J=1;j<=bitreedepth (T); j + +) {printf ("Layer%d:", j); for (k=1; K <= Pow (2,j-1); k++) {p.level=j;p.order=k;e=value (t,p); if (E!=nil) printf ("%d:%c", K,e);} printf ("\ n");}} int visit (Telemtype e) {printf ("%c", e); return 0;} int main () {int i,j;position p; Telemtype e; Sqbitree T,s;initbitree (T); Createbitree (T);p rintf ("The tree is empty after establishing a two-fork tree?"). %d (1: Yes 0: NO) tree depth =%d\n ", Bitreeempty (t), bitreedepth (t)), I=root (T,&e), if (i) printf (" Binary tree root:%c\n ", e); elseprintf (" Tree empty, no root \ n ");p rintf (" Sequence traversal binary tree: \ n "); Levelordertraverse (t,visit);p rintf ("Middle sequence traversal binary tree: \ n"); Inordertraverse (T,visIT);p rintf ("post-secondary traversal of binary tree: \ n"); Postordertraverse (t,visit);p rintf ("Please enter the layer number of the node to be modified:") scanf ("%d%d%*c", &p.level,&p.order); E=value (t,p); printf ("The original value of the node to be modified is%c Please enter a new value:", e); scanf ("%c%*c", &e); Assign (t,p,e);p rintf ("Sequential traversal of binary tree: \ n"); Preordertraverse (t,visit);p rintf ("The parents of the node%c are%c, the children are", e,parent (t,e));p rintf ("%c,%c, left and right brothers respectively", Leftchild (T,e), Rightchild (t,e));p rintf ("%c,%c\n", Leftsibling (T,e), rightsibling (t,e)), Initbitree (s);p rintf ("Establishing a tree with a right subtree empty s:\n"); Createbitree (s);p rintf ("Tree S" in tree T, enter the parent node s of tree S in tree T as Left (0) or right (1) subtree: "); scanf ("%c%d%*c ", &e,&j); Insertchild (t,e,j , s); Print (T);p rintf ("Delete subtree, enter the layer number of the subtree node you want to delete the left (0) or right (1) subtree:"); scanf ("%d%d%d%*c", &p.level,&p.order,&j);D Eletechild (T,P,J); Print (T); Clearbitree (T);p rintf ("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:%c\n ", e); elseprintf (" Tree empty, no root \ n "); return 0;}
:
---c language of data structure to achieve sequential storage of two-fork trees