Binary sorting tree (Binary Search Tree)

Source: Internet
Author: User

Reference: http://student.zjzk.cn/course_ware/data_structure/web/chazhao/chazhao9.3.1.1.htm

1 # include "stdafx. H "2 # include <iostream> 3 4 typedef int infotype; // other data fields, custom 5 typedef int keytype; // assume that the keyword type is Integer 6 typedef struct node // node type 7 {8 keytype key; // keyword 9 infotype otherinfo; // other data fields, infotype depends on the application. Next we will not handle it with 10 nodes * lchild, * rchild; // left and right child pointer 11} bstnode; 12 typedef bstnode * bstree; // bstree is the binary sorting tree type 13 14 // non-recursive 15 void insertbstnode (bstree * tptr, keytype key) 16 {// If the binary sorting tree * does not contain the keyword K in the tptr Key, then insert, otherwise, 17 bstnode * F, * P = * tptr; // The initial value of P points to the root node 18 while (P) 19 {20 if (p-> key = Key) // you do not need to insert 21 return; 22 f = P; // record 23 p = Key <p-> key? P-> lchild: p-> rchild; 24} 25 p = new bstnode; 26 p-> key = key; 27 p-> lchild = NULL; 28 p-> rchild = NULL; 29 If (null = * tptr) 30 * tptr = P; 31 else32 {33 If (Key <F-> key) 34 F-> lchild = P; 35 else36 F-> rchild = P; 37} 38} 39 40 bstree createbstree (keytype * array, int size) 41 {42 bstnode * root = NULL; 43 for (INT I = 0; I <size; ++ I) 44 insertbstnode (& root, array [I]); 45 return root; 46} 47 48 int _ tmain (INT argc, _ tchar * argv []) 49 {50 int array [] = {5, 3, 7, 2, 4, 8 }; 51 int size = sizeof (array)/sizeof (INT); 52 bstree tree = createbstree (array, size); 53 system ("pause"); 54 return 0; 55}

View the binary search tree built in debug mode, as shown in figure

 

(1) Insert and generate a binary sorting tree
① Process of inserting a new node into the binary sorting tree
To insert a new node into the binary sorting tree, ensure that the new node still meets the BST nature after insertion. The insert process is as follows:
(A) If the binary sorting tree T is empty, a new node is applied for the key of the keyword to be inserted and the node is the root;
(B) If the binary sorting tree T is not empty, the key and root keywords are compared:
(I) if the two are equal, it indicates that the key of this keyword already exists in the tree and does not need to be inserted.
(Ii) if the key is <t → key, the key is inserted into the left subtree of the root.
(Iii) If key> t → key, insert it into the right subtree of the root.
The Insert Process in the subtree is the same as that in the preceding tree. Continue until the key is inserted into the binary sorting tree as the key of a new leaf node, or until the key is found in the tree.

② Recursive algorithm for inserting a binary sorting tree into a new node 
[See bibliography]

③ Non-recursive algorithm for inserting a binary sorting tree into a new node
Void insertbst (bstree * tptr, keytype key)
{// If the key is not found in the binary sorting tree * tptr, It is inserted; otherwise, it is directly returned.
Bstnode * F, * P = * tptr; // The initial value of P points to the root node.
While (p) {// find the insert position
If (p-> key = Key) return; // you do not need to insert a key in the tree.
F = P; // F Save the node currently searched
P = (Key <p-> key )? P-> lchild: p-> rchild;
// If the key is <p-> key, search in the left subtree; otherwise, search in the right subtree.
} // Endwhile
P = (bstnode *) malloc (sizeof (bstnode ));
P-> key = key; P-> lchild = p-> rchild = NULL; // generate a new node
If (* tptr = NULL) // The original tree is empty.
* Tptr = P; // The newly inserted node is the new root.
Else // when the original tree is not empty, insert the new node P as the Left or Right child of the F-off
If (Key <F-> key)
F-> lchild = P;
Else F-> rchild = P;
} // Insertbst

④ Generate a binary sorting tree
The binary sorting tree is generated from an empty binary sorting tree. Every time a node data is input, an Insertion Algorithm is called to insert it into the generated binary sorting tree. The algorithm for generating the binary sorting tree is as follows:
Bstree createbst (void)
{// Enter a node sequence, create a binary sorting tree, and return the root node pointer
Bstree T = NULL; // The T is an empty tree at the initial time.
Keytype key;
Scanf ("% d", & Key); // a keyword for the reader
While (key) {// assume that key = 0 indicates the end of the loser
Insertbst (& T, key); // Insert the key into the binary sorting tree T
Scanf ("% d", & Key); // The next keyword of the reader
}
Return t; // return the root pointer of the binary sorting tree.
} // Bstree

⑤ Process of generating the binary sorting tree
The process of generating a binary sorting tree from an input instance (,) based on the binary sorting tree algorithm [see animation demonstration]
Note:
The input sequence determines the shape of the binary sorting tree.
The central sequence of the binary sorting tree is an ordered sequence. Therefore, to construct a binary sorting tree for any keyword sequence, the essence is to sort the sequence of this keyword to make it an ordered sequence. The name of the "Sort Tree" also comes from this. Generally, this sort is called tree sort, which proves that the average execution time of this sort is O (nlgn ).
For the same input instance, the execution time of tree sorting is about two to three times that of heap sorting. Therefore, in general, the purpose of constructing a binary sorting tree is not to sort, but to use it to accelerate search, this is because searching on an ordered set is usually faster than searching on an unordered set. Therefore, binary sorting trees are often called binary search trees.

Binary sorting tree (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.