[binary search tree] Binary sort tree

Source: Internet
Author: User
Tags cmath

Title Description

Enter a series of integers, establish a two-fork sort number, and carry out pre-order, middle-order, post-sequence traversal.

Input

Enter the first line to include an integer n (1<=n<=100). The next line consists of n Integers.

Output

There may be more than one set of test data, for each set of data, the problem of the data to create a binary sorting tree, and the two-fork sorting tree for the pre-order, middle order and Post-sequence Traversal. Each of the traversal results outputs a ROW. There is a space after the last data in each ROW.

Sample input1 2 2 8 4-5Sample output2 2 2 8 , 8, 8, 5, 5-ten 5

Analysis: the topic involves the establishment of Two-fork search tree, the first sequence traversal, the sequence traversal, the Post-order traversal. The establishment of binary search tree is simply extended on the basis of the establishment of the common binary tree. The first, middle, and post-sequential traversal here is the same as the normal binary tree.

#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<vector>#include<cmath>#include<queue>using namespacestd;structnode{intdata; Node* lchild,*rchild;};voidInsert (node * & root,intData) {    if(root==NULL) {root=Newnode; Root->data=data; Root->lchild=NULL; Root->rchild=NULL; return ; }    if(data==root->Data) {        return ; }    Else if(data<root->Data) {insert (root-lchild,data); }    Else{insert (root-rchild,data); }}voidPreorder (node *Root) {    if(root!=NULL) {cout<<root->data<<" "; Preorder (root-lchild); Preorder (root-rchild); }}voidInorder (node *Root) {    if(root!=NULL) {inorder (root-lchild); cout<<root->data<<" "; Inorder (root-rchild); }}voidPostorder (node *Root) {    if(root!=NULL) {postorder (root-lchild); Postorder (root-rchild); cout<<root->data<<" "; }}intmain () {intn;  while(cin>>N) {node* root=NULL;  for(intI=0; i<n;i++)        {            inta; CIN>>a;        Insert (root,a);        } Preorder (root); cout<<endl;        Inorder (root); cout<<endl;        Postorder (root); cout<<endl; }}

[binary search tree] Binary sort 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.