Implementation of the root insertion, selection, deletion, merging, sorting and other operations of binary search tree

Source: Internet
Author: User

The source code is as follows:


The key here is not treated as a keyword, but instead treats item.c as a keyword


#include <stdlib.h> #include <stdio.h>//#define Key inttypedef int key;struct item{key key;char c;}; typedef struct STNODE* link;struct stnode{item Item; link l,r; int N;}; Static link head, Z; static struct Item nullitem; Key key (item item) {return item.key;} Create a node link NEW (item item, link L,link r, int N) {link x = (link) malloc (sizeof *x); X->item = Item;x->l = L;x->r = R;x->n = N;if (head==z) head=x; This sentence can not be less!!! return x;} initialize void Stinit () {head = (z = NEW (nullitem,0,0,0));} Number of nodes int Stcount () {return head->n;}//Search subroutine Item searchr (link h, char v) {char t = h->item.c;if (h==z) return Nulli Tem;if (v==t) return h->item;if (v<t) return SEARCHR (H-&GT;L,V); else return SEARCHR (h->r,v);} Search Main program Item Stsearch (Key v) {return searchr (head,v);} Select the entry K small key value K starting from 0 item selectr (link h, int k) {int t;if (h==z) return nullitem;t = (h->l==z)? 0:h->l->n;if (T>k) Return selectr (H-&GT;L,K); if (t<k) return selectr (h->r,k-t-1); return h->item;} Item Stselect (int k) {return selectr (head,k);}  ----------------root Insert--------------------////Right rotation clockwise turn link ROTR (link h) {link x = h->l; h->l = x->r; x->r=h;} Left rotation counterclockwise rotate link rotl (link h) {link x = h->r; h->r = x->l; x->l=h;} Insert subroutine link Insertt (link h, item item) {//key v = key (item), T = key (H->item), char v = item.c, t = h->item.c;if (h==z Return NEW (item,z,z,1), if (v<t) {h->l = Insertt (h->l,item); h = ROTR (h);} else {h->r = Insertt (h->r,item); h = rotl (h);} (h->n) ++;return h;} BST root Insert main program void Stinsert (item item) {//The newly inserted node will be treated as root node head = Insertt (Head,item);} ----------------root Insert--------------------////----------------Delete a node----Method 1-----A great diversion-----------//Link PARTR (  Link h, int k) {int t = h->l->n; Why not judge non-empty? if (t>k) {h->l = PARTR (h->l,k); H=rotr (h);} if (t<k) {h->r = PARTR (h->r,k-t-1); H=rotl (h);} return h;} Link JOINLR (link a, link b) {if (b==z) return A;b=partr (b,0); b->l = A;return b;} Link DELR (link h, char k) {link x; char t = h->Item.c;if (h==z) return z;if (t>k) H-&GT;L=DELR (h->l,k), if (t<k) H-&GT;R=DELR (h->r,k); if (t==k) {x = h;h= JOINLR (H->l,h->r); free (x);} return h;} Delete main program void Stdelete (Char v) {head = DELR (head,v);} ----------------Delete a node-----Method 1-----The Big one----------////----------------Delete a node-----Method 2---------------////delete sub-program Item deleter (link F) {item tmp; link p;if (f->l==null) {p = f;tmp = f->item; F = F->r;free (p); return tmp;} else return deleter (f->l);} Delete subroutine void Deleterr (link h, key v) {if (h!=null) {Key t = key (H->item), if (v<t) Deleterr (h->l,v); else if (v>t) d Eleterr (h->r,v); ElseIf (h->l==null) {//processing only one subtree or no subtree 1 link p = h->r; h=p; free (p);} else if (h->r==null) {//handles only one subtree or no subtree 2 link p = h->l; h=p; free (p);} else h->item= deleter (h->r); If the node to be deleted has both Zuozi and the right subtree//Then replace it with the leftmost lower node of the right subtree of the node, maintain binary search Tree}}//delete main program void STdelete2 (Key v) {deleterr (head,v);} ----------------Delete a node-----Method 2---------------//void Sortr (link h) {if (h==z) return;sortr (h->l); if (h->item.key!=0) printf ("%c", H-&GT;ITEM.C); Sortr (h->r);} Binary search tree sort void stsort (link h) {sortr (h);} Two binary search Trees link stjoin (link a, link b) {if (b==z) return a;if (a==z) return b;b = Insertt (b,a->item); b->l = Stjoin (A-&G T;L,B-&GT;L); B->r=stjoin (a->r,b->r); free (a); return b;} void Test () {struct Item item1 = {322, ' a '};struct item item2 = {23°c, ' s '};struct item item3 = {2, ' E '};struct item item4 = {33 2, ' R '};struct Item ITEM5 = {332, ' C '};struct item ITEM6 = {332, ' h '};struct item Item7 = {, ' I '}; Stinit (); Stinsert (ITEM1); Stinsert (ITEM2); Stinsert (ITEM3); Stinsert (ITEM4); Stinsert (ITEM5); Stinsert (ITEM6); Stinsert (ITEM7); Stsort (head);p rintf ("\ n"), struct Item item11 = stselect (2); printf ("%c\n", item11.c); Stdelete (' i '); Stsort (head);}  Main () {test ();}



Run results



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implementation of the root insertion, selection, deletion, merging, sorting and other operations of binary search 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.