Binary Search Tree-C language implementation (from data structure and algorithm analysis C language description)

Source: Internet
Author: User
1. An important application of Binary Trees is their use in search. The nature of making a binary tree a search tree is that for each node in the tree XAll the key values in the left subtree are smaller than the key values of X, and all the keywords in the right subtree are greater than the key values of X. In Figure 1, the tree on the left is a binary search tree, but the tree on the right is not (think about why ). Figure 1 Two Binary Trees (only the tree on the left is the search tree) 2. Implementation
Because a binary tree can have at most two sons, we can direct them with pointers. The declaration of a tree node is similar to the declaration of a double-stranded table. In the declaration, a node adds two pointers (left and right) to other nodes by the key (keyword) information) structure. File Name: tree. h
#ifndef _Tree_Htypedef int ElementType;struct TreeNode;typedef struct TreeNode *Position;typedef struct TreeNode *SearchTree;SearchTree MakeEmpty( SearchTree T );Position Find( ElementType X, SearchTree T );Position FindMin( SearchTree T );Position FindMax( SearchTree T );SearchTree Insert( ElementType X, SearchTree T );SearchTree Delete( ElementType X, SearchTree T );ElementType Retrieve( Position P );void PrintElement( SearchTree T );void PreOrder( SearchTree T );void InOrder( SearchTree T );void PostOrder( SearchTree T );#endif /* Tree_H */

File Name: tree. c

# Include "Fatal. H" # include "tree. H" struct treenode {elementtype element; searchtree left; searchtree right ;}; searchtree makeempty (searchtree t) {If (T! = NULL) {makeempty (t-> left); makeempty (t-> right); free (t);} return NULL;} positionfind (elementtype X, searchtree T) {If (t = NULL) return NULL; If (x <t-> element) return find (x, t-> left); elseif (x> T-> element) return find (x, t-> right); else return t;}/* Recursive Implementation of the findmin of the binary search tree */positionfindmin (searchtree T) {If (t = NULL) return NULL; elseif (t-> left = NULL) return t; elsereturn findmin (T-> left);}/* Non-Recursive Implementation of the findmax of the binary search tree */positionfindmax (searchtree t) {If (T! = NULL) while (t-> right! = NULL) t = T-> right; return t;} searchtreeinsert (elementtype X, searchtree t) {If (t = NULL) {/* Create and return a one-node tree */T = malloc (sizeof (struct treenode); If (t = NULL) fatalError ("out of space !!! "); Else {T-> element = x; t-> left = T-> right = NULL;} elseif (x <t-> element) t-> left = insert (x, t-> left); elseif (x> T-> element) T-> right = insert (x, t-> right ); /* else X is in the tree already; we'll do nothing */return t;/* Do not forget this line !!! */} Searchtreedelete (elementtype X, searchtree t) {position tmpcell; If (t = NULL) error ("element not found"); elseif (x <t-> element) /* Go left */t-> left = Delete (x, t-> left); elseif (x> T-> element) /* Go right */t-> right = Delete (x, t-> left ); else/* found element to be deleted */If (t-> left & T-> right) /* Two Children */{/* replace with smallest in right subtree */tmpcell = findmin (T-> right); t-> element = tmpcell-> element; t-> right = Delete (t-> element, T-> right );} else/* One or Zero Children */{tmpcell = T; If (t-> left = NULL)/* also handles 0 children */T = T-> right; else if (t-> right = NULL) t = T-> left; free (tmpcell);} return t;} elementtype retrieve (position P) {return p-> element;} void printelement (searchtree t) {printf ("% 3d", retrieve (t);} void preorder (searc Htree t) {If (T! = NULL) {printelement (t); preorder (t-> left); preorder (t-> right) ;}} void inorder (searchtree t) {If (T! = NULL) {inorder (t-> left); printelement (t); inorder (t-> right) ;}} void postorder (searchtree t) {If (T! = NULL) {postorder (t-> left); postorder (t-> right); printelement (t );}}

File Name: Main. c

#include "tree.h"#include <stdio.h>int main(){SearchTree T = NULL;int i, j, m, n;ElementType tmp;printf( "Number of Elements:" );scanf( "%d", &n );for ( i = 0; i < n; i++){scanf( "%d", &tmp );T = Insert( tmp, T );}        printf( "\nPreOrder :" );        PreOrder( T );printf( "\nInOrder  :" );InOrder( T );printf( "\nPostOrder:" );PostOrder( T );        printf( "\n" );return 0;}

Appendix: the above Code uses functions such as error and fatalError. The implementation is as follows (fatal. h file ):

#include <stdio.h>#include <stdlib.h>#define Error( Str )        FatalError( Str )#define FatalError( Str )   fprintf( stderr, "%s\n", Str ), exit( 1 )

Note: This article is excerpted from "data structure and algorithm analysis C language description Mark Allen Weiss". The Code has been compiled and tested by GCC.

Attachment download: http://download.csdn.net/detail/shuxiao9058/4212427#tree_20120401.tar.gz

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.