Small code learns BST simple C language version from the source

Source: Internet
Author: User

  /********************************  Operating Environment:http://www.anycodes.cn/zh/  Original:/http blog.csdn.net/u014488381/article/details/41719765/  Two binary sorting tree Lookup algorithm C code implementation   modification to direct test   pending C + + class package version  **** /#include  <stdio.h> #include  <stdlib.h>typedef int  elemtype;typedef struct bitnode{elemtype data;struct bitnode *lchild, * Rchild;} bitnode, *bitree;/*/inserts a node in a given BST, whose data field (value) is Element/*/void bstinsert ( BiTree *t,  elemtype element ) {/*/each insertion   opening space/*/if ( NULL == *t )  {(*t)  =  ( Bitree) malloc (sizeof (Bitnode));(*t)->data = element; (*t)->lchild =  (*t)->rchild  = null;} /*/based on binary search tree Properties/*/if ( element ==  (*t)->data )  return ; /*/does not allow duplicate data, If you encounter a direct non-processing/*/else if ( element <  (*t)->data )  bstinsert ( & (*t) Lchild, element );     else                        bstinsert ( & (*t)->rchild,  element );} /*/Create BST/*/VOID&NBSP;CREATEBST ( BiTree *t, Elemtype *a, int n ) {(*t)  =  null;for ( int i=0; i<n; i++ ) Bstinsert ( t, a[i] );} /*/bst Recursive lookup/*/void searchbst ( BiTree t, Elemtype key ) {bitree p;   p = t;if ( p )  {if ( key == p->data ) printf ("Find success!\n"); Else if (  (key < p->data)  &&  (null != p->lchild)   ) Searchbst ( p->lchild , key ); Else if (  (key > p->data)   &&  (null != p->rchild)  ) Searchbst ( p->rchild , key ); Elseprintf ("No this element!\n ");}} /*/bst Iteration Find/*/void _searchbst ( BiTree t, Elemtype key ) {bitree p;   p = t;while ( NULL != p && key != p->data )  /*/nodes exist and values are unequal/*/{if ( key < p->data )   p = p->lchild  ; else          p = p->rchild ;} /*/  out of the loop after the  p pointer always points to the found element   or   The empty node under the leaf node/*/if ( NULL != p )  printf (" Find Success!\n ");  else        printf (" No This element!\n ");} /*/BST node deletion      This binary tree content focus/*/void delbstnode ( bitree t, elemtype key  ) {bitree p, q;p = t; elemtype temp;/*/  first traverse to find this element/*/while ( null != p && key != p- >data )  {q = p; /*/ q is always above the node p (q is always the father when P is not the root node)              p is the element we're going to delete./*/if ( key < p-> data ) p = p->lchild ;elsep = p->rchild ;} if ( NULL == p )   printf ("No This Element!\n"),/*/find element    There are a few things       1.         2.               3.      q             q                    q      /\           / \                  /  \        p  p        p   p                p     p              /     \              / \   / \               x       x            x   x  x  x /*/         else {/*/Case 1: The parent node of node p is Q, and p is a leaf node, it is deleted directly. /*/if ( NULL == p->lchild && NULL == p->rchild )  {if ( p == q->lchild )  q->lchild = null;if ( p == q-> rchild )  q->rchild = null;free (p);p  = NULL;} /*/Case 2: The parent node of node p is Q, and P has only Zuozi or only right subtree, then the left subtree or right subtree of p can be changed directly to the left or right subtree of the parent node Q. /*/else if (  (null == p->rchild && null != p->lchild)  )  {/*/p only left dial hand tree/*/if ( p == q->lchild ) q->lchild = p->lchild  ;else if ( p == q->rchild ) Q->rchild = p->lchild ;free (p );p  = null;} Else if ( NULL == p->lchild && NULL != p->rchild )  {/*/p only right subtree/*/if ( p == q->lchild ) q->lchild = p->rchild ;if (  p == q->rchild ) Q->rchild = p->rchild ;free (p);p  = null ;} /*/Case 3: The parent node of node p is Q, and P has both Zuozi and right subtree. This code uses direct precursor (or direct successor) here is the largest element in the left subtree/*/else if ( null != p->lchild && null  != p->rchild )  {BiTree s, sParent;sParent = p;s =  Sparent->lChild ;while ( NULL != s->rchild ) {/*/found the direct precursor of P/*/sparent = s;s =  s->rchild ;      /*/left dial Hand Tree The largest is always in the left sub-tree in the bottom right corner/*/}temp = s-> data ;    /*/this time  s is pointing to the largest right lower leaf node   for the general situation 1  directly delete/*/delbstnode ( t,  temp );p->data = temp;   /*/finally change the data of P that was originally to be deleted to the deletion of temp/*/}}}/*/pending version   The beauty of the Bst/*/void printbst sequence traversal print ( BiTree t ) {if ( t )  {printbst ( t-> lchild );p rintf ("%d ",  t->data); Printbst ( t->rchild );}} Void use () {int n;int *a; elemtype key; bitree t;printf ("Please enter the node number of the binary lookup tree: \ n"); scanf ("%d",  &n);a =  (int *) malloc (sizeof (int) * N);p rintf ("Please input binary tree node data: \ n"); for ( int i=0; i<n; i++ ) scanf ("%d",  &a[i]); Createbst ( &t, a, n );p rintf ("The current binary lookup tree's middle order traversal result is: \ n"); Printbst ( t );p Rintf ("\n##############################################\n");p rintf ("Please enter the element to find: \ n"); scanf ("%d",  &key); printf ("BST recursive lookup result: \ n"); Searchbst ( t, key );//recursively find printf ("##############################################\n");p rintf (" Please enter the element to be deleted: \ n "), scanf ("%d ",  &key);D Elbstnode ( t, key );p rintf (" The current binary lookup tree's middle order traversal result is: \ n "); Printbst ( t );p rintf ("\n##############################################\n");p rintf ("Please enter the element you want to find: \ n"); scanf ("%d",  &key);p rintf ("BST iterative lookup results: \ n"); _searchbst ( t, key );//Iteration Lookup  }void  Test () {Int n;  elemtype key;   bitree t;    int  a[]={5,8,2,1,4,7,9,6,3};p rintf ("Please enter the node number of the binary lookup tree: \ n");     n=9;printf ("Please enter the node data for the binary tree: \ n"); Createbst ( &t, a, n );p rintf ("The current binary lookup tree's middle order traversal result is: \ n"); Printbst ( t );p rintf ("\n##############################################\n");p rintf ("Please enter the element you want to find: \ n"); key=8;printf ("BST recursive lookup result: \ n"); SearchbST ( t, key );//recursively find printf ("##############################################\n");p rintf ("Please enter the element to be deleted : \ n ");  key=5;delbstnode ( t, key );p rintf (" The current binary lookup tree's middle order traversal result is: \ n "); Printbst ( t );p rintf ("\n##############################################\n");p rintf ("Please enter the element you want to find: \ n");  key=5;printf ("BST iterative lookup results: \ n"); _searchbst ( t, key );//Iteration Lookup  }int main (void) {printf ("hello,c world of anycodex!\n"); test (); return exit_success;}


Small code learns BST simple C language version from the source

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.