Data Structure Tree exercises (II)

Source: Internet
Author: User

 

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 ;}}}

 

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.