Creating a binary array Delete insert Lookup

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>typedef struct TreeNode *bintree;           Defines an object pointer typedef bintree Position; Define an object pointer name struct Treenode{int Data; Bintree left; Bintree right;};/ /Two fork Search number lookup Position find (int x,bintree BST) {if (! BST)//Determine if BST is empty return null;if (x > Bst->data) return Find (x,bst->right);                The right index continues to find the else if (x < Bst->data)//In the left index to continue to find return find (x,bst->left); Elsereturn BST; Find successful}//two search number lookup non-recursive implementation position iterfind (int x,bintree BST) {while (BST) {if (x > Bst->data) BST = bst->right; else if (x < bst->data) BST = Bst->left;elsereturn BST;} return NULL;} Position findmin (Bintree BST) {if (! BST) return Null;else if (! Bst->left)//Find the left lobe node and return to the return Bst;elsereturn findmin (bst->left);} Position Findmax (bintree BST) {if (BST) while (bst->right) BST = Bst->right;return BST;} The interpolation algorithm of binary search number is suitable for the two-fork tree bintree Insert (int x,bintree BST) {if (!) in the lead node. BST)//Determine if the incoming pointer is empty {BST = new TreeNode; Bst->data = x; Bst-> left = Bst->right = NULL;} else if (x < bst->data) Bst->left = insert (x,bst->left), else if (x > Bst->data) bst->right = insert (x,   Bst->right); return BST; If none is executed, return the}//search binary number of the delete bintree Delete (int x,bintree BST) {Position tmp;if (! BST) printf ("Deleted element not found"); else if (x < bst->data) Bst->left = delete (x,bst->left); else if (x > Bst->data) Bst->right = Delete (x,bst->right), else{if (bst->left&&bst->right)//Deleted node contains left and right two sub-nodes {Tmp =   Findmin (Bst->right); Find the minimum value in the right index to replace the deleted element bst->data = tmp->data;  Bst->right = Delete (bst->data,bst->right); On the right side of the delete element, remove the minimum value from the node}else{tmp = bst;if (! Bst->left)//have right child paper or no knot BST = Bst->right;else if (! Bst->right) BST = bst->left;//have left child paper or no node free (TMP);}} return BST;} void Showbintree (bintree BST) {if (BST)//If judged for easy return of {showbintree (bst->left); Showbintree (bst->right);p rintf ("%d \ n ", Bst->data);}} int main () {TreeNode *a = new Treenode;a->data = 9;a->left = Null;a->right = Null;a = insert (5,a), a = insert (6,a), a = insert (10,a), a = insert (13,a), a = Delete (5,a), Showbintree (a), System ("pause"); Test Data 1 5 2 6 4//test Data 9 5 6 10 13

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

Creating a binary array Delete insert Lookup

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.