Create a two-fork tree (binary sort tree)

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>/* recursion before and after traversal */typedef struct node{  int  Data;  struct node*left;  struct node*right;} Btnode; Btnode*createtree (int a[],int n) {  btnode*root,*c,*p,*pa;  int i;   root= (btnode*) malloc (sizeof (Btnode));  root->data=a[0];  root->left=root-> right=null;//Establish root node   for (i=1;i<n;i++) {      p= (BTnode*) malloc (sizeof (Btnode));       p->data=a[i];      p->left=p->right =null;      c=root;               //the root node to the C pointer while (c) {                 //to determine if p node belongs to Zuozi or right sub-tree   pa=c;              &nbsThe P;&NBSP;&NBSP;//PA pointer is the parent node of the P node   if (c->data>p->data)      c=c->left ;   else     c=c->right;} if (Pa->data>p->data)   //p node when parent's left child or right child    pa->left=p;else    pa->right=p;  }  return root;} Void forder (btnode*root) {  if (root) {  printf ("%d", root->data);   printf (" \ n ");   forder (Root->left);   forder (root->right);   }}void inorder ( Btnode*root) {  if (root) {  inorder (root->left);   printf ("%3d", root->data);   printf ("\ n");   inorder (root->right);   }}void porder (BTnode*root) {   if (Root) {  porder (root->left);   porder (root->right);   printf (" %6d ", root->data);   printf (" \ n ");    }}int main (void) {  btnode*root;  int *a; int n; int i; printf ("Please enter n="); &NBSP;SCANF ("%d", &n);  a= (int*) malloc (n*sizeof (int));  printf ("Enter array a[]="),  for (i=0;i<n;i++)    scanf ("%d", &a[i ]); Root=createtree (a,n);  forder (root); inorder (root); Porder (root);}

Create a two-fork tree (binary sort tree)

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.