// Sequential storage representation of Binary Trees # define max_tree_size 100 // maximum number of Binary Tree nodes typedef telemtype sqbitree [max_tree_size] // unit 0 stores the root node sqbitree Bt;
Sequential Storage Structure: uses a set of sequential storage units to store node elements on a Complete Binary Tree from top to bottom, from left to right, the node element of the Complete Binary Tree is stored in the component defined as a one-dimensional array named I-1. This storage structure is only applicable to full Binary Trees.
# Include <stdio. h>/* EOF (= ^ Z or F6), null */# include <math. h>/* floor (), Ceil (), ABS () */# define OK 1 # define error 0 # define true 1 # define false 0 typedef int status; /* status is the function type, and its value is the function result status code, such as OK */# If chartypedef char telemtype; telemtype nil = ''; /* set the struct type to null with a space character */# else typedef int telemtype; telemtype nil = 0; /* set the integer to 0 as null */# endif # define max_tree_size 100/* Maximum number of nodes of the binary tree */typedef telemtype sqbitree [max_tre E_size];/* unit 0 storage root node */status initbitree (sqbitree t) {/* construct an empty binary tree T. Because T is a fixed array and does not change, you do not need to & */int I; for (I = 0; I <max_tree_size; I ++) T [I] = nil; /* The initial value is blank */Return OK;} status createbitree (sqbitree t) {/* enter the node values (sorted or integer) in the binary tree in sequence ), construct a binary tree T */INT I = 0; # If char int L; char s [max_tree_size]; printf ("Enter the node value (character) according to the sequence ), space indicates an empty node. The number of knots is less than or equal to % d: \ n ", max_tree_size); gets (s);/* input string */L = strlen (s ); /* evaluate the length of the string */For (; I <L; I ++)/* assign the string to T */{T [I] = s [I]; if (I! = 0 & T [(I + 1)/2-1] = nil & T [I]! = Nil)/* this node (not empty) has no parent and is not the root */{printf ("non-root node % C \ n", t [I]); exit (error) ;}}for (I = L; I <max_tree_size; I ++) /* assign the null value to the node next to T */T [I] = nil; # else printf ("press the value of the sequence input node (integer), and 0 indicates the null node, enter 999 and end. Knots ≤ % d: \ n ", max_tree_size); While (1) {scanf (" % d ", & T [I]); if (T [I] = 999) break; if (I! = 0 & T [(I + 1)/2-1] = nil & T [I]! = Nil)/* this node (not empty) has no parent and is not the root */{printf ("non-root node % d \ n", t [I]); exit (error) ;} I ++ ;}while (I <max_tree_size) {T [I] = nil; /* assign the null value to the node after T */I ++;} # endif Return OK;} status bitreeempty (sqbitree t) {/* initial condition: binary Tree t exists * // * operation result: If t is null Binary Tree, true is returned; otherwise, false */If (T [0] = nil) /* If the root node is empty, the tree is empty */return true; else return false;} int bitreedepth (sqbitree t) {/* initial condition: binary tree T exists. Operation results: returns the depth of T */INT I, j =-1; for (I = MAX_TREE_SIZE-1; I> = 0; I --) /* locate the last node */If (T [I]! = Nil) break; I ++;/* For ease of computing */Do J ++; while (I> = POW (2, j )); // calculate the J power of 2 and return the double value. Because I <= POW (2, j)-1, I <POW (2, J ). return J; // return the depth of the Binary Tree} status root (sqbitree T, telemtype * E) {/* initial condition: binary tree T exists * // * Operation Result: When t is not empty, use E to return the root of T and Return OK; otherwise, error is returned. e is not defined */If (bitreeempty (t)/* t null */return error; else {* E = T [0]; // return the root of T, that is, Return OK;} void main () {status I; telemtype E; sqbitree t; initbitree (t); createbitree (t); printf ("is the tree empty after a binary tree is created? % D (1: YES 0: No) depth of the tree = % d \ n ", bitreeempty (t), bitreedepth (t); I = root (t, & E); if (I) printf ("the root of the binary tree is % d \ n", e); else printf ("tree blank, rootless \ n ");}