Look at the data structure write code (55) Binary sort tree

Source: Internet
Author: User

Binary sort tree is a dynamic lookup tree, and its creation is generated in the lookup. When the lookup fails, it inserts the number into the appropriate position. Binary find tree The value on the left subtree of the father is small, and the value on the right subtree is always larger than the parent node. This looks for similar and binary lookups, where the maximum number of lookups is equal to the depth of the tree.


The following code is primarily two-way: inserting nodes and deleting nodes.

Here's the code, and you are welcome to indicate that the code is insufficient:

BinarySortTree.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <cstdlib>typedef int treetype;typedef struct BSTNODE{TREETYPE data; Bstnode * LEFTCHILD; Bstnode * RIGHTCHILD;} *bstree;//Two fork sort tree//exists returns TRUE, there is no return false//f initial value must be Nullbool searchbst (bstree tree,treetype key,bstree F,bstree * p) {if ( Tree = = NULL) {//lookup failed, must have looked for a node's left subtree or right subtree to null,*p = = This node. *p = F;return false;} Else{treetype data = tree->data;if (data = = key) {return true;} else if (Data > key) {return Searchbst (tree->leftchild,key,tree,p);} Else{return Searchbst (tree->rightchild,key,tree,p);}} int Insertbst (Bstree * Tree,treetype key) {Bstree p;if (searchbst (*tree,key,null,&p) = = False) {//not found, need to be inserted. Bstree node = (bstree) malloc (sizeof (Bstnode)); node->data = Key;node->leftchild = Node->rightchild = null;// The inserted is definitely the leaf node. if (p = = NULL) {//Before tree is empty tree *tree = node;} Else{if (P->data > key) {p->leftchild = node;} else{p->rightchild = node;}} return true;} return false;} Delete node bool Deletenode (Bstree * tree) {BStree p = * TREE;IF (P->leftchild = = null) {//Left dial hand tree is empty (leaf node also walks this step) *tree = P->rightchild;free (p);} else if (P->rightchild = = null) {//Right subtree is empty *tree = P->leftchild;free (p);} else{//trees are not empty, look for the left subtree maximum node, modify the Node data field to the maximum node, and delete the maximum node Bstree max, maxpre = Null;//max: node left subtree, max node (right bottom of left subtree) max = p-> Leftchild;while (max->rightchild!= NULL) {maxpre = max;//The predecessor of the maximum node max= max->rightchild;} P->data = max->data;//Change maximum if (max = = P->leftchild) {//delete node right subtree is empty//p->leftchild = Null;p->leftchild = max ->leftchild;} Else{maxpre->rightchild = Max->leftchild;} Free (max);} return true;}  BOOL Deletebst (Bstree * Tree,treetype key) {Bstree p = *tree;if (P! = NULL) {if (P->data = = key) {return deletenode (tree); }else if (P->data > key) {return Deletebst (& (P->leftchild), key);} Else{return Deletebst (& (P->rightchild), key);}} return false;} Bstree Find (Bstree tree,treetype key) {while (tree) {Treetype data = tree->data;if (data = = key) {return tree;} else if (Data > key) {tree = Tree->lEftchild;} Else{tree = Tree->rightchild;}} return NULL;} Middle order traversal void inordertraverse (Bstree tree) {if (Tree! = NULL) {inordertraverse (tree->leftchild);p rintf ("%d\t", tree- >data); Inordertraverse (Tree->rightchild);}} int _tmain (int argc, _tchar* argv[]) {//bstree tree; Bstree tree = null;//must be equal to NULL int array[] = {88,22,33,55,66,77,44,11,99};for (int i = 0; i < 9; i++) {Insertbst (&tre E,array[i]);} By definition, the middle order is based on the small to large printf ("-------------sequence traversal--------------\ n"); Inordertraverse (tree); Bstree f = Find (tree,88);p rintf ("%d\n", F->data);p rintf ("-------------Delete 88 sequential traversal--------------\ n");d Eletebst ( &tree,88); Inordertraverse (tree);p rintf ("\ n-------------Delete 11 sequence traversal--------------\ n");d Eletebst (&tree,11); Inordertraverse (tree);p rintf ("\ n-------------Delete 99 sequence traversal--------------\ n");d Eletebst (&tree,99); Inordertraverse (tree);p rintf ("\ n-------------Delete 55 sequence traversal--------------\ n");d Eletebst (&tree,55); Inordertraverse (tree);p rintf ("\ n-------------remove 44 sequence traversal--------------\ n");d Eletebst (&treE,44); Inordertraverse (tree);p rintf ("\ n-------------Delete 33 sequence traversal--------------\ n");d Eletebst (&tree,33); Inordertraverse (tree);p rintf ("\ n-------------Delete 22 sequence traversal--------------\ n");d Eletebst (&tree,22); Inordertraverse (tree);p rintf ("\ n-------------Delete 66 sequence traversal--------------\ n");d Eletebst (&tree,66); Inordertraverse (tree);p rintf ("\ n-------------Delete 77 sequence traversal--------------\ n");d Eletebst (&tree,77); Inordertraverse (tree); return 0;}
Run:



Look at the data structure write code (55) Binary sort 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.