Complex algorithms for finding algorithm families: Two-fork sort tree BST

Source: Internet
Author: User

The previous summary of the sequential lookup, binary lookup, block lookup algorithm, this blog post will be detailed introduction to binary sorting algorithm (binary sort Tree).

Before introducing the binary sorting algorithm, we first describe what the binary sort tree (BST) is.

Start with the two-fork tree:


1. The concept of two-fork tree

A binary tree is an ordered tree with a maximum of two subtrees per node. Usually the root of the subtree is called the "left subtree" (Leftsubtree) and the "right subtree" (Rightsubtree). Binary trees are often used as binary search trees and two-fork or binary-ordered trees. Each node of a binary tree has a maximum of two subtrees trees (no nodes with a degree greater than 2), and the subtree of the binary tree has left and right points, and the order cannot be reversed. The first layer of the binary tree has a maximum of 2 i-1 nodes, and a two-fork tree with a depth of K has a maximum of 2^ (k)-1 nodes; for any binary tree T, if its terminal node number (that is, the leaf node number) is N0, the degree of 2 is n2, then N0 = N2 +1.

The chain storage structure of binary tree is a kind of important data structure, and its form is defined as follows:

Binary tree node  typedef struct bitnode{      //Data      char;      Left and right child pointer      struct Bitnode *lchild,*rchild;  } Bitnode,*bitree;  

2, the establishment of two-fork tree

By reading a string, the algorithm for building a two-fork tree is as follows:

Create two  -fork-Tree int createbitree (bitree &t) {      char data,      in order sequence) Enter the value of the node in the binary tree in order of precedence (one character), ' # ' for the Empty tree      scanf ("%c", &data);      if (data = = ' # ') {          T = NULL;      }      else{          T = (bitree) malloc (sizeof (Bitnode));          Generate root node          t->data = data;          Constructs left subtree          createbitree (t->lchild);          Construct right subtree          createbitree (t->rchild);      }      return 0;  }  


3, Binary Tree traversal

Traversal is one of the most basic operations of the tree, so-called traversal of the binary tree, that is, according to a certain rules and order all the nodes of the binary tree, so that each node is visited once, and only be visited once. Because the binary tree is a nonlinear structure, the traversal of the tree is essentially the transformation of each node of the two-fork tree into a linear sequence to be represented.

First-order traversal: access to the root node, access to the left child node, access to the right child node

Middle-order traversal: Access to left dial hand nodes, access to the root node, access to right child nodes

Post-post traversal: Access the left child node, access the right child node, access the root node

Hierarchical traversal: Each node is accessed hierarchically from the top to the bottom, from the top down, from left to right, and the queue is used during the hierarchy traversal.

In fact, knowing either way, and not uniquely determining the structure of the tree, but as long as you know the middle sequence traversal and any other way of traversal, you can definitely determine a tree.


First Order traversal void Preordertraverse (BST T) {if (t) {cout << t->key << "";p reordertraverse (T->lchild); Preordertraverse (T->rchild);}} Middle order traversal void Inordertraverse (BST T) {if (t) {inordertraverse (t->lchild); cout << t->key << ""; Inordertraverse (T->rchild);}} Post-sequential traversal of void Postordertraverse (BST T) {if (t) {postordertraverse (t->lchild);p ostordertraverse (t->rchild); cout << t->key << "";}} The hierarchy traversal of void Levelorder (BST T) {BST p = t;//Queue queue<bst> Queue;queue.push (p); while (!queue.empty ()) {p = Queue.front ( );cout<< p->key<< ""; Queue.pop (); if (p->lchild! = NULL) Queue.push (p->lchild); if (p->rchild! = NULL) Queue.push (P->rchild);}}



The concept of 4.BST trees

It is either an empty tree or a two-fork tree with the following properties:

(1) The Joz tree is not empty, then the value of all nodes on the left subtree is smaller than the root node of the tree where Zuozi is located;

(2) If the right subtree is not empty, then the value of all nodes on the right subtree is greater than the value of the root node of the tree where the right subtree is located;

(3) The left and right sub-trees are also two-fork sorting trees respectively;

The maximum keyword and the smallest key element: As the name implies, is definitely the maximum minimum value of the two-fork search tree, with the largest keyword as an example, always query the right child of the tree, until the node has no right child, the node is the largest keyword, of course, the least keyword the same;

Successor and precursor: for a node, the largest is less than the value of the node is the precursor, the smallest is greater than the value of the node is the successor. In the following example, if the right subtree of the node is not empty, then the successor is the smallest key element in the right subtree, and if the right child of the node does not exist, then only the node is looking up until the node is the left child of its parent node. Of course, the precursor is a similar situation;


5, the BST Tree search:

The time complexity is related to the depth of the tree, O (log n).

Step: Joghen The keyword value of the node is equal to the keyword found, success.

Otherwise: If the key value of the small root node, recursively check the left subtree.

Jordahugen the key value of the node and recursively checks the right subtree.

If the subtree is empty, the lookup is unsuccessful.


6, the insertion of BST tree

First, the search algorithm is performed to find the Father node of the inserted node.

The left son or the right son who is the father of the knot is judged by the insertion point. Insert nodes as leaf nodes.

If the binary tree is empty. First, the root node is generated separately.

Note: The newly inserted node is always the leaf node, so the algorithm complexity is O (h).

The establishment of the BST tree is the process of inserting sequentially.

7, the deletion of BST Tree

If the deleted node does not have a child, then the algorithm ends after deletion;

If the deleted node has only one child, then the child replaces the location of the deleted node after the deletion;

If you delete a node that has two children, select the node's successor (the node right child is the lowest point in the left subtree of the root tree) as the new root, and at the beginning of the subsequent node, recursively executes the delete algorithm until the leaf node, the deletion algorithm ends.

Write the implementation of BST to the BST.h file

#include <iostream> #include <queue>using namespace std;//BST node typedef int ELEMTYPE;TYPEDEF struct Node {      Elemtype key;  struct node *lchild, *rchild; }node, *bst; insert bool Bstinsert (BST & Root, Elemtype x) {if (root = NULL) {root = new Node;root->key = X;root->lchild = N in the BST tree Ull;root->rchild = Null;return true;} if (Root->key = = x) return false;if (x < Root->key) return Bstinsert (Root->lchild, x); return Bstinsert (root- &GT;RCHILD,X);} Create a BST tree void Createbst (BST &root, elemtype *x, int N) {root = null;for (int i = 0; i < N; i++) {Bstinsert (root,x[i]);} }//first-order traversal of void Preordertraverse (BST T) {if (t) {cout << t->key << "";p reordertraverse (T->lchild); Preordertraverse (T->rchild);}} Middle order traversal void Inordertraverse (BST T) {if (t) {inordertraverse (t->lchild); cout << t->key << ""; Inordertraverse (T->rchild);}} Post-sequential traversal of void Postordertraverse (BST T) {if (t) {postordertraverse (t->lchild);p ostordertraverse (t->rchild); cout <&Lt T->key << "";}} The hierarchy traversal of void Levelorder (BST T) {BST p = t;//Queue queue<bst> Queue;queue.push (p); while (!queue.empty ()) {p = Queue.front ( );cout<< p->key<< ""; Queue.pop (); if (p->lchild! = NULL) Queue.push (p->lchild); if (p->rchild! = NULL) Queue.push (P->rchild);}} Find element, recursive algorithm, find the node pointer that returns the keyword, not found return Nullbst bstsearch (BST root, Elemtype key)//recursive algorithm {if (root = NULL) return null;if (root- >key = = key) return root;if (Key > Root->key) return Bstsearch (Root->rchild, key); Elsereturn Bstsearch (root-& Gt;lchild, key);} Lookup element, non-recursive algorithm BST BSTSEARCH2 (BST Root, elemtype key) {if (root = null) return null; BST p = root;while (P->key! = key) {if (Key < P->key) P = p->lchild;elsep = p->rchild;if (p = = null) return null ;} return p;}    Find the minimum keyword, null when the tree returns null BST searchmin (BST Root) {if (root = NULL) return root;      while (root->lchild! = NULL) root = root->lchild;  return root; }//Find the maximum keyword, empty tree returns null BST Searchmax (BST Root) {if (root = NULL) return root;      while (root->rchild! = NULL)//Iteration root = root->rchild;  return root; }//Find precursor, that is, the largest number in the left subtree that is smaller than the current number, if there is no left subtree, complex, temporarily does not implement BST searchpredecessor (BST Root) {if (root = NULL) return root;//If there is a left child, The precursor is the largest if (root->lchild! = NULL) return Searchmax (Root->lchild) in its left subtree;//There is no left child, need to backtrack to find its parent,  It is therefore necessary to add a parent/BST y = root->parent; in the sruct   while (y = NULL && root = = Y->lchild)//{//root = y;   y = root->parent;  }//return y;        }//Find subsequent BST searchsuccessor (BST Root) {if (root = NULL)//null return root;   If there is a right child, the successor is the smallest if (root->rchild! = NULL) return searchmin (Root->rchild) in its right subtree;  BST y = root->parent;   while (y = NULL && root = = Y->rchild)//{//root = y;   y = root->parent;  }//return y; }
BST Bstdelete (BST Root, elemtype key) {BST p,q;if (root==null) return root;if (Root->key = = key) {if (Root->lchild = = Null && Root->rchild = = null) {free (root); return null;} else if (Root->lchild = = NULL) {p = root->rchild;free (root); return p;} else if (Root->rchild = = NULL) {p = root->lchild;free (root); return p;} else{p = q = Root->rchild;while (p->lchild!=null) p = P->lchild;p->lchild = Root->lchild;free (root); return (q);}} if (Root->key > key && root->lchild!=null) root->lchild = Bstdelete (Root->lchild,key); if (root- >key < key && Root->rchild!=null) root->rchild = Bstdelete (Root->rchild,key); return root;}



This article refers to the article _luffy: http://blog.csdn.net/xjm199/article/details/20003045

And another article: http://www.open-open.com/bbs/view/1410494404789


Complex algorithms for finding algorithm families: Two-fork sort tree BST

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.