modify3
This commit is contained in:
parent
3417137798
commit
7f54b96dc7
|
@ -31,13 +31,19 @@ static void inorder(RBTree tree)
|
||||||
if(tree != NULL)
|
if(tree != NULL)
|
||||||
{
|
{
|
||||||
inorder(tree->left_child);
|
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);
|
inorder(tree->right_child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RBTreeTraversal(RBRoot *root)
|
void RBTreeTraversal(RBRoot *root)
|
||||||
{
|
{
|
||||||
|
printf("---------------------Inorder Traversal-----------------------------\n");
|
||||||
if (root)
|
if (root)
|
||||||
inorder(root->node);
|
inorder(root->node);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,18 +13,28 @@ int main(void) {
|
||||||
char cmd;
|
char cmd;
|
||||||
RBRoot *root = create_rbtree();
|
RBRoot *root = create_rbtree();
|
||||||
|
|
||||||
// 构建红黑树
|
int length = sizeof(arr) / sizeof(arr[0]);
|
||||||
printf("== 原始数据: ");
|
printf("------------------------------------------------------------");
|
||||||
for (i = 0; i < ARRAY_SIZE(arr); i++)
|
printf("\nTest Red Black Tree \n");
|
||||||
|
printf("default_key array: ");
|
||||||
|
for(int i=0;i<length;i++){
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
|
}
|
||||||
printf("\n");
|
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++) {
|
for (i = 0; i < ARRAY_SIZE(arr); i++) {
|
||||||
RBTreeInsert(root, arr[i]);
|
RBTreeInsert(root, arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 输出红黑树
|
// 输出红黑树
|
||||||
printf("== 中序遍历: ");
|
|
||||||
RBTreeTraversal(root);
|
RBTreeTraversal(root);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
@ -59,15 +69,11 @@ int main(void) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n\n是否显示当前的红黑树 (Y/N): ");
|
printf("\n\n是否显示当前的红黑树 (Y/N): \n");
|
||||||
|
|
||||||
// char inputChar;
|
|
||||||
// scanf(" %c", &inputChar);
|
RBTreeTraversal(root);
|
||||||
// if (inputChar == 'Y' || inputChar == 'y') {
|
|
||||||
// printf("\n== 中序遍历: ");
|
|
||||||
// RBTreeTraversal(root);
|
|
||||||
// printf("\n");
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy_rbtree(root);
|
destroy_rbtree(root);
|
||||||
|
|
Loading…
Reference in New Issue