国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Binary Search Tree analog

2019-11-09 21:07:15
字體:
供稿:網(wǎng)友

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 l To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node:l Visit the root. l Traverse the left subtree. l Traverse the right subtree. l To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node:l Traverse the left subtree. l Visit the root. l Traverse the right subtree. l To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node:l Traverse the left subtree. l Traverse the right subtree. l 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 figure:

InputThe 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].

OutputEach 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 Input153 6 9 5 1Sample Output3 1 6 5 91 3 5 6 91 5 9 6 3

根據(jù)題意建立二叉樹,編寫insert函數(shù)遞歸將數(shù)據(jù)插入二叉樹中,接下來基本的BST

#include<iostream>using namespace std;struct tree{	int v;	struct tree *left;	struct tree *right;}*root,*t;int pre,mid,lat,ro;void pre_order(tree *p){	if (p!=NULL)	{		if (pre==0)			cout<<p->v;		else cout<<" "<<p->v;		pre++;		pre_order(p->left);		pre_order(p->right);	}}void mid_order(tree *p){	if (p!=NULL)	{		mid_order(p->left);		if (mid==0)			cout<<p->v;		else cout<<" "<<p->v;		mid++;		mid_order(p->right);	}}void lat_order(tree *p){	if (p!=NULL)	{		lat_order(p->left);			lat_order(p->right);		if (lat==0)			cout<<p->v;		else cout<<" "<<p->v;		lat++;	}}void insert(int k,tree *p){	if (ro==0)	{		p->v=k;		p->left=NULL;		p->right=NULL;		ro++;	}	else if(k<p->v)	{		if (p->left==NULL)		{		    t=new tree;		    t->left=NULL;		    t->right=NULL;		    t->v=k;			p->left=t;		}		else insert(k,p->left);	}	else if(k>=p->v)	{		if (p->right==NULL)		{		    t=new tree;		    t->left=NULL;		    t->right=NULL;		    t->v=k;			p->right=t;		}		else insert(k,p->right);	}}void delet(tree *p){	int i;	if (p->left!=NULL) delet(p->left);	if (p->right!=NULL) delet(p->right);	delete p;}int main(){	int T,n,i,a;	cin>>T;	while (T--)	{		pre=0;		mid=0;		lat=0;		ro=0;		cin>>n;		root=new tree;		for (i=0;i<n;i++)		{			cin>>a;			insert(a,root);		}		pre_order(root);		cout<<endl;		mid_order(root);		cout<<endl;		lat_order(root);		cout<<endl;		delet(root);		cout<<endl;	}	return 0;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 什邡市| 南京市| 临武县| 太仓市| 塔城市| 大方县| 光泽县| 正安县| 延川县| 莫力| 武山县| 濮阳县| 开封市| 亳州市| 大余县| 石景山区| 策勒县| 老河口市| 邳州市| 项城市| 垣曲县| 郧西县| 拉孜县| 临桂县| 吐鲁番市| 青冈县| 夏邑县| 舞阳县| 治县。| 竹山县| 缙云县| 景德镇市| 大新县| 南川市| 建始县| 南安市| 铜川市| 阳原县| 宾阳县| 托克逊县| 乌审旗|