The binary tree with the known depth of H has been stored in the array BT [^ h-1], please write a non-recursive algorithm to generate the binary chain table structure of the binary tree. Set the link node in the binary linked list to (lchild, Data, rchild). The pointer to the link node where the root node is located is given by T.
Design Concept: Use the queue structure to first establish a root node to join the queue. When the queue is not empty, perform the following steps cyclically: when the first node of the queue is out of the queue, if there is a left child, create the left child node and join the queue. If there is a right child, create the right child node and join the queue.
Code:
Typedef struct {bitree Bt; int num;} tnodetnode Q [maxsize]; void creat (bitree T, elemtype BT [], int h) {tnode TQ; int Len = POW (2, H)-1; t = (bitree) malloc (sizeof (binode); t-> DATA = BT [1]; TQ. bt = T; TQ. num = 1; Q [1] = TQ; front = 0; Rear = 1; while (front! = Rear) {front = (front + 1) % maxsize; TQ = Q [Front]; P = TQ. bt; I = TQ. num; If (BT [2 * I] = '#' | 2 * I> Len) {P-> lchild = NULL ;} else {P-> lchild = (bitree) malloc (sizeof (binode); P-> lchild-> DATA = BT [2 * I]; TQ. bt = p-> lchild; TQ. num = 2 * I; Rear = (Rear + 1) % maxsize; Q [rear] = TQ;} If (BT [2 * I + 1] = '#') {P-> rchild = NULL;} else {P-> rchild = (bitree) malloc (sizeof (binode )); p-> rchild-> DATA = BT [2 * I + 1]; TQ. bt = p-> child; TQ. num = 2 * I + 1; Rear = (Rear + 1) % maxsize; Q [rear] = TQ ;}}}