1. 程式人生 > >04-樹6 Complete Binary Search Tree (30分)

04-樹6 Complete Binary Search Tree (30分)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

二叉搜尋樹(BST)被遞迴地定義為具有以下屬性的二叉樹:

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 with keys greater than or equal to the node's key.

節點的右子樹僅包含與鍵大於或等於節點的關鍵節點。

Both the left and right subtrees must also be binary search trees.

左和右子樹都必須是二叉搜尋樹。

A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

完全二進位制樹(CBT)是完全填充的樹,除了最底層例外,左到右都被填充滿。                                                                                                   

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.   

現在給定不同的非負整數鍵的序列,如果可以構造唯一的BST則需要樹必須也是CBT。 您應該輸出此BST的層次遍歷序列。

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (\le 10001000). Then  distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.   

每個輸入檔案包含一個測試用例。 對於每種情況,第一行包含正整數N(≤1000)。 然後下一行給出N個不同的非負整數鍵。 一行中的所有數字由一個空格分隔,並且不大於2000。

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. 

對於每個測試用例,在一行中列印相應的完整二叉搜尋樹的層次遍歷序列。一行中的所有數字必須用空格分隔,並且在行尾必須沒有額外的空格。

Sample Input:

10
1 2 3 4 5 6 7 8 9 0

Sample Output:

6 3 8 1 5 7 9 0 2 4

思路:

二叉搜尋樹中序遍歷序列自然有序!順序從小到大!

二叉搜尋樹的左節點<根節點<右結點,中序遍歷的順序為左->根->右,正好就是從小到大的順序。

又因為是完全二叉搜尋樹,根節點為0時,節點i的左孩子和右孩子分別是2i和2i+1, 因此可以用陣列實現。

首先要對輸入的資料進行排序。STL中有函式sort,是可以直接對陣列進行排序,複雜度為n*log2(n),預設為升序排列。用這個函式,需要的標頭檔案如下:

#include <algorithm>
using namespace std;

sort(a,a+n); //兩個引數分別為待排序陣列的首地址和尾地址

但是,也可以自己寫一個cmp函式,按照自己的意願進行排序。比如下面這個sort,就是降序排序。
int cmp( const int &a, const int &b )
{
	if( a > b )
	return 1;
	else
	return 0;
}
sort(a,a+n,cmp);//對陣列a降序排序

這道題的思路是:將輸入的資料用sort函式從小到大排序->得到的序列是二叉搜尋樹的中序遍歷序列->輸出二叉搜尋樹的層次遍歷序列。
#include <iostream>
#include <algorithm>

using namespace std;

int a[1000];//儲存輸入的資料 
int b[1000];//儲存輸出的資料 
int len=0;	 

void trans(int a[],int b[],int n,int root)
{
	
	if(root*2<=n)	//如果有左孩子 
		trans(a,b,n,root*2);
		
	b[root]=a[len++];	//中序遍歷陣列a,將a的結點按遍歷順序放在陣列b中 

	if(root*2+1<=n)	//如果有右孩子 
		trans(a,b,n,root*2+1);
	
}

int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];		//輸入鍵值,存放在陣列a中 
	}
	sort(a,a+n);	//把陣列a的第0到n-1的元素排序
	trans(a,b,n,1);
	for(int i=1;i<=n-1;i++)
	{
		cout<<b[i]<<" ";	
	}
	cout<<b[n];
	return 0;
} 

相關推薦

04-6 Complete Binary Search Tree (30)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: 二叉搜尋樹(BST)被遞迴地定義為具有以下屬性的二叉樹: T

根據中序遍歷順序構建完全二叉搜尋-04-6 Complete Binary Search Tree (30)

題目分析 題目就是給出一棵完全二叉搜尋樹的各個結點的值,然後讓我們輸出該樹層序遍歷的結果。 我們首先可以分析一下,完全二叉搜尋樹 有什麼特點?顯然可以知道: 1.它是一棵完全二叉樹,那麼可以用

04-6 Complete Binary Search Tree30

null pro arc line 結束 his ott hat sin 04-樹6 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a

04-6 Complete Binary Search Tree30

題目來源:中國大學MOOC-陳越、何欽銘-資料結構-2018春 作者: 陳越 單位: 浙江大學 問題描述: A Binary Search Tree (BST) is recursively defined as a binary tree whic

c語言 04-6 Complete Binary Search Tree

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains

《資料結構》04-6 Complete Binary Search Tree

題目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a no

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

function namespace his () 技術 androi sed tel evel 題目信息 1064. Complete Binary Search Tree (30) 時間限制100 ms 內存限制65536 kB 代碼長度限制

【PAT】1064. Complete Binary Search Tree (30)【完全二叉搜尋

題目描述 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a

1064. Complete Binary Search Tree (30)

left keys nod positive oot key ear spec sort 1064. Complete Binary Search Tree (30) A Binary Search Tree (BST) is recursively defined as

建立完全搜尋二叉Complete Binary Search Tree )(c++)

Complete Binary Search Tree(c++) 因為題目要求,首先輸入二叉樹的結點個數,再輸入每個結點對應的值,建立二叉樹,既是二叉搜尋樹,又是完全二叉樹。 整體思路 根據輸入的結點數,建立完全二叉樹; 將節點數值放在陣列中,從小到大排序;

1064 Complete Binary Search Tree30 )(二叉查詢

  中序遍歷建樹 #include<bits/stdc++.h> using namespace std; const int N=1e3+10; int s[N]; int n; int tree[N]; int cnt; void inorder(int root)

1064 Complete Binary Search Tree30 )【二叉中序轉化成層序】

1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propert

1064 Complete Binary Search Tree30 )完全二叉搜尋

題目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contain

PAT 1064 Complete Binary Search Tree[二叉][難]

als tom 自然 resp scanf cbt oot 根據 attr 1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as

PAT 1064 Complete Binary Search Tree30

1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: T

1064 Complete Binary Search Tree30

一棵排序二叉樹的中序遍歷就是這一組數的遞增序列。這邊是完全二叉樹,假設從0開始,那麼節點i的左孩子的標號就是2i+1,右孩子的標號就是2(i+1)。先將這組數按照遞增來排序,然後用中序遍歷復原這棵完全排序二叉樹,最後直接輸出。 #include <bits/stdc++.h>

1064 Complete Binary Search Tree30 )PAT (Advanced Level) Practice

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 n

dfs_1064 Complete Binary Search Tree30

1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propert

Complete Binary Search Tree(完全二叉搜尋)用陣列表示和計算左子的規模

void solve(int ALeft,int ARight,int TRoot) {//初始呼叫為solve(0,N-1,0) n=ARignt-ALeft+1; if(n==0) ret

Complete Binary Search Tree

recursive .cn 慕課 end ati choose mat term next 本博客的代碼的思想和圖片參考:好大學慕課浙江大學陳越老師、何欽銘老師的《數據結構》 Complete Binary Search Tree 1 Question A Binary