Binary Search Tree (sorting tree, Search Tree) creation, search;

Source: Internet
Author: User

# Include <cstdio> <br/> # include <string. h> <br/> # include <algorithm> <br/> # include <stdlib. h> <br/> # include <iostream> <br/> using namespace STD; <br/> struct node <br/> {int val; <br/> node * left; <br/> node * right; <br/> node () <br/>{}< br/> node (int xx) <br/>{< br/> val = xx; left = right = NULL; <br/>}</P> <p>} tree; <br/> void build (node * tree, int X) // The process of building a tree is the process of Binary Search <br/>{</P> <p> If (tree-> Val> X) <br/>{< br /> If (tree-> left = NULL) <br/> tree-> left = new node (X ); <br/> else <br/> build (tree-> left, X ); </P> <p >}< br/> else if (tree-> Val <= X) <br/>{< br/> If (tree-> right = NULL) <br/> tree-> right = new node (X ); <br/> else <br/> build (tree-> right, X ); </P> <p >}< br/> void inorder (node * tree) <br/>{< br/> If (tree! = NULL) <br/>{< br/> inorder (tree-> left); <br/> printf ("% d", tree-> Val ); <br/> inorder (tree-> right); <br/> // printf ("% d", tree-> Val ); <br/>}< br/> // void postorder (node * tree) <br/> // {<br/> // If (tree! = NULL) <br/> // {<br/> // postorder (tree-> left ); <br/> // postorder (tree-> right); <br/> // Delete tree; <br/> //} <br/> int bsearch (node * tree, int key) // this search is a binary search process <br/>{< br/> If (tree = NULL) <br/> return-1; // indicates that no such data exists; search failed <br/> else <br/> {<br/> If (tree-> val = Key) // search successful <br/> return key; <br/> else if (tree-> Val> key) <br/> {<br/> bsearch (tree-> left, key ); <br/>}< br/> else if (tree-> Val <key) <br/>{< br/> bsearch (tree-> right, key ); <br/>}< br/> int main () <br/>{< br/> int X; <br/> int N; <br/> CIN> X; <br/> tree. val = x; <br/> n --; <br/> while (n --) <br/>{< br/> CIN> X; <br/> build (& tree, x); <br/>}< br/> inorder (& tree); // It is already sorted; <br/> printf ("\ n"); <br/> // postorder (& tree); // release the memory <br/> printf ("\ n "); <br/> cout <"Hello word" <Endl; <br/> return 0; <br/>}

 

The binary sorting tree, the binary search tree, And the Binary Search Tree refer to the same thing. They used to be confused by these concepts. Now they finally understand, the line segment tree is also a kind of deformation of the Binary Decision Tree. Therefore, it is a basic requirement to master the binary decision tree. In the future, there will be a lot of magical data structures in the binary decision tree, which has a long way to go;

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.