Binary search tree with two-step learning algorithm

Source: Internet
Author: User

Binary search tree is also called binary search trees, binary sort tree

What kind of tree is this?

In fact, the root node of the Zuzi Shubigen node small right subtree is larger than the root node and the left and right subtree is also a binary search tree

Code comparison simple Basic recursive implementation better understand that it is difficult to understand only deleting nodes with left and right subtrees

The method is to find a minimum node in the right subtree instead of the node to be deleted and continue to delete the node in the right subtree.

See the code in detail

1#include"stdio.h"2#include"stdlib.h"3typedefstructBintree4 {5     intdata;6     structBintree *Left ;7     structBintree *Right ;8 }bintree;9 TenBintree *bintreefind (intKey,bintree *t) One { A      while(!t) -     { -         if(key>t->data) theT=t->Right ; -         Else if(key<t->data) -T=t->Left ; -         Else +             returnT;  -     } +     returnNULL; A } at  -Bintree *bintreeinsert (intKey,bintree *t) - { -     if(!t) -     { -T= (bintree*)malloc(sizeof(Bintree)); inT->data=key; -t->left=NULL; tot->right=NULL; +     } -     Else if(key>t->data) theT->right=bintreeinsert (key,t->Right ); *     Else if(key<t->data) $T->left=bintreeinsert (key,t->Left );Panax Notoginseng     Else -         returnT; the } +Bintree *findmin (Bintree *t) A { the     if(t) +     { -          while(t->Left ) $T=t->Left ; $     } -     returnT; - } theBintree *delete (intKey,bintree *t) - {WuyiBintree *temp; the     if(!t) -printf"Delete Failed"); Wu     Else if(key<t->data) -T->left=delete (key,t->Left ); About     Else if(key>t->data) $T->right=delete (key,t->Right ); -     Else if(T->left && t->Right ) -     { -Temp=findmin (t->Right ); A      +T->data=temp->data; theT->right=delete (t->data,t->Right ); -     }  $     Else the     { thetemp=T; the         if(!t->Left ) theT=t->Right ; -         Else if(!t->Right ) inT=t->Left ; the          Free(temp); the     } About}

Binary search tree with two-step learning algorithm

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.