Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

纪念下第一个2叉排序

Posted by sinper at 2007-12-12 11:02:30 on Problem 2418
#include <stdio.h>
#include <string.h>
#include <malloc.h>

typedef struct _node
{
	char str[31];
	int count;
	struct _node *left;
	struct _node *right;
}node, *node_ptr;

double k      = 0.0;
node_ptr root = NULL;

void InsertTree(node_ptr z)
{
	node_ptr y = NULL;
	node_ptr x = root;
	int n;

	while (x != NULL)
	{
		y = x;
		
		n = strcmp(z->str, x->str);
		if (n == 0)
		{
			break;
		}
		else
		if (n < 0)
		{
			x = x->left;
		}
		else
		{
			x = x->right;
		}
	}

	if (y == NULL)
	{
		root = z;
	}
	else
	{
		if (x != NULL && strcmp(x->str, y->str) == 0)
		{
			x->count++;
			free(z);
			z = NULL;
		}
		else
		if (strcmp(z->str, y->str) < 0)
		{
			y->left = z;
		}
		else
		{
			y->right = z;
		}
	}
}

void PrintTree(node_ptr root)
{
	if (root != NULL)
	{
		PrintTree(root->left);
		printf("%s %.4f\n", root->str, root->count*100/k);
		PrintTree(root->right);
	}
}

int main()
{
	char str[31];

	while (gets(str) != NULL)
	{
		k++;
		node_ptr z = (node_ptr)malloc(sizeof(node));
		z->count = 1;
		strcpy(z->str, str);
		z->right = NULL;
		z->left  = NULL;

		InsertTree(z);
	}

	PrintTree(root);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator