Binary Search Tree

Source: Internet
Author: User

Introduction:


The nature of the binary tree to become a binary search tree is: For each node X in the tree, all the key values in its left subtree are less than the key value of X, all the keyword values in its right subtree are greater than those of X.


Binary Search Tree statement

struct TreeNode;typedef struct TreeNode *Position;typedef struct TreeNode *SearchTree;struct TreeNode{ElementType Element;SearchTree  Left;SearchTree  Right;};

Create an empty tree routine

SearchTree MakeEmpty(SearchTree T){if(T != NULL){MakeEmpty(T->Left);MakeEmpty(T->Right);free(T);}return NULL;}

Find operation of Binary Search Tree

Position Find(ElementType X, SearchTree T){if(T == NULL)return NULL;if(X < T->Element)return Find(X, T->Left);else if(X > T->Element)return Find(X, T->Right);return T;}

Recursive and non-Recursive Implementation of the findmin of the Binary Search Tree

Position FindMin(SearchTree T){if(T == NULL)return NULL;else if(T->Left == NULL)return T;else return FindMin(T->Left);}Position FindMin(SearchTree T){if(T != NULL)while(T->Left != NULL)T = T->Left;return T;}

Recursive and non-Recursive Implementation of the findmax of the Binary Search Tree

Position FindMax(SearchTree T){if(T == NULL)return NULL;else if(T->Right == NULL)return T;else return FindMax(T->Right);}Position FindMax(SearchTree T){if(T != NULL)while(T->Right != NULL)T = T->Right;return T;}

Routine for inserting elements into a binary search tree

SearchTree Insert(ElementType X, SearchTree T){if(T == NULL){T = (SearchTree)malloc(sizeof(struct TreeNode));if(T == NULL){printf("Out of space.\n");return NULL;}}else if(X < T->Element){T->Left = Insert(X, T->Left);}else (X > T->Element){T->Right = Insert(X, T->Right);}return T;}

Binary Search tree deletion routine

SearchTree Delete(ElementType X, SearchTree T){Position TmpCell;if(T == NULL){fprintf(stderr,"Element not found.\n");return NULL;}else if(X < T->Element)T->Left = Delete(X, T->Left);else if(X > T->Element)T->Right = Dlelte(X, T->Right);else if(T->Left && T->Right){TmpCell = FindMin(T->Right);T->Element = TmpCell->Element;T->Right = Delete(T->Element, T->Right);}else{TmpCell = T;if(T->Left == NULL)T = T->Right;else if(T->Right == NULL)T = T->Left;free(TmpCell);}return T;}


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.