數據結構實驗報告-zstu計科全英一Experiment 2:判斷紅黑樹

題目

(1) Task description:
There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:
(1) Every node is either red or black.
(2) The root is black.
(3) Every leaf (NULL) is black.
(4) If a node is red, then both its children are black.
(5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
For each given binary search tree, you are supposed to tell if it is a legal red-black tree.

(2) Methods:

  1. Using insert function to build the red-black tree, judging the absolute value of the number and the element of the node.
  2. The rule 1 and 3 are must true, so we just need to judge the rule 2,4,5.
  3. Rule 2 is easy.
  4. Rule 4, I use a recursive function to judge. When the node is null, the function will end. If a red node has a red children node, the function will return false.
  5. Rule 5, I first calculate the number of black nodes in the leftmost subtree path, than use the recursive function to calculate the number of black nodes of every paths of the tree, if not equal, return false.

代碼實現

# include<stdio.h>

typedef struct TreeNode* Tree;

//值的大小來表示紅黑樹,負數爲紅,正數爲黑

struct TreeNode {
	int Element;
	Tree  Left;
	Tree  Right;
};
int baseBlack = 0; //定義最左邊子樹路徑的黑色節點數


//通過插入函數,把前序轉化爲二叉樹
void insert(Tree T,int number)
{
	if (abs(number) <= abs(T->Element))
	{
		if (T->Left == NULL)
		{
			T->Left = malloc(sizeof(struct TreeNode));
			T->Left->Element = number;
			T->Left->Left = NULL;
			T->Left->Right = NULL;
			return;
		}
		else
			insert(T->Left, number);
	}
	else
	{
		if (T->Right == NULL)
		{
			T->Right = malloc(sizeof(struct TreeNode));
			T->Right->Element = number;
			T->Right->Left = NULL;
			T->Right->Right = NULL;
			return;
		}
		else
			insert(T->Right, number);
	}
}
//第一條和第三條一定是滿足的  需要判斷的是規則2,4,5

// judge (2) The root is black.
// 判斷第二條規則 根是否爲黑色
int judgeRull2(Tree rbtree)
{
	if (rbtree->Element < 0) return 0;
}


// judge (4) If a node is red, then both its children are black.
// 通過遞歸,判斷第四個規則是否成立
int judgeRull4(Tree rbtree)
{
	if (rbtree == NULL) return 1;
	if (rbtree->Element < 0)
	{
		if (!rbtree->Left) return 1; //如果爲空 返回1
		if (rbtree->Left->Element < 0) //非空 看看值
		{
			return 0;
		}
		if (!rbtree->Right) return 1;
		if (rbtree->Right->Element < 0)
		{
			return 0;
		}
	}
	return judgeRull4(rbtree->Left) && judgeRull4(rbtree->Right);
}


//獲得最左邊路徑子樹的黑色節點個數,作爲比較的基準,後面的所有路徑的黑色節點個數應該等於這個
void blackOfLeft(Tree rbtree)
{
	while (rbtree != NULL)
	{
		if (rbtree->Element > 0)
			baseBlack++;
		rbtree = rbtree->Left;
	}
}



//judge (5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
// 通過遞歸比較每個路徑的黑色節點個數是否等於最左邊的路徑的黑色節點個數
int judgeRull5(Tree rbtree,int count)
{
	if (rbtree == NULL) return 1;
	if (rbtree->Element > 0) count++;
	if (rbtree->Right == NULL && rbtree->Left == NULL)
	{
		if (count != baseBlack) return 0;
	}
	return judgeRull5(rbtree->Left, count) && judgeRull5(rbtree->Right, count);
}


int main()
{
	Tree rbtree = malloc(sizeof(struct TreeNode));
	


	int N;
	printf("Please input the number of nodes first,and then input the preorder traversal sequence of the tree\n");
	scanf("%d", &N);
	int tempElement;

	//先輸入根節點,並初始化,再順序輸入
	scanf("%d", &tempElement);
	rbtree->Element = tempElement;
	rbtree->Left = NULL;
	rbtree->Right = NULL;


	
	for (int i = 1; i < N; i++)
	{
		scanf("%d", &tempElement);
		insert(rbtree, tempElement);
	}

	blackOfLeft(rbtree);
	if (judgeRull2 && judgeRull4 && judgeRull5) printf("Yes");
	else printf("No");

	return 0;
}

實現

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章