#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