Binary search Tree

Source: Internet
Author: User

The reference links are as follows:http://www.cnblogs.com/unpolishedgem/archive/2012/05/12/2494770.html

http://blog.csdn.net/wypblog/article/details/7530826

Http://www.cnblogs.com/aiyelinglong/archive/2012/03/27/2419972.htmlTwo fork find tree definition

Binary search tree (binary search trees), (also: two fork search tree, binary sort tree) it is either an empty tree or a two-tree with the following properties: if its left subtree is not empty, then the value of all nodes on the left subtree is less than the value of its root node; if its right subtree is not empty, The value of all nodes on the right subtree is not less than the value of its root node, and its left and right subtrees are also two-fork sort trees.

the two-fork sorting tree is similar to the two-fork tree, and usually takes the binary list as the storage structure of the binary sort 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. The complexity of search, insert, and delete equals tree height o (log (n)).

Features: 1. From the root node to the left, know that there is no left to go, that is, to get the smallest element, from the root node to the right to the left, until there is no left to go, that is to get the largest element.

2. The successor node of a node must have no left subtree, or no right subtree; the precursor node of a node must have no right sub-tree, or no left subtree.

Two fork Find tree insert Operation

The insertion process for the binary lookup tree is as follows: 1. If the current two-fork lookup tree is empty, the inserted element is the root node, and 2. If the inserted element value is less than the root node value, the element is inserted into the left subtree, and 3. If the inserted element value is not small, the element is inserted into the right subtree.

find operations for two-fork find treeThe process of finding X in binary sort tree B is: If B is an empty tree, then the search fails if x equals the value of the data field of the root node of B, the lookup succeeds if X is less than the value of the data field of the root node of B, find Zuozi; otherwise, find the right subtree. Two delete operation of the fork Treeafter removing a node from the two-fork lookup tree, the remaining nodes may not satisfy the nature of the binary lookup tree, so the tree is adjusted after the node is deleted to satisfy the nature of the binary lookup tree. According to the number of children in the node, the deletion is divided into three cases, we will delete the node is Z, actually delete the node is Y.

1. The Z-node has no children.
As shown in a, we want to delete a node with a value of 13, because the node has no children, so it does not affect the overall nature of the two fork tree, and
That is, the 13 node can be deleted directly, A is shown, from the left two fork tree Delete 13 This point to the right of the two fork tree.



2. Z node has a child.
As shown in B, to delete a node with a value of 16 has a child, and is the right child, then from the figure, if we take 16 off, and then 20 as the node of the subtree as the right subtree of 15, then the whole tree is consistent with the nature of the binary search tree, so there is a child's node deletion operation, is to have their child as the parent node of the child. As shown in B.



3. The Z-node has two children.

As shown in C, to delete a node with a value of 5, there are two children, after deletion it is certain that the whole tree does not conform to the nature of the binary search tree, so
To make adjustments, we found that 5 of the successor, the value of 6 of the node to put to 5 position, and then 6 of the child 7 as 6 of the parent node 10 of the child, such as C
As shown, we're going to delete the Z-node, and we're actually going to delete the Y-node and replace the Z-node. The point to note here is that if a node has a right
Child, the successor of the node, at most one child, and the right child. Because if the successor of the node has left child and right child, then his left child
The value must be somewhere between the node and its successor, then according to the nature of the binary search tree, the left child should be the successor of that node, so this
Conflicting with the previous successor, therefore, the conclusion is established.




The program code is as follows:searchBiTree.h File Contents
#ifndef Searchbitree_h#define searchbitree_h#include <iostream>//defines a two-fork tree node structure typedef struct BINODE{INT data; struct Binode *lchild;struct binode *rchild; Binode (int value):d ata (value), lchild (null), rchild (null) {}}*bitree;//Two fork lookup tree lookup algorithm, returns false if it does not exist, returns Turebool Searchvalue (bitree root,int value) {while (root!=null) {if (Root->data==value) return True;else if (root->data> Value) Root=root->lchild;else Root=root->rchild;} return false;} Binary search Tree insertion algorithm (build two-fork tree algorithm) void Insertsearchtree (Bitree &root,int value) {///If it is an empty tree, insert if (root==null) {root=new Binode ( value);} else{//The root node value is greater than the insertion value, insert the left subtree if (root->data>value) Insertsearchtree (root->lchild,value); else// Insert right subtree Insertsearchtree (root->rchild,value);}} The middle sequence traverses the binary lookup tree, resulting in an ordered sequence of void Inordertraverse (Bitree root) {if (root!=null) {inordertraverse (root->lchild); std:: cout<<root->data<< ""; Inordertraverse (Root->rchild);}} Get two fork find tree max, if empty tree, then return -1int Findmax (Bitree root) {if (root==null) return-1;while (root->rchild!=null) {root=root-& GT;rchild;} return root->data;} Get two fork find tree minimum, if empty tree, return -1int findmin (Bitree root) {if (root==null) return-1;while (root->lchild!=null) {root=root- >lchild;} return root->data;}  Get the parent node to delete node and its own node pointer void findpostion (bitree root, int deletevalue, bitree& deletenode,bitree& parentnode) { Deletenode=root;while (deletenode!=null) {if (deletenode->data==deletevalue) {return;} else if (deletenode->data>deletevalue) {parentnode=deletenode;deletenode=deletenode->lchild;}   Else{parentnode=deletenode;deletenode=deletenode->rchild;}}} Binary lookup tree Delete algorithm void Deletesearchtree (Bitree &root,int deletevalue) {Bitree deletenode=null,parentnode=null;// A pointer to the node to be deleted and a pointer to the parent findpostion (Root,deletevalue,deletenode,parentnode);//std::cout<<parentnode->data << "" <<deletenode->data<<std::endl;//the node to be deleted is the leaf node (special case only one root node, need special treatment) if (deletenode-> Lchild==null && deletenode->rchild==null) {if (deletenode=parentnode->lchild) parentnode->lchild= Null;else if (DeleteNoDe=parentnode->rchild) parentnode->rchild=null;else//deleted is the root node parentnode=null;delete deleteNode;} else if (deletenode->lchild==null && deletenode->rchild!=null) {//delete node only right subtree if (root->data== Deletenode->data)//delete the root node root=deletenode->rchild;else{if (deletenode==parentnode->lchild) parentNode- >lchild=deletenode->rchild;elseparentnode->rchild=deletenode->rchild;} Delete Deletenode;} else if (deletenode->lchild!=null && deletenode->rchild==null) {//delete node only left dial hand tree if (root->data== Deletenode->data)//delete the root node root=deletenode->rchild;else{if (deletenode==parentnode->lchild) parentNode- >lchild=deletenode->lchild;elseparentnode->rchild=deletenode->lchild;} Delete Deletenode;}          else{//Delete node has both Zuozi and right subtree bitree temp = deletenode->lchild;  Bitree tempparent = Deletenode;              Locate the immediate predecessor while (temp->rchild! = NULL) of the node to be deleted {tempparent = temp;          temp = temp->rchild; }//Exchange The value of the node to be deleted and its precursor node DelEtenode->data = temp->data;            Here are two scenarios to consider, specific self-drawing analysis if (Tempparent->lchild = = temp) {Tempparent->lchild = temp->lchild;          }else{Tempparent->rchild = temp->lchild; } delete temp;}} #endif

Main.cpp File Contents
#include <iostream> #include "searchBiTree.h" using namespace Std;int Main () {Bitree root=null;int value[]={ 15,5,16,3,12,20,10,13,18,23,6,7};//Insert build two fork find tree for (int i=0;i<12;i++) {insertsearchtree (root,value[i]);} Middle sequence Traversal binary search tree, get ordered sequence cout<< "middle order traversal binary search tree:"; inordertraverse (root); cout<<endl;//get two fork find tree Max cout<< " Binary find tree Max: "<<findmax (Root) <<endl;//get two fork find tree minimum cout<<" binary find tree Max: "<<findmin (Root) << endl;//Two fork find tree lookup algorithm cout<< "Find All:" <<boolalpha<<searchvalue (root,11) <<endl;cout<< "Find 12 : "<<searchvalue (root,12) <<endl;//two fork find tree delete algorithm cout<<" Delete node 12 after output: ";D eletesearchtree (root,12); Nordertraverse (root); return 0;}

Program run

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.