Create, insert, and delete a binary search tree.

Source: Internet
Author: User
Binary sorting tree
  • The binary sorting tree is a basic data structure of the tree. There are many applications.
  • Its feature is that the left child is smaller than the parent, and the right child is greater than the parent.
Search for nodes

Looking for a node is to directly traverse from the root node based on the size of the value. If it is larger than the current traversal node, it will search for its right subtree. On the contrary, it will find its left subtree. Then return.

Find the maximum and minimum nodes

Based on the root node, traverse to the rightmost is the largest node, and traverse to the leftmost is the smallest node.

Insert Node

Insert nodes I insert here will become leaf nodes. Traverse down based on the relationship of the size, traverse to the last node, and then insert it.

Delete a node

Deleting a node is a little effort-consuming. When the deleted node is a leaf node or has only one child, you can directly Delete the current node so that the child can replace his location, when both the left and right nodes of the deleted node exist, to delete the current node, You need to replace the deleted node with the minimum node of the right subtree.

The Code is as follows:

//// Main. CPP // bstree // created by Alps on 14-7-31. // copyright (c) 2014 Chen. all rights reserved. // # include <iostream> # define elementtype intusing namespace STD; struct node; typedef node * ptrtonode; typedef skip treenode; treenode makeempty (treenode T); int isempty (treenode t ); treenode findtree (elementtype X, treenode T); treenode findmin (treenode T); treenode findmax (treenode T); treenode inser Ttree (elementtype X, treenode T); treenode deletetree (elementtype X, treenode T); struct node {elementtype element; treenode left; treenode right ;}; treenode makeempty (treenode T) {If (T! = NULL) {makeempty (t-> left); makeempty (t-> right); free (t);} return NULL;} int isempty (treenode T) {return T = NULL;} treenode findtree (elementtype X, treenode t) {If (t = NULL) {return NULL;} If (x <t-> element) {return findtree (x, t-> left);} else if (x> T-> element) {return findtree (x, t-> right );} else {return t ;}} treenode findmin (treenode t) {If (t = NULL) {return NULL;} If (t-> left = NUL L) {return t;} else {return findmin (t-> left);} // return NULL;} treenode findmax (treenode t) {If (t = NULL) {return NULL;} If (t-> right = NULL) {return t;} else {return findmax (t-> right) ;}// return NULL ;} treenode inserttree (elementtype X, treenode t) {If (t = NULL) {T = (treenode) malloc (sizeof (node); t-> element = X; t-> left = NULL; t-> right = NULL;} else if (x> T-> element) {T-> right = INS Erttree (x, t-> right);} else if (x <t-> element) {T-> left = inserttree (x, t-> left );} return t;} treenode deletetree (elementtype X, treenode t) {treenode xnode = findtree (x, t); If (xnode = NULL | T = NULL) {printf ("can't find the node is: % d", x); exit (1);} If (x> T-> element) {T-> right = deletetree (x, t-> right);} else if (x <t-> element) {T-> left = deletetree (X, t-> left);} else {treenode TMP; If (t-> left & T-> right) {TMP = findmin (t-> right); t-> element = TMP-> element; t-> right = deletetree (t-> element, T-> right);} else {TMP = T; If (t-> left = NULL) {T = T-> right;} else if (t-> right = NULL) {T = T-> left;} Free (TMP) ;}} return t ;} void preordertree (treenode t) {If (T! = NULL) {printf ("% d", T-> element); preordertree (t-> left); preordertree (t-> right );}} int main (INT argc, const char * argv []) {treenode t = (treenode) malloc (sizeof (node); t = makeempty (t ); T = inserttree (6, T); t = inserttree (2, T); t = inserttree (8, T); t = inserttree (1, t ); T = inserttree (5, T); t = inserttree (3, T); t = inserttree (4, T); // printf ("% d \ n ", t-> element); preordertree (t); printf ("\ n"); t Reenode TMP; TMP = findtree (2, T); printf ("% d \ n", TMP-> element); t = deletetree (2, T ); preordertree (t); printf ("\ n"); TMP = findmax (t); printf ("% d \ n", TMP-> element ); TMP = findmin (t); printf ("% d \ n", TMP-> element); // STD: cout <"Hello, world! \ N "; return 0 ;}


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.