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

首頁 > 學院 > 開發設計 > 正文

Binary Search Tree analog

2019-11-09 20:32:47
字體:
來源:轉載
供稿:網友

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

根據題意建立二叉樹,編寫insert函數遞歸將數據插入二叉樹中,接下來基本的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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新营市| 天水市| 永康市| 新竹县| 抚顺县| 东辽县| 邢台县| 乌拉特后旗| 改则县| 五常市| 衡东县| 民乐县| 盐津县| 平昌县| 洛宁县| 芮城县| 时尚| 花莲县| 织金县| 土默特右旗| 鱼台县| 闽侯县| 乌鲁木齐县| 城步| 济南市| 盐津县| 勐海县| 中超| 遵化市| 吴忠市| 宜兰市| 博罗县| 新余市| 多伦县| 扎鲁特旗| 晴隆县| 阳原县| 赣州市| 原阳县| 玉溪市| 连平县|