Demo entry 6656391

1

   

Submitted by 1 on Oct 31, 2017 at 11:09
Language: C. Code size: 819 Bytes.

Tree Insert(ElementType X, Tree T, int count)					/*Buil a tree with numbers we provide.*/
{
	if (T == NULL)
	{
		T = (Tree)malloc(sizeof(struct TreeNode));
		T->Element = abs(X);
		T->Left = T->Right = NULL;
		T->Color = (X < 0);										/*1-red;0-black*/	
		if (T->Color == 0)
		{
			T->TotalBlack = count + 1;						/*If the color of a tree node is black,its TotalBlack  =  TotalBlack(parent) + 1.*/
		}
		else
		{
			T->TotalBlack = count;							/*If the color of a tree node is red,its TotalBlack  =  TotalBlack(parent).*/
		}
	}
	else if (abs(X) < T->Element)								/*Insert in the left subtree*/
	{
		T->Left = Insert(X, T->Left, T->TotalBlack);
	}
	else if (abs(X) > T->Element)								/*Insert in the right subtree*/
	{
		T->Right = Insert(X, T->Right, T->TotalBlack);
	}
	return T;

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).