From two fork sorting tree to balanced binary tree to red black Tree Series 1

Source: Internet
Author: User

Recently want to write some about the red and Black tree blog, both want to write a comprehensive, but also intuitive, but do not know where to start. Start writing from the simplest two-fork sort tree.

Binary sorting tree (binary sort trees) is also called Binary search tree. It is a special structure of a two-fork tree. It is either an empty tree, or has the following properties:

(1) If its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node.

(2) if its right subtree is not empty, the value of all nodes on the left subtree is greater than the value of its root node.

Obviously, its middle order traversal is an ascending sequential sequence.

Theorem: On a two-fork search tree with a height of H, the operation Serach,minmum,maxmum,successor,predecessor on the dynamic set can be completed in O (h) time.

That is to find a given node, find the minimum value of the maximum value, and the predecessor of a given node, they can be completed in O (h) time. See the third edition of the introduction to the algorithm 12th chapter


Inserting and deleting

In order to maintain the ordered nature of the binary sorting tree, the insert and delete operations will cause the dynamic change of the binary sort tree, because the inserted node always becomes the leaf nodes, and the deleted nodes also modify the direction of the left and right sub-trees, so inserting a node is more complex than deleting a node.


Insert: First find the location where you want to insert the node, and then insert it.

Delete: Delete a node node there are three scenarios to consider:

1 node does not have left and right child nodes, this is the simplest case, delete it directly, and then place the pointer of the parent node pointing to node null.

2 node has only one child, then elevate the child to node and modify node's parent node.

3 node has two children. Then look for the successor Y of node (the next element in the middle sequence traversal), and let y occupy node's position. At the same time node's original right subtree part is called Y's New Right subtree,

Node's Zuozi is called the New left subtree of Y. Simply put: Swap the node node with the leftmost node position on the right subtree of node. this satisfies the sequential traversal increment property.

always remember that the order of the two forks is incremented, and when a node is deleted, its position is followed by its successor, that is, to delete the middle order of the right subtree of the node to traverse the first node (and also the leftmost node of the right subtree).

The following examples illustrate:



The following code is directly affixed:

#include <stdio.h> #include <stdlib.h> #define Error-1#define OK 1typedef struct bstnode{struct bstnode * Lchild,*rchild;int data;} bstnode,*bstree;/*** @auther: Jungege 2015 5.13**///insert element into two fork sort tree bstree insertbst (bstree bst,int key) {Bstree node,parent , new_node;if (BST = = null)//is an empty tree, creating a root node {BST = (bstree) malloc (sizeof (Bstnode)); bst->data = Key;bst->lchild = null; Bst->rchild = Null;return BST;} node = bst;//Find the insertion position, save Bstwhile (node! = NULL) {if (Node->data = = key) return bst;else{parent = node;//Record parent node, Last saved is the parent node of the Insert node if (Node->data < key) node = Node->rchild;elsenode = Node->lchild;}} Create the node, insert the new key into the specified location, the inserted node must be the leaf node New_node = (bstree) malloc (sizeof (Bstnode)), if (Parent->data < key)// Insert to Parent right child Parent->rchild = New_node;elseparent->lchild = New_node;new_node->data = key;new_node-> Lchild = Null;new_node->rchild = Null;return BST;} Delete binary sort tree element int Deletebst (bstree BST, int key) {Bstree node=bst,pre=null,nearestkey,nearest_pre;if (BST = = NULL) return- 1;//Empty Tree//Look for delete node location while (node! = NULL && node->data! = key) {pre = node;//Save parent node if (Node->data > key) node = Node->l Child;elsenode = Node->rchild;} if (node = = null) return 0;//no node to delete//Find node to delete and its parent node Preif ((Node->lchild! = null) && (node->rchild! =) NULL)//*** To delete the node has left and right child {Nearestkey = Node->rchild;nearest_pre = Node;while (nearestkey->lchild! = NULL) {Nearest_ Pre = Nearestkey;//pre points to the parent node of the Nearestkey node Nearestkey = nearestkey->lchild;} Node->data = nearestkey->data;//is directly assigned to avoid swapping nodes//processing Nearestkey node's child nodes, obviously it is the leftmost node, so there is no left subtree, just deal with its right subtree nearest_pre-> Lchild = Nearestkey->rchild;free (Nearestkey);} else if (Node->lchild = = NULL && node->rchild!=null)//*** to delete the node only right child {if (Pre->rchild = = node)// node is the right subtree of its parent node Pre->rchild = Node->rchild;else Pre->lchild = node->rchild;free (node);//Release Node}else if (node- >lchild! = NULL && node->rchild ==null)//*** to remove the node only left child {if (Pre->rchild = = node)//node is the right subtree of its parent node pre- >rchild = Node->lchild;elsePre->lchild = node->lchild;free (node);//Release node}else//*** deleted node No subtree {if (Pre->lchild = = node) Pre->lchild = Null;if (Pre->rchild = = node) Pre->rchild = null;free (node);} Return 1;//Delete succeeded}//find int searchbst (Bstree bst,int key) {printf ("%d", bst->data), if (bst==null) return error;if (bst- >data = = key) return Ok;else if (Key < Bst->data) Searchbst (Bst->lchild,key); Elsesearchbst (Bst->rchild, key);} The middle sequence iterates through void inorder (Bstree root) {if (root) {inorder (root->lchild);p rintf ("%d", root->data); Inorder (root-> rchild);}} void Main () {int a[10]={3,2,6,5,1,9,7,7,10,8};int i; Bstree root=null;//initialization is very important, otherwise it will not be possible to determine the tree empty//array build binary sort tree for (i=0;i<10;i++) root = Insertbst (Root,a[i]);//Sequence Traversal inorder ( Root);p rintf ("\ n Remove node 6:");d Eletebst (root,6);p rintf ("\ n Delete node 6 after the middle order Traversal:"), inorder (root);p rintf ("\ n");}


The results of the operation are as follows:



From two fork sorting tree to balanced binary tree to red black Tree Series 1

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.