This commit is contained in:
yzd 2023-09-29 04:22:31 -07:00
parent 3417137798
commit 7f54b96dc7
2 changed files with 25 additions and 13 deletions

View File

@ -31,13 +31,19 @@ static void inorder(RBTree tree)
if(tree != NULL)
{
inorder(tree->left_child);
printf("%d ", tree->key);
printf("key:%d, color: ", tree->key);
if(tree->is_red==RED){
printf("Red\n");
}else{
printf("Black\n");
}
inorder(tree->right_child);
}
}
void RBTreeTraversal(RBRoot *root)
{
printf("---------------------Inorder Traversal-----------------------------\n");
if (root)
inorder(root->node);
}

View File

@ -13,18 +13,28 @@ int main(void) {
char cmd;
RBRoot *root = create_rbtree();
// 构建红黑树
printf("== 原始数据: ");
for (i = 0; i < ARRAY_SIZE(arr); i++)
int length = sizeof(arr) / sizeof(arr[0]);
printf("------------------------------------------------------------");
printf("\nTest Red Black Tree \n");
printf("default_key array: ");
for(int i=0;i<length;i++){
printf("%d ", arr[i]);
}
printf("\n");
printf("%d elements",length);
printf("\n\n");
// 构建红黑树
for (i = 0; i < ARRAY_SIZE(arr); i++){
printf("insert key[%d]=%d",i,arr[i]);
printf("\n");
}
for (i = 0; i < ARRAY_SIZE(arr); i++) {
RBTreeInsert(root, arr[i]);
}
// 输出红黑树
printf("== 中序遍历: ");
RBTreeTraversal(root);
printf("\n");
@ -59,15 +69,11 @@ int main(void) {
break;
}
printf("\n\n是否显示当前的红黑树 (Y/N): ");
printf("\n\n是否显示当前的红黑树 (Y/N): \n");
// char inputChar;
// scanf(" %c", &inputChar);
// if (inputChar == 'Y' || inputChar == 'y') {
// printf("\n== 中序遍历: ");
// RBTreeTraversal(root);
// printf("\n");
// }
RBTreeTraversal(root);
}
destroy_rbtree(root);