Create and search Binary Trees in C Language

Source: Internet
Author: User

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Define MAXWORD 100 struct tree_node * addtree (struct tree_node *, char *); void treeprint (struct tree_node *); int getword (char *, int ); // define the binary tree structure struct tree_node {char * word; // pointer to the word int * count; // count the number of times the word appears struct tree_node * left; // pointer to the left subtree self-reference struct tree_node * right; // pointer to the right subtree}; int main () {struct tree_node * root; char word [MAXWORD]; root = NULL; while (getword (word, MAXWORD )! = EOF) if (isalpha (word [0]) root = addtree (root, word); treeprint (root); return 0 ;} // allocate space for the new node struct tree_node * tree_alloc (void); // copy the new word char * strdup (char *); struct tree_node * addtree (struct tree_node * p, char * w) {int cond; if (p = NULL) {// a new word has arrved; p = tree_alloc (); // make a new node; p-> word = strdup (w); p-> count = 1; p-> next = p-> right = NULL;} else if (cond = strcmp (w, p-> word) = 0) p-> Count ++; // The word else if (cond <0) that exists in the binary tree) // The new word is smaller than the node in the tree and placed on the left p-> left = addtree (p-> left, w); elsep-> right = addtree (p-> right, w); return p;} // The treeprint function prints the tree in sequence ==> print the left subtree and then print itself, print the right subtree void treeprint (struct tree_node * p) {if (p! = NULL) {treeprint (p-> left); printf ("% 8d % s \ n", p-> count, p-> word ); treeprint (p-> right) ;}// allocate space required by the new node // struct tree_node * tree_alloc (void) {return (struct tree_node *) malloc (sizeof (struct tree_node);} // The strdup function copies the input string to a safe position char * strdup (char * s) {char * p by calling the malloc function; p = (char *) malloc (strlen (s) + 1); // allocate one more byte to store '\ 0 ', the length returned by the strlen function does not include '\ 0. if (p! = NULL) // space allocation may fail, so judge strcpy (p, s); return p;} first ;}
    
   
  
 

Related Article

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.