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

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

Binary Search Tree analog

2019-11-10 18:50:45
字體:
供稿:網(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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乡城县| 张北县| 汾西县| 嵊泗县| 陵川县| 铜鼓县| 长顺县| 邹平县| 南开区| 靖西县| 泰顺县| 巫山县| 陕西省| 大关县| 长汀县| 襄城县| 博白县| 凌海市| 南阳市| 沾益县| 旌德县| 泸水县| 淄博市| 礼泉县| 察隅县| 峨边| 湘潭县| 武鸣县| 白沙| 梅河口市| 调兵山市| 三门县| 谢通门县| 淮阳县| 安仁县| 南靖县| 乌鲁木齐县| 南开区| 平谷区| 郎溪县| 巨野县|