csuoj-1005-Binary Search Tree analog

题目:

Description

Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following property:

  1. each node has a Key value, which can be used to compare with each other.
  2. For every node in the tree, every Key value in its left subtree is smaller than its own Key value.
  3. For every node in the tree, every Key value in its right subtree is equal to or larger than its own Key value.

Now we need to analog a BST, we only require one kind of operation: inserting.

First, we have an empty BST. Input is a sequence of numbers. We need to insert them one by one flowing the rules below:

If the inserted value is smaller than the root‘s value, insert it to the left subtree.

If the inserted value is larger than or equal to the value of the root‘s value, insert it to the right subtree.

After each input, we need to output the preorder, inorder, postorder traversal sequences.

About tree traversal, the following is from Wikipedia:

Depth-first Traversal

To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node:

  • Visit the root.
  • Traverse the left subtree.
  • Traverse the right subtree.

To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node:

  • Traverse the left subtree.
  • Visit the root.
  • Traverse the right subtree.

To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node:

  • Traverse the left subtree.
  • Traverse the right subtree.
  • Visit the root.

Look at the folowing example:

Intput is a sequence of 5 integers: 3 6 9 5 1

After each integer inserted the structure of the tree is illustrated in the flowing:

   3
 /   1      6
     /      5     9

Input

The first integer of the input is T, the number of test cases. Each test case has two lines. The first line contain an integer N,(1<=N<=1000), the number of numbers need to be inserted into the BST. The second line contain N integers separated by space, each integer is in the range of [0,230].

Output

Each test case, output must contain three lines: the preorder, inorder and postorder traversal sequence. The numbers in each line should be separated by a single space and you should not output anything at the end of the line! Output a blank line after each case.

Sample Input

1
5
3 6 9 5 1

Sample Output

3 1 6 5 9
1 3 5 6 9
1 5 9 6 3
分析:1,初始化:输入第一个数,将其左右孩子设置为-1;2,从树的根开始,与节点的值进行比较,若小于该值,则插入左子树,若大于等于,则插入右子树;3,若所要插入的子树为空,则插入在该位置。4,递归进行树的前序,中序,后序遍历,并输出结果。代码:
#include<iostream>
using namespace std;
struct Node{
    int key;
    int lchild;
    int rchild;
}node[1000];
int n;
int cnt;

void insert(int root,int i){
    if(node[i].key < node[root].key){
        if(node[root].lchild == -1) node[root].lchild = i;
        else insert(node[root].lchild,i);
    }
    else{
        if(node[root].rchild == -1) node[root].rchild = i;
        else insert(node[root].rchild,i);
    }
}

void traverse(int kind,int root){
    if(root != -1){
        if(kind == 1){
            cnt++;
            if(cnt < n) cout << node[root].key << " ";
            else cout << node[root].key << endl;
            traverse(kind,node[root].lchild);
            traverse(kind,node[root].rchild);
        }
        else if(kind == 2){
            traverse(kind,node[root].lchild);
            cnt++;
            if(cnt < n) cout << node[root].key << " ";
            else cout << node[root].key << endl;
            traverse(kind,node[root].rchild);
        }
        else{
            traverse(kind,node[root].lchild);
            traverse(kind,node[root].rchild);
            cnt++;
            if(cnt < n) cout << node[root].key << " ";
            else cout << node[root].key << endl;
        }
    }
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        for(int i = 0;i < n;i++){
            node[i].lchild = node[i].rchild = -1;
        }//for
        cin >> node[0].key;
        for(int i = 1;i < n;i++){
            cin >> node[i].key;
            insert(0,i);
        }//for
        cnt = 0;
        traverse(1,0);
        cnt = 0;
        traverse(2,0);
        cnt = 0;
        traverse(3,0);
        cout << endl;
    }
    return 0;
}
 
时间: 2024-10-07 22:38:48

csuoj-1005-Binary Search Tree analog的相关文章

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

1043. Is It a Binary Search Tree (25)

时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue (二叉树建立方法) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than th

pat1043. Is It a Binary Search Tree (25)

1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only

04-树6 Complete Binary Search Tree

刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两者即可解决问题! A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nod

pat1064. Complete Binary Search Tree (30)

1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only

1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less tha

1043 Is It a Binary Search Tree (25 分)

1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a no

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th