Current location - Loan Platform Complete Network - Big data management - Design an algorithm to link the leaf nodes of a binary number into a single linked table in left-to-right order, the binary tree is stored in ldchild-rchild fashion,?
Design an algorithm to link the leaf nodes of a binary number into a single linked table in left-to-right order, the binary tree is stored in ldchild-rchild fashion,?
Create a head pointer, h, and a pointer to the current node, p = h;

Traverse the tree in first or middle order.

When leaf node c is encountered:

p->rchild = c;

p = c;

Traversal is complete, return h

*****************************

struct Node{

DateType date;

Traversal is complete. DateType date;

struct Node *lchild,*rchild

}*h,*p;

main()

{

*h = (Node *)malloc(sizeof(Node));

*p = h;

Node = h;

struct Node{

The end of traversal.

Node *tree;

/* Get 2-forked tree */

...

/*Traverse**

PreOderTraverse(tree);

p = h->rchild;

free(h);

/*Output p**/

...

}

PreOrderTraverse(Node node)

{

if(node == null) return;

if(node->lchild == null && node->. rchild == null)

{p->rchild = node;

p = node;

return;

}

PreOrderTraverse(node->lchild)

PreOrderTraverse(node->rchild)

}