Binary Search Tree

Source: Internet
Author: User

Binary Search Tree:

Binary Tree search is simple. You can search for Binary Trees in ascending order and then in descending order. At the beginning, you must determine whether the query is empty.

To insert a node, check whether it exists. Record its parent node at the same time during the search until an empty node is found and insert the node.

Deletion is more complex:

Judge one by one:

First, judge whether it is null, find the node P to be deleted, and record its parent node Q. If it cannot be found, false is returned;

When the P node has two Subtrees, it finds the successor nodes that traverse in order, that is, after sorting, it is thought as S. Record the parent node R of s while searching, then copy the value of S to P, then use S as P, R as Q, convert the problem to the case where only one subtree exists on the P node, that is, the later case. This is because it is the first node in the middle-order traversal. At this time, the P point has no left subtree.

When a p node has only one subtree or is absent, assign its subtree or null a value of C to the child node of P, and then let it replace P in the Q position.

If the P node is the root node, the C node is the root node.

Release P memory.


The Code is as follows:


#include "BSTree.h"template<typename T>bool BSTree<T>::Search(const T& x) const{if(!root) return false;return Search(root,x);}template<typename T>bool BSTree<T>::Search(const BTNode<T> *p, const T& x)const{if(!p)return false;if(p->element == x)return true;if(p->lchild)return Search(p->lchild,x);if(p->rchild)return Search(p->rchild,x);return false;}template<typename T>bool BSTree<T>:: Insert(const T& x){if(!root)root = new BTNode<T>(x);else{BTNode<T>* p = root,q = 0;while(p){q = p;if(p->element==x)return false;else if(x < p->element)p = p->lchild;else p = p->rchild;}p = new BTNode<T>(x);q -> rchild = p;}return true;}template<typename T>bool BSTree<T>::Remove(const T& x){BTNode<T> *c,*s,*r,*p = root,*q = 0;if(p)return false;while(p &&p->element != x){q = p;if(x < p->element) p = p->lchild;else p =  p->rchild;}if(!p ) return false;if(p->lchild && p->rchild){s = p->rchild;r = p;while(r->lchild){r = s;s = s->lchild;}p->element = s->element;p = s;q = r;}if(p ->lchild ) c = p->rchild;else c = p->lchild;if( p == root) root = c;else if(p == q->lchild) q->lchild = c;else q->rchild  = c;delete p;return true;}



Header file:

#ifndef BSTREE#define BSTREEtemplate <typename T>struct BTNode{BTNode(){lchild =rchild = 0;}BTNode(const T& x){element = x;lchild =rchild = 0;}BTNode<T>* lchild,*rchild;T element;}; template <typename T>class BSTree{public:BSTree(){root = 0;}bool Search(const T& x) const;bool Insert(const T& x);bool Remove(const T& x);private:bool Search(const BTNode<T> *p, const T& x)const;protected:BTNode<T>* root;};#endif






Binary Search 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.