Binary search tree (binary sort tree)

Source: Internet
Author: User
Tags bool class definition sort

Content and Code reference Yin version of "Data structure"

Binary search tree is also called binary sort tree, binary search tree is either an empty tree or has the following properties:

(1) Each node has a node value as the basis for the search, and each node value is different;

(2) All node values on Zuozi (if present) are smaller than the value of the root node;

(3) All node values on the right subtree (if present) are greater than the values of the root node;

(4) Saozi right subtree is also a binary search tree.


A binary search tree in the middle sequence traversal, can be from small to large to arrange the node value. note here that the same set of data can be constructed with different two-fork search trees depending on the arrangement. For a set of n key codes, there are different permutations in the n!, which can form a different two-fork search tree.

The average time cost for binary search tree search, insert, and delete operations is O (LOGN).


Structure definition of binary search tree:

The class definition of the struct
template<class t>
struct bstree{
	T data;
	Bstree* left, right;
	Bstree (): Left (null), right (null) {};
};


Binary search Tree Lookup:(Note: I look at the other data code, some lookup operations return void, if the return void will not know whether to return success; Lookup operation should return the found tree node)

Find
template<class t>
bstree* Search (const T x, bstree* ptr) {
	if (ptr = = null) return null;   Search failed
	Else if (x < ptr->data) search (x, ptr->left);
	else if (x > Ptr->data) search (x, ptr->right);
	else return ptr;  Search Success
}


Binary search Tree insertion:(note: To insert an element into a binary search tree, you must first check if the element already exists in the tree)

Insert
template<class t>
bool Insert (const T x, bstree* & ptr) {
	if (ptr = = NULL) {    // The new node is inserted as a leaf node in
		ptr = new Bstree (x);   Create a new node
		if (ptr = = NULL) {cerr<< "Out of Space" <<endl; exit ( -1)};
		return true;
	}
	else if (x < ptr->data) insert (x,ptr->left);   Search for the
	else if (x > Ptr->data) Insert (x,ptr->right) to the left dial hand tree;  Right subtree search for
	else return false;   This indicates that the element to be inserted already exists in the tree and returns false
}


Binary Search Tree deletion: (there are three ways to delete a binary search tree: 1. The right sub-tree of the node is empty, then it is filled with Zuozi; 2, the left subtree of the node is empty, and the right subtree is filled; 3. The left and right subtree of the node is not empty, then the first node in the sequence traversal of it is filled, and then the right subtree is processed recursively.

Delete
template<class t>
bool Remove (const T x, bstree* & ptr) {
	bstree* temp;
	if (ptr! = NULL) {
		if (x < Ptr->data) remove (x,ptr->left);
		else if (x > Ptr->data) Remove (x, ptr->right);
		else if (ptr->left! = NULL && ptr->right! = null) {  ///left and right subtrees are not empty
			temp = ptr->right;
			while (temp->left! = NULL) temp = temp->left;  Fill
			ptr->data = temp->data;
			with the first node in its right sub-tree under sequence traversal Remove (Ptr->data, ptr->right);  Then to delete the node that was taken to fill, recursively handle the right subtree
		}
		else{
			temp = ptr;
			if (Ptr->left = = NULL) ptr = ptr->right;
			else ptr = ptr->left;
			Delete temp;
			return true;
		}
	}
	Reurn false;
}



The above operations are implemented in a recursive way, I think the recursive way efficiency, although not high, but it looks really comfortable ah =_=, so directly with the way of recursion, and later have time to use non-recursive way to achieve it again.

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.