C + + Implementation find examples of all paths in a binary tree and for a value _c language

Source: Internet
Author: User

A path is formed from the root node of the tree and down to all nodes that have passed through the leaf node.
Prints all paths that are equal to the input integer.
For example, enter the integer 22 and the following two-dollar Tree

Then print out two paths: 10, 12 and 10, 5, 7.

The result can be obtained by first traversing the tree.
Algorithm: Findpath (btree * root,int sum,int Target,stack * s) is used to compute the sum of the elements in the stack and target is the destination value.
Calculates the sum of current nodes and sums after reaching a node, if target, the output path returns, if it is greater than target, it is returned directly, if less than, the current node's value is put on the stack, the value of sum is updated, the traversal is continued, and after the traversal is completed, that is, when the current node returns, Pop it out of the stack, update sum
The code is as follows (GCC compiles through):


#include "stdio.h" #include "stdlib.h" #define MAXSIZE 8 typedef struct Node {int data;
 struct node * left;
struct node * right;
 
}btree;
 typedef struct {int top;
int data[maxsize];
 
}stack;
Btree * Creattree (int a[],int n);
void Iorder (Btree * root);
void Porder (Btree * root);
void Findpath (Btree * root,int sum,int target,stack * Stack);
void Initstack (stack * stack);
void Push (Stack * s,int val);
 
int Pop (Stack *s);
 int main (void) {int array[maxsize] = {5,3,8,7,2,4,1,9},target;
 Btree * ROOT;
  
 Stack stack;
 target = 12;
 Root = Creattree (array,maxsize);
 
 Initstack (&stack);
 printf ("In ascending order of elements in binary trees:");
 Iorder (root);
 
 printf ("\ n");
 printf ("Target value:%d, path:", target);
 
 Findpath (Root,0,target,&stack);
 printf ("\ n");
return 0;
 Btree * Creattree (int a[],int n) {btree * root, *P,*CU,*PA, based on array generation of two-fork sort tree;
  
 int i;
 Root = (Btree *) malloc (sizeof (btree)); 
 Root->data = a[0];
  
 Root->left = Root->right =null; for (i=1;i<n;i++) {p = (btree *) malloc(sizeof (btree));
  P->data = A[i];
  P->left = P->right =null;
 
  Cu = root;
   while (CU) {pa = cu;
   if (Cu->data > P->data) cu = cu->left;
  else Cu = cu->right;
  } if (Pa->data > P->data) pa->left = p;
 else pa->right = p;
return root;
  }//In root traversal, print binary tree void Iorder (btree * root) {if (root) {Iorder (root->left);
  printf ("%3d", root->data);
 Iorder (Root->right);
 
 Find path void Findpath (Btree * root,int sum,int Target,stack * s) {int i;
 if (!root) return;
  if (sum + root->data = = target) {Push (s,root->data);
  for (i = 0;i<s->top;i++) printf ("%3d", S->data[i]);
 Return
   else if (sum + root->data > Target) {return;
  else {Push (s,root->data);
  Sum + + root->data;
  Findpath (root->left,sum,target,s);
  Findpath (root->right,sum,target,s);
  Sum-= root->data;
   Pop (s);
 }///Initialize stack void Initstack (stack * s) {s->top = 0;}
into stack void Push (Stack *s,int val) {if (s->top = = MAXSIZE) {printf ("stack full, unable to stack!")
  \ n ");
 Return
 
} s->data[(S->top) + +] = val;
  }//out stack int Pop (stack *s) {if (S->top = 0) {printf ("stack empty, unable to stack!\n");
 Return
Return s->data[--(S->top)];
 }

 

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.