POJ 2418 Hardwood species__ two fork search tree

Source: Internet
Author: User
Tags strcmp

The main effect of the topic:

Broad-leaved Forest is a kind of plant community, among which the trees have the common characteristics of a large leaf face, more knot of nuts, winter will enter the dormant state. The temperature and climate of the United States have created nearly hundreds of broad-leaved species, such as oak, maple, cherry trees, and so on, which make up almost 40% of all American trees. Coniferous trees, on the other hand, are known as "cone-shaped trees" because their leaves are conical needles, and are found in the United States, such as cedar, fir, hemlock, sequoia, etc. Generally conifers are used as ornamental wood at home.

The current natural resources sector uses satellite imaging technology to compile a list of images each day, recording each tree within a certain range, and you need to programmatically calculate the "demographic structure" of the tree species recorded each day.

There are a number of examples (no upper limit for the survey), in each case, a list of trees (taken by satellite, given by tree names), one row for each tree, no more than 30 characters in the tree, which repeats itself, no more than 10,000 trees, and no more than 1,000,000 trees, For each sample, the tree name of each tree and the percentage of the total (truncated to 4 decimal digits) are given in ascending dictionary, each of which is one row.

Topic link

Bst:

Comment Code:

* * Problem id:poj 2418 Hardwood species * author:lirx.t.una * Run time:1110 MS * Run memory:312 KB/#include <string.h> #include <stdl		ib.h> #include <stdio.h>//maximum length of species ' name,//The maximum length of the species name//The last one left to the null character//30 + 1 #define Maxnamelen

struct Node;
typedef struct NODE * PTNODE;

typedef struct NODE * tree;
	
	struct Node {//Two fork Search tree node char Name[maxnamelen];
	Tree LfT;
	
	Tree Rht;

int Cnt;//count, recording how many times the species has appeared}; The total number of int n;//trees void Insert (tree &tree, char *name) {//Insert a node//to the BST is a reference to the node pointer so that the upper recursive function can be passed in//father_node->
		
		Lft/rht to modify the if (!tree) {//If the number is empty on behalf of has been recursive to the bottom (leaf node below the layer)//At this point can be inserted into the node tree = (ptnode) malloc (sizeof (Node));
		
		strcpy (tree->name, name);
		Tree->lft = NULL;
		Tree->rht = NULL;
		
		tree->cnt = 1;
	return;
	
	int CMP;
	
	CMP = strcmp (tree->name, name); if (!cmp) {//If it happens to be the object represented by the current node.Species//The number of the species is added to 1 tree->cnt++;
	return;
	{if (CMP < 0)//If the alphabetical order is less than the species represented by the current node//INSERT into its left subtree insert (TREE-&GT;RHT, name);
else//Otherwise insert its right subtree inserts (TREE-&GT;LFT, name);
		} void Travel {//in-order traversal if (tree) {//First left-right travel (TREE-&GT;LFT);
		printf ("%s%.4f\n", Tree->name, (double) tree->cnt/(Double) n * 100.0);
	Travel (TREE-&GT;RHT);
	int main () {char name[maxnamelen];
	
	Tree tree;
	tree = NULL;
	n = 0;
		while (gets (name)) {Insert (tree, name);
	n++;
	
	} travel (tree);
return 0; }

No comment code:

#include <string.h> #include <stdlib.h> #include <stdio.h> #define Maxnamelen struct Node;
typedef struct NODE * PTNODE;

typedef struct NODE * tree;
	
	struct Node {char name[maxnamelen];
	Tree LfT;
	
	Tree Rht;
int cnt;

};

int n;
		
		void Insert (tree &tree, char *name) {if (!tree) {tree = (ptnode) malloc (sizeof (Node));
		
		strcpy (tree->name, name);
		Tree->lft = NULL;
		Tree->rht = NULL;
		
		tree->cnt = 1;
	return;
	
	int CMP;
	
	CMP = strcmp (tree->name, name);
		if (!cmp) {tree->cnt++;
	return;
	if (CMP < 0) Insert (tree->rht, name);
else Insert (tree->lft, name);
		} void Travel {if (tree) {travel (TREE-&GT;LFT);
		printf ("%s%.4f\n", Tree->name, (double) tree->cnt/(Double) n * 100.0);
	Travel (TREE-&GT;RHT);
	int main () {char name[maxnamelen];
	
	Tree tree;
	tree = NULL;
	n = 0;
while (gets (name)) {Insert (tree, name);		n++;
	
	} travel (tree);
return 0; }

Avl:

Comment Code:

* * Problem id:poj 2418 Hardwood species * author:lirx.t.una * Run time:1329 MS * Run memory:512 KB */#pragma GCC optimize ("O2") #includ

E <string.h> #include <stdlib.h> #include <stdio.h> #define Maxnamelen to struct Node;
typedef struct NODE * PTNODE;

typedef struct NODE * tree;

	struct Node {char name[maxnamelen];	
	Tree LfT;

	Tree Rht;
int h;//Each AVL tree node also needs to maintain///The height of the number represented by the node//side to the rear left and right subtree height adjustment//and rotation to bring the basis int cnt;

};

int n;

	int height (tree tree) {//Empty node height is-1,//single node height is 0 if (!tree) return-1;
Return tree->h; } int Maxhrl (tree tree) {//maximum The height in the left trees and right trees,//compute the high level int HL of both the right subtree, hr;//height
	EE and right tree hl = Height (TREE-&GT;LFT);

	hr = Height (TREE-&GT;RHT); return hl > hr?
HL:HR;

	Tree Rotl (K2) {//rotation on left, rotate tree K1; K1 = K2-&GT;lft;
	K2->lft = k1->rht;

	K1->rht = K2;
	After rotation, due to K2 's left subtree changed//and K2 became the K1 subtree//So need to update K2 height//Then update k1 height k2->h = maxhrl (K2) + 1;

	K1->h = Maxhrl (k1) + 1;
return K1;

	Tree ROTR (tree K2) {//rotation, K1;
	K1 = k2->rht;
	K2->rht = k1->lft;

	K1->lft = K2;
	K2->h = Maxhrl (K2) + 1;

	K1->h = Maxhrl (k1) + 1;
return K1;

	"Tree ROTLR" {//rotation first in Left,then on right,//double rotate, turn left on T's right-hand subtree, then turn right at t T->rht = Rotl (T->rht);
return ROTR (t);

	"Tree Rotrl" {//rotation, then in left t->lft = ROTR (T-&GT;LFT);
return Rotl (t);

		Tree inserts (tree, char *name) {//Insert node if (!tree) {ptnode node to AVL;
		
		node = (ptnode) malloc (sizeof (struct node));
		
		strcpy (node->name, name);
		Node->lft = NULL;
		
		Node->rht = NULL;
		
		Node->h = 0;//single node tree height is 0 node->cnt = 1;
	return node;
	
	int CMP;
	
	CMP = strcmp (name, tree->name); if (!cmp) {tree->cnt++;
	return to tree;
		if (CMP < 0) {//Insert species alphabetical order less than the current node is inserted into China tree->lft = insert (tree->lft, name); Since the insertion of Zuozi is AVL balanced, there is no guarantee that the tree is also balanced (since this is recursive insertion) if (2 = height (tree->lft)-height (Tree->rht))//If unbalanced if (St RCMP (name, Tree->lft->name) < 0)//Insert the species letter is less than the left subtree,//This means that the tree left-leaning return Rotl (
			tree);
	else//otherwise is greater than the right subtree//can not appear equal to, because equals means that there is no insertion point, because the original is AVL balanced//Since there is no insertion point is now balanced, so it is not possible to appear unbalanced return Rotrl (trees);
		else {///insert to right subtree is the same Tree->rht = insert (Tree->rht, name); if (2 = height (tree->rht)-height (tree->lft)) if (strcmp (name, tree->rht->name) > 0) ret
			Urn ROTR (tree);
	else return ROTLR (tree);  ///Due to rotation the rotation function has updated the height of the tree//But if there is no imbalance after inserting the left and right subtree, that is to say, there is no rotation function passed//So the tree height has not been updated//Therefore the height of the trees needs to be updated tree->h =
	
	Maxhrl (tree) + 1;
return to tree;
		} void Travel {if (tree) {Travel (TREE-&GT;LFT); printf ("%s%.4f\ n ", Tree->name, (double) tree->cnt/(Double) n * 100.0);
	Travel (TREE-&GT;RHT);
	int main () {char name[maxnamelen];
	
	Tree tree;
	tree = NULL;
	n = 0;
		while (gets (name)) {tree = Insert (tree, name);
	n++;
	
	} Travel (tree);
return 0; }

No comment code:

#pragma GCC optimize ("O2") #include <string.h> #include <stdlib.h> #include <stdio.h> #define Maxnamel

EN to struct Node;
typedef struct NODE * PTNODE;

typedef struct NODE * tree;

	struct Node {char name[maxnamelen];	
	Tree LfT;

	Tree Rht;
	int h;
int cnt;

};

int n;

	int Height (tree tree) {if (!tree) return-1;
Return tree->h;

	int Maxhrl (tree tree) {int HL, hr;
	HL = Height (TREE-&GT;LFT);

	hr = Height (TREE-&GT;RHT); return hl > hr?
HL:HR;

	Tree Rotl (K2) {tree K1;
	K1 = k2->lft;
	K2->lft = k1->rht;

	K1->rht = K2;
	K2->h = Maxhrl (K2) + 1;

	K1->h = Maxhrl (k1) + 1;
return K1;

	Tree ROTR (K2) {tree K1;
	K1 = k2->rht;
	K2->rht = k1->lft;

	K1->lft = K2;
	K2->h = Maxhrl (K2) + 1;

	K1->h = Maxhrl (k1) + 1;
return K1;

	Tree ROTLR (Tree t) {T->rht = Rotl (T->rht);
return ROTR (t);
	
	Tree Rotrl (Tree t) {t->lft = ROTR (T-&GT;LFT);
return Rotl (t);

}Tree inserts (tree, char *name) {if (!tree) {ptnode node;
		
		node = (ptnode) malloc (sizeof (struct node));
		
		strcpy (node->name, name);
		Node->lft = NULL;
		
		Node->rht = NULL;
		node->h = 0;
		
		node->cnt = 1;
	return node;
	
	int CMP;
	
	CMP = strcmp (name, tree->name);
		if (!cmp) {tree->cnt++;
	return to tree;
		} if (CMP < 0) {tree->lft = Insert (tree->lft, name); if (2 = height (tree->lft)-height (Tree->rht)) if (strcmp (name, Tree->lft->name) < 0) ret
			Urn Rotl (tree);
	else return Rotrl (tree);
		else {Tree->rht = Insert (Tree->rht, name); if (2 = height (tree->rht)-height (tree->lft)) if (strcmp (name, tree->rht->name) > 0) ret
			Urn ROTR (tree);
	else return ROTLR (tree);
	
	} tree->h = Maxhrl (tree) + 1;
return to tree;
		} void Travel {if (tree) {Travel (TREE-&GT;LFT); printf ("%S%.4f\n ", Tree->name, (double) tree->cnt/(Double) n * 100.0);
	Travel (TREE-&GT;RHT);
	int main () {char name[maxnamelen];
	
	Tree tree;
	tree = NULL;
	n = 0;
		while (gets (name)) {tree = Insert (tree, name);
	n++;
	
	} Travel (tree);
return 0; }

Word Explanation:

Hardwood:n, broadleaf, hardwood

Hardwoods:n, Broad-leaved Forest

Species:n, species, type

Botanical:n, botany.

Broad:adj, Broad.

Nut:n, nuts.

Dormant:adj, dormant

Generally:adv, usually, general

Climate:n, climate

Biological:adj, Biology.

Characteristic:n, characteristic

Oak:n, Oak.

Maple:n, Maple

Cherry:n, Cherry Tree

Softwood:n, coniferous trees, cork

Softwoods:n, coniferous Forest

Conifer:n, coniferous trees

Cone:n, Cone

Bearing:n, bearing

Cone bearing:n, tapered bearing

Needle:n, Needles

Widely available: widely visible and widely accessible

CEDAR:N, Cedar

Fir:n, Fir

Hemlock:n, Hemlock.

Pine:n, Pine.

Redwood:n, Sequoia

Spruce:n, Spruce

Cypress:n, Cypress

Primarily:adv, predominantly, first

Lumber:n, Wood

Structural:adj, Architectural, structural

Decorative:adj, decoration, decorative

Application:n, use, application, application

Satellite:n, Sputnik.

Imaging:n, imaging

Satellite imaging technology:n, satellite imaging technology

Resource:n, resources (refers to natural resources), can also refer to human resources

Inventory:n, detailed catalogue, inventory list

COMPILE:VT, compiling, compiling

Fraction:n, partial, small part

Alphabetical:adj, in alphabetical order.

Percentage:n, percentage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.