#include <iostream>using namespace std;typedef struct Bitnode{int data;struct bitnode *lchild, *rchild;} Bitnode,*bitree;class Solution{public:bool searchbst (bitree root, int key, Bitree F, Bitree *p) {if (!root) {*p = F;return f Alse;} else if (Root->data = = key) {*p = Root;return true;} else if (Key < Root->data) {Searchbst (Root->lchild, key, Root, p);} Else{searchbst (Root->rchild, key, Root, p);}} BOOL Insertnode (Bitree *root,int key) {Bitree p;if (!searchbst (*root, Key, NULL, &p)) {bitnode* s = new Bitnode;s->da Ta = key;s->lchild = S->rchild = Null;if (!p) {*root = s;} else if (Key < P->data) {p->lchild = s;} Else{p->rchild = s;} return true;} Else{return false;}} void Printtree (Bitree root) {if (root = NULL) Return;printtree (root->lchild); cout << root->data << ""; Printtree (root->rchild);} Bitnode* maxValue (Bitree root) {if (!root) return NULL; bitnode* s = root;while (s->rchild) {s = s->rchild;} return s;} bitnode* MinValue (Bitree roOT) {if (!root) return NULL; bitnode* s = root;while (s->lchild) {s = s->lchild;} return s;} BOOL Delete (bitree* p) {Bitree q,s;if ((*p)->lchild = = NULL) {Bitree q = *p;*p = (*p)->rchild;delete q;} else if ((*p)->rchild = = NULL) {Bitree q = *p;*p = (*p)->lchild;delete q;} Else{s = (*p)->lchild;q = *p;while (s->rchild) {q = S;s = S->rchild;} (*p)->data = s->data;if (*p = = q) {q->lchild = S->lchild;} Else{q->rchild = S->lchild;} Delete S;} return true;} BOOL Deletebst (bitree* root, int key) {if (!root &&!) ( *root)) return false;else{if (key = = (*root)->data) Delete (root), else if (Key < (*root)->data) return Deletebst (& amp; ((*root)->lchild), key) Elsereturn Deletebst (& ((*root)->rchild), key);}}; int main () {solution S; Bitree Root=null;int A[10] = {6, 4, 8, 5, 0, 9, 3, 7, 1, 2};for (int i = 0; i < ++i) {S.insertnode (&root, A[i] );} cout << "pre-sequence Traversal results:" << endl;s.printtree (Root), cout << endl;cout << "Max:" << Endl;cout << (Root)->data << endl;cout << "min:" << endl;cout << ( S.minvalue (Root)->data << endl;cout << "Find a node with a value of ' 3 ':" << Endl; Bitree p;cout << (S.searchbst (Root, 3, NULL, &p)? "Exist": "No Exist") << endl;cout << (S.searchbst (Root, 8, NULL, &p)? "Exist": "No Exist") << endl;cout << (S.searchbst (root, all, NULL, &p)? "Exist": "No Exist") << endl;cout << "Delete node with value 3:" << Endl;s.deletebst (&root, 3); S.printtree (root); cout << Endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Binary search tree (c + +)