Binary search Tree Adt_bstree

Source: Internet
Author: User

A binary search tree or an empty binary tree, or a two-fork tree with the following properties:

1. If the left subtree is not empty, the key value of all nodes on the left subtree is smaller than the key value of the root node.

2. If the right subtree is not empty, the key values for all nodes on the right subtree are greater than the key values of the root node.

3. The left and right sub-trees are also two-fork search trees respectively.


Property: If you traverse a binary search tree in the middle order, you will get an ordered sequence with the increment of the keyword value.


1. Search implementation: If the binary tree is empty, the search fails. Otherwise, the X is compared to the root node. If x is less than the value of the node, searching the left subtree in the same way does not have to search the right sub-tree. If X

is greater than the value of the node, the right subtree is searched in the same way, without having to search the left sub-tree. If x equals the value of the node, the search is terminated successfully.

Search () is recursive, search1 () is an iterative search.

2. Insert implementation: When inserting a new element, the insertion position of the new element needs to be searched first. If the tree has duplicate elements, return to duplicate. The search reaches the empty tree, indicating that the tree does not pack

Contains repeating elements. The new node p becomes the root of the new binary tree when a new node p is constructed to hold the new element x, connected to the node Q, and becomes the child of the Q.

3. Delete implementation: When deleting an element, first search for the deleted node p, and record the parent node Q of P. If there are no deleted elements, return to notpresent.

(1) If P has two non-empty empty trees, then the direct successor node in the sequence of the search node P is set to S. Copy the value of S to p.

(2) If P has only one non-empty tree or P is a leaf, the only child with node P C or the empty tree c = null instead of P.

(3) If p is the root node, delete the Post node C becomes the new root. Otherwise, if p is the parent of the left child, then node C should also be the left child of Q, finally releasing the space occupied by node p.


Implementation code:

#include "iostream" #include "Cstdio" #include "CString" #include "algorithm" #include "Cmath" #include "utility" #include
"Map" #include "set" #include "vector" using namespace std;
typedef long Long LL;
const int MOD = 1e9 + 7; Enum resultcode{underflow, Overflow, Success, Duplicate, notpresent}; Assignment is 0, 1, 2, 3, 4, 5 template <class t> class Dynamicset {public:virtual ~dynamicset () {} virtual ResultCode
	Search (T &x) const = 0;
	Virtual ResultCode Insert (T &x) = 0;
	Virtual ResultCode Remove (T &x) = 0;

/* data */};
	Template <class t> struct Btnode {btnode () {lchild = Rchild = NULL;}
		Btnode (const T &x) {element = x;
	Lchild = Rchild = NULL;
		} btnode (const T &x, btnode<t> *l, btnode<t> *r) {element = x;
		Lchild = l;
	Rchild = R;
	} T element;
	Btnode<t> *lchild, *rchild;

/* data */}; 
	Template <class t> class bstree:public dynamicset<t> {public:explicit Bstree () {root = NULL;}//can only display conversionsVirtual ~bstree () {Clear (root);}
	ResultCode Search (T &x) const;
	ResultCode Search1 (T &x) const;
	ResultCode Insert (T &x);
ResultCode Remove (T &x);
Protected:btnode<t> *root;
	Private:void Clear (btnode<t> *t);
	ResultCode Search (btnode<t> *p, T &x) const;

/* data */};
		Template <class t> void Bstree<t>::clear (btnode<t> *t) {if (t) {Clear (T-lchild);
		Clear (T-rchild);
		cout << "Delete" << T-element << "..." << Endl;
	Delete T; }} template< class t> ResultCode bstree<t>::search (T &x) const {return Search (root, x);} template &lt
	; class t> ResultCode Bstree<t>::search (btnode<t> *p, T &x) const {if (!p) return notpresent;
	if (x < P-Element) return Search (P-lchild, X);
	if (x > P-Element) return Search (P-rchild, X);
	x = element p;
return Success; } template <class t> ResultCode bstree<t>::seArch1 (T &x) const {btnode<t> *p = root;
		while (p) {if (x < P-Element) p = P-lchild;
		else if (x > P-Element) p = P-rchild;
			else {x = P-element;
		return Success;
}} return notpresent;
	} template <class t> resultcode bstree<t>::insert (T &x) {btnode<t> *p = root, *q = NULL;
		while (p) {q = p;
		if (x < p, element) p = P-lchild;
		else if (x > P-Element) p = P-rchild;
			else {x = P-element;
		return Duplicate;
	}} p = new btnode<t> (x);
	if (!root) root = p;
	else if (x < Q-Element) Q-lchild = P;
	else Q-rchild = P;
return Success; } template <class t> resultcode bstree<t>::remove (T &x) {btnode<t> *c, *s, *r, *p = root, *q = N
	ULL;
		while (p && p, Element! = x) {q = p;
		if (x < p, element) p = P-lchild;
	else P = P-rchild;
	} if (!p) return notpresent;
	x = element p;if (P-lchild && P--rchild) {s = P, rchild;
		r = P;
			while (S-lchild) {R = s;
		s = S-lchild;
		} element = S---element p.
		p = s;
	Q = r;
	if (P-lchild) c = P--lchild;
	else C = P-rchild;
	if (p = = root) root = C;
	else if (p = = q-lchild) Q-lchild = C;
	else Q-rchild = C;
	Delete p;
return Success;
	} int main (int argc, char const *argv[]) {bstree<int> BST; int x = 28; Bst.
	Insert (x); x = 21; Bst.
	Insert (x); x = 25; Bst.
	Insert (x); x = 36; Bst.
	Insert (x); x = 33; Bst.
	Insert (x); x = 43; Bst.
	Insert (x);
return 0; }


Related Article

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.