Design an algorithm that outputs the inverse path from each leaf node to the root node

Source: Internet
Author: User

The following three kinds of traversal methods are used to output the inverse path , assuming that the binary tree is stored with a two-fork chain storage structure.

Method 1: using a hierarchical traversal -based approach, the designed queue is a non-circular sequential queue, with all the scanned node pointers in the queue and the positions of the parent nodes in the queues. When a leaf node is found, the reverse path of the leaf node to the root node is output through the position of the parent node in the queue. the corresponding algorithm is as follows:

void Allpath (Btnode * b)

{

struct Snode

{

Btnode *node; //Hold current node pointer

int parent; //Store the position of the parent node in the queue

} Qu[maxsize]; //define non-circular queues

int front,rear,p; //define team head and end of team pointer

Front=rear=-1; //Set the queue to an empty queue

rear++;

Qu[rear].node=b; //root node pointer into the team

Qu[rear].parent=-1; //root node does not have a parent node

while (front<rear)//queue is not empty

{

front++; The //front is the subscript for the current out-of-band node

B=qu[front].node; //Team head out Team

if (b->lchild==null&&b->rchild==null)//*b is a leaf node

{

printf ("%c to root-node inverse path:", B->data);

P=front;

while (Qu[p].parent!=-1)

{

printf ("%c\n", qu[p].node->data);

P=qu[p].parent;

}

printf ("%c\n", qu[front].node->data);

}

if (b->lchild!=null)//left child in team

{

rear++;

qu[rear].node=b->lchild;

Qu[rear].parent=front;

}

if (b->rchild!=null)//Right child into Team

{

rear++;

qu[rear].node=b->rchild;

Qu[rear].parent=front;

}

}

}

Method 2: Use the post-order traversal of the non-recursive algorithm, the access to a node to: Determine whether the node *p is a leaf node, if the leaf node, the output stack of all the values of the nodes from the node to the root node of the inverse path. The corresponding non-recursive algorithm is as follows:

void Allpath (Btnode * b)

{

Btnode *st[maxsize]; //define a sequential stack

Btnode *p=b,*q;

int flag,top=-1,i; //stack pointer null value

if (b!=null)

{

Do

{

while (p!=null)//Put all left nodes of *p into the stack

{

top++;

St[top]=p;

p=p->lchild;

}

Q=null; //q Point to the previous visited node of the top node of the stack

flag=1; //Set Flag=1 to handle the top node of the stack

while (top!==-1&&flag==1)

{

P=st[top]; //Remove the top element of the current stack

if (p->rchild==q)//Right child does not exist or has been visited, access to the

{

if (p->lchild==null&&p->rchild==null)//leaf node

{

printf ("%c to root" inverse path: ", P->data);

for (i=top;i>=0;i--)

printf ("%c", st[i]->data);

printf ("\ n");

}

top--;

Q=p; //q point to the node you just accessed

}

Else

{

p=p->lchild; //p finger right child node

flag=0; //Set flag=0 indicates that the top node of the stack has finished processing

}

}

}while (Top!=-1);

printf ("\ n");

}

}

Method 3: Use the path array to store the paths, Pathlen the integer to store the path length. The function f (b,path,pathlen) is to output the inverse path of all leaf nodes in the binary tree B to the root node, and it is necessary to implement the algorithm by means of path and Pathlen two parameters. the corresponding recursive model is as follows:

The corresponding recursive algorithm is as follows:

void Allpath (Btnode *b,elementtype path[],int pathlen)//Initial call when all elements of path are empty, Pathlen is 0,b

{ //is the root node pointer

int i;

if (b!=null)

{

if (b->lchild==null&&b->rchild==null)

{

printf ("%c to root" inverse path:%c ", b->data,b->data);

for (i=pathlen-1;i>=0;i--)

printf ("%c", Path[i]);

printf ("\ n");

}

Else

{

path[pathlen]=b->data; //Put the current node in the path

pathlen++; //path length increased by 1

Allpath (B->lchild,path,pathlen); //recursive scan left subtree

Allpath (B->rchild,path,pathlen); //recursive scan right sub-tree

pathlen--; //Recovery Environment

}

}

}

For the two-fork tree, the result of the path of each leaf node to the root node is obtained.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design an algorithm that outputs the inverse path from each leaf node to the root node

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.