Binary search Tree

Source: Internet
Author: User

Introduction to Binary search tree:

the two-fork search tree usually takes a binary chain as the storage structure of the binary search tree. In order to get the ordered sequence of a key word, an unordered sequence can be constructed by constructing a binary sort tree, and the process of constructing the tree is the process of ordering the unordered sequence. Each new node that is inserted is a new leaf node on the two-fork sort tree, and the insertion operation does not have to move other nodes, just change the pointer of a node, and the null becomes non-empty. Search, insert, delete complexity equals tree height,O (log (n)), and in the worst case, the binary search tree will degenerate into a linked list. In order to solve this problem, people put forward a balanced search tree;

Binary Search Tree Properties:
1. Each node has a key code that is used as the basis for the search, and the key codes for all nodes are different.

2. The key code (key) for all nodes on the left subtree is small for the root node key code (key).

3. All nodes on the right subtree have key codes (keys) that are larger than the root node's key code (key).

4. The left and right sub-trees are two-fork search trees.

/* Node Structure */
template<class k>struct node{public: Node (K key): Key (Key), left ( NULL), right (null) {} K key; Node<k>* left ; Node<k>* right ;};

/ * Balance Search Tree class * /
template<classK>classsearchbinarytree{ Public: Searchbinarytree (K*key,size_t size): _root (NULL) { for(inti =0; i < size; ++i)//creat (_root, key[i]);Creat_nor (_root,key[i]); } BOOLCreat (node<k>*&root, K key); //Recursive creation BOOLCreat_nor (node<k>*&root, K key); //non-recursive creation BOOLDelete (node<k>*root, K key); //Delete a node Node<k>* Find (node<k>*root, K key); //Find a node Node<k>* Find_nor (node<k>*root, K key); //non-recursive lookup of a node voidInorder (node<k>*root); //Middle sequence traversal Public: Node<k>*_root;};

/ * Implementation of related functions * /
template<classK>BOOLSearchbinarytree<k>::creat (node<k>*&root,k Key) { if(Root = =NULL) {Root=NewNode<k>(key); return true; } if(Key > Root->key)returnCreat (root->Right , key); Else if(Key < Root->key)returnCreat (root->Left , key); Else return false; }
Template<classK>BOOLSearchbinarytree<k>::creat_nor (node<k>*&root, K key) { if(Root = =NULL) {Root=Newnode<int>(key); return true; } Node<k>* prev =NULL; Node<k>* cur =Root; while(cur) {prev=cur; if(Key > Cur->key) {cur= cur->Right ; } Else if(Key < Cur->key) {cur= cur->Left ; } Else return false; } if(Prev->key <key) {prev->right =NewNode<k>(key); } Else{prev->left =NewNode<k>(key); } return true;}
Template<classK>BOOLSearchbinarytree<k>::D elete (node<k>*root, K key) { if(Root = =NULL) { return false; } Node<k>* prev =NULL; while(root) {if(Key > Root->key) {prev=Root; Root= root->Right ; } Else if(Key < Root->key) {prev=Root; Root= root->Left ; } Else //The corresponding node needs to be deleted. { if(Root->left = = NULL)//left is empty { if(prev = =NULL) {_root= root->Right ; } Else if(Root->key > prev->key) {prev->right = root->Right ; } Else{prev->left = root->Right ; } DeleteRoot; } Else if(Root->right = = NULL)//right is empty { if(prev = =NULL) {_root= root->Left ; } Else if(Root->key > prev->key) {prev->right = root->Left ; } Else{prev->left = root->Left ; } } Else //Both sides are not empty { if(prev = =NULL) {_root=Root; } Node<k>* tmp = root->Right ; Prev= root;//The leftmost node of Root's right subtree is root->right, which ensures that Prev is always the parent node of TMP while(tmp->Left ) {prev=tmp; TMP= tmp->Left ; } Swap (TMP->key, root->key); Prev= = root? Prev->right = Tmp->right:prev->left = tmp->Right ; } return true; } } return false;}



When the left child, right child is not empty, why should the right subtree of the left child and root exchange, because: after Exchange can guarantee Root->left->key < Root->key && root->right- >key > Root->key
Which makes the search binary tree still orderly.

Template<classK>Node<k>* Searchbinarytree<k>::find (node<k>*root, K key) { if(Root = =NULL) { returnNULL; } if(Key > Root->key) { returnFind (root->Right , key); } Else if(Key < Root->key) { returnFind (root->Left , key); } Else { returnRoot; }}template<classK>Node<k>* Searchbinarytree<k>::find_nor (node<k>*root, K key) { if(Root = =NULL) { returnNULL; } while(root) {if(Key > Root->key) Root= root->Right ; Else if(Key < Root->key) Root= root->Left ; Else returnRoot; } returnNULL;} Template<classK>voidSearchbinarytree<k>::inorder (node<k>*root) { if(Root = =NULL) { return; } inorder (Root-Left ); cout<< Root->key <<" "; Inorder (Root-Right );}

Binary search tree, it is here, welcome to participate in the review, please feel free to enlighten ~ ~

Http://www.cnblogs.com/yangecnu/p/Introduce-Binary-Search-Tree.html

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.