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

首頁 > 學院 > 開發(fā)設計 > 正文

Binary Search Tree analog

2019-11-10 16:56:13
字體:
來源:轉載
供稿:網(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ā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 罗山县| 瓦房店市| 浦北县| 遵义市| 梧州市| 株洲县| 厦门市| 定南县| 四川省| 临清市| 芜湖市| 阳谷县| 离岛区| 包头市| 灵宝市| 漳州市| 平昌县| 平利县| 顺平县| 景洪市| 松滋市| 衢州市| 江西省| 遵化市| 陆河县| 龙川县| 安达市| 新宾| 汉寿县| 沙湾县| 香港 | 高州市| 青铜峡市| 葫芦岛市| 宁城县| 星子县| 岢岚县| 兴化市| 拉萨市| 乌鲁木齐市| 沙洋县|