Binary sorting tree

Source: Internet
Author: User

Binary sort tree: An empty tree or a tree of the following properties: (1) if its left subtree is not empty, then the value of the node on the left subtree is smaller than the value of its root node. (2) if its right subtree is not empty, then, the value of the node on the right subtree is greater than the value of its root node. (3) Its left and right subtree are also binary sorting trees.

The basic operations of the binary sorting tree can be completed in O (h) Time (Introduction to algorithms p165 ).

The operation code is as follows:

Int insertbst (bitree & T, int key) // recursive insert {If (t = NULL) {T = new binode; t-> DATA = key; t-> lchild = T-> rchild = NULL; return 1;} else {If (Key = T-> data) return 0; else if (Key <t-> data) return insertbst (t-> lchild, key); elsereturn insertbst (t-> rchild, key );}} int insertbst _ (bitree & T, int key) // iterative insert {// find the insert positionbinode * f = T, * P = T; while (P! = NULL) {If (Key = p-> data) return 0; F = P; // records the last accessed node P = Key <p-> data? P-> lchild: p-> rchild;} // allocate the new node binode * q = new binode; q-> lchild = Q-> rchild = NULL; q-> DATA = key; // insert if (t = NULL) // if the root is null {T = Q; return 1 ;} if (Key <F-> data) F-> lchild = Q; elsef-> rchild = Q; return 1;} binode * searchbst (bitree T, int key) // recursive search {If (! T) return NULL; else {If (Key = T-> data) return t; else if (Key <t-> data) return searchbst (t-> lchild, key); elsereturn searchbst (t-> rchild, key) ;}} binode * searchbst _ (bitree T, int key) // iterative search {While (T! = NULL) {If (Key = T-> data) break; t = Key <t-> data? T-> lchild: T-> rchild;} return t;} int deletenode (binode * & P) {binode * q; // Delete the node P from the binary sorting tree, returns 0 if (P = NULL); If (p-> lchild = NULL) // If the left subtree is empty, you only need to re-connect the right subtree {q = P; P = p-> rchild; Delete Q;} else if (p-> rchild = NULL) // if the right subtree is empty, you only need to re-connect the left subtree {q = P; P = p-> lchild; Delete Q ;} else // left and right subtree are not empty {binode * s; # If 0 // replace P with the direct precursor of P, and then delete the direct precursor q = P of P; S = p-> lchild; // turn left, and then right to the end while (S-> rchild) {q = s; S = s-> rchild ;} p-> DATA = s -> Data; // s points to the deleted node, and Q points to the precursor of the deleted node if (Q! = P) q-> rchild = s-> lchild; // re-connect the right subtree of Q elseq-> lchild = s-> lchild; // re-connect the left subtree of Q # else // replace P with the direct successor of P, and then delete the direct successor q = P; S = p-> rchild of P; while (S-> lchild) {q = s; S = s-> lchild;} p-> DATA = s-> data; If (P! = P) q-> lchild = s-> rchild; elseq-> rchild = s-> rchild; # endifdelete s;} return 1;} int deletebst (bitree & T, int key) {If (t = NULL) return 0; else {If (Key = T-> data) return deletenode (t ); else if (Key <t-> data) return deletebst (t-> lchild, key); elsereturn deletebst (t-> rchild, key );}} void createbst (bitree & T, int A [], int N) {T = NULL; For (INT I = 0; I <n; I ++) {insertbst _ (t, a [I]) ;}} void destorybst (bitree & T) {If (t = NULL) return; destorybst (t-> lchild ); destorybst (t-> rchild); Delete t; t = NULL;} void inordertraverse (bitree t) // = O (n) time complexity {If (t = NULL) return; inordertraverse (t-> lchild); cout <t-> data <""; inordertraverse (t-> rchild );}
Test code:

Int main () {const int n = 10; int A [n] = {3, 2, 8, 6, 1, 4, 5, 7, 1, 3 }; bitree t; createbst (T, A, n); inordertraverse (t); cout <Endl; binode * P; For (INT I = 1; I <10; I ++) {P = searchbst _ (t, I); If (P! = NULL) cout <p-> data <Endl;} int B [5] = {0, 2, 6, 1, 7}; int ret; for (INT I = 0; I <5; I ++) {ret = deletebst (T, B [I]); If (ret = 0) after cout <"delete" <B [I] <"failed" <Endl; else {cout <"delete" <B [I] <: "; inordertraverse (t); cout <Endl ;}} destorybst (t); getchar (); Return 0 ;}

Reference: Data Structure C language and algorithm introduction (for delete node operations, note on p173 of this book)


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.