Skip to content

Commit

Permalink
So sánh cây nhị phân
Browse files Browse the repository at this point in the history
  • Loading branch information
bangoc committed Mar 22, 2024
1 parent baf9101 commit 17f7b9a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
43 changes: 12 additions & 31 deletions training/bst-int.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,22 @@ struct bst_node *bst_search(struct bst_tree *t, int key) {
return NULL;
}

int bst_equal_lnr(struct bst_node *n1, struct bst_node *n2) {
if (n1 != NULL && n2 == NULL || n1 == NULL && n2 != NULL) {
return 0;
}
if (n1 == NULL && n2 == NULL) {
return 1;
}
return bst_equal_lnr(n1->left, n2->left) && n1->key == n2->key &&
bst_equal_lnr(n1->right, n2->right);
}

int bst_equal(struct bst_tree *t1, struct bst_tree *t2) {
if (t1->size != t2->size) {
return 0;
}
struct bst_node *n1 = bst_first_lnr(t1), *n2 = bst_first_lnr(t2);
while (n1 && n2) {
if (n1->key != n2->key) {
return 0;
}
n1 = bst_next_lnr(n1);
n2 = bst_next_lnr(n2);
}
return 1;
return bst_equal_lnr(t1->root, t2->root);
}

void bst_change(struct bst_node *old_node, struct bst_node *new_node,
Expand Down Expand Up @@ -209,25 +212,3 @@ void bst_free(struct bst_tree *t) {
}
free(t);
}

struct bst_node *bst_left_most(struct bst_node *n) {
while (n->left) {
n = n->left;
}
return n;
}

struct bst_node *bst_first_lnr(struct bst_tree *t) {
return bst_left_most(t->root);
}
struct bst_node *bst_next_lnr(struct bst_node *n) {
if (n->right) {
return bst_left_most(n->right);
}
struct bst_node *top = n->top;
while (top != NULL && n == top->right) {
n = top;
top = n->top;
}
return top;
}
2 changes: 0 additions & 2 deletions training/bst-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ struct bst_node *bst_node(int key);
struct bst_tree *bst_tree();
struct bst_node *bst_put(struct bst_tree *t, int key);
struct bst_node *bst_search(struct bst_tree *t, int key);
struct bst_node *bst_first_lnr(struct bst_tree *t);
struct bst_node *bst_next_lnr(struct bst_node *n);
int bst_equal(struct bst_tree *t1, struct bst_tree *t2);
int bst_remove(struct bst_tree *t, int key);
void bst_pprint(struct bst_tree *t);
Expand Down

0 comments on commit 17f7b9a

Please sign in to comment.