TEST_F(AVLTest, Balanced) { auto avl = dyset.getObj(); // test AVL balanced avl->preorder([avl](node_pointer x){ EXPECT_TRUE(avl->isbalanced(x)); }); int gaps = this->N/5; for (int i=0; i<gaps; i++) { EXPECT_TRUE(avl->del(i)); } for (int i=0; i<gaps; i++) { EXPECT_TRUE(avl->del(2*gaps+i)); } for (int i=0; i<gaps; i++) { EXPECT_TRUE(avl->del(4*gaps+i)); } avl->preorder([avl](node_pointer x){ EXPECT_TRUE(avl->isbalanced(x)); }); // special case: delete 5 -> delete 6' // ____5____ // _3_ ___8___ // _2 4 6'_ _10_ // 1 7 9 11_ // 12 avl->clear(); int arr[12] = {5,3,8,2,4,6,10,1,7,9,11,12}; for (int i=0; i<12; i++) avl->insert(arr[i]); EXPECT_TRUE(avl->del(5)); avl->preorder([avl](node_pointer x){ EXPECT_TRUE(avl->isbalanced(x)); }); }
int isbalanced(link t) { if (t == NULL) return 1; int lc = depth(t->l); int rc = depth(t->r); int lb = isbalanced(t->l); if (lb == 0) printf("t->l->item = %c is not balanced\n", t->l->item); int rb = isbalanced(t->r); if (rb == 0) printf("t->r->item = %c is not balanced\n", t->r->item); if (lc - rc >= 2 || rc - lc >= 2) printf("t->item = %c is not balanced\n", t->item); if (lb == 0 || rb == 0) return 0; if (lc - rc >= 2 || rc - lc >= 2) return 0; return 1; }
int isbalanced(struct node *root){ int lheight; int rheight; if(root == NULL) return 1; lheight=height(root->left); rheight=height(root->right); if( abs(lheight-rheight) <=1 && isbalanced(root->left) && isbalanced(root->right)) return 1; /* If we reach here then tree is not balanced */ return 0; }//end of isbalanced
int main(void) { #if 0 char pre_seq[] = "4213657"; char in_seq[] = "1234567"; root = init(pre_seq, in_seq, 7); #endif #if 0 char pre_seq[] = "421356"; char in_seq[] = "123456"; root = init(pre_seq, in_seq, 6); #endif #if 0 link a, b, c; link d, e, f; // printf("demo binary tree!\n"); a = make_node('4'); b = make_node('2'); c = make_node('5'); d = make_node('1'); e = make_node('3'); f = make_node('6'); a->l = b; a->r = c; b->l = d; //b->r = e; //c->r = f; d->r = f; f->r = e; root = a; #endif extern yyin; yyin = stdin; while (1) { yylex(); continue; char ch; ch = getchar(); getchar(); if (ch == 'q') break; root = insert(root, ch); printf("\t\\tree "); traverse(root); printf("\n\n"); fflush(stdout); } while (1) { char ch = getchar(); getchar(); link p = search(root, ch); if (p != NULL) printf("%c is found\n", ch); else printf("%c is NOT found\n", ch); } printf("count = %d\n", count(root)); printf("depth = %d\n", depth(root)); printf("balance = %d\n", isbalanced(root)); if (mypow(2, depth(root)) == count(root) + 1) printf("it is a FULL Btree\n"); else printf("it is not a FULL Btree\n"); return 0; }
int main(void) { // char pre_seq[] = "421356"; // char in_seq[] = "123456"; // root = init(pre_seq, in_seq, 6); #if 0 link a, b, c, d, e, f; // printf("demo binary tree!\n"); a = make_node('4'); b = make_node('2'); c = make_node('5'); d = make_node('1'); e = make_node('3'); f = make_node('6'); a->l = b; // a->r = c; b->l = d; d->l = c; c->l = f; b->r = e; // c->r = f; root = a; #endif while (1) { char ch; ch = getchar(); getchar(); if (ch == 'q') break; root = insert(root, ch); printf("\\tree"); traverse(root); printf("\n"); } while (1) { char ch = getchar(); getchar(); link p = search(root, ch); if (p != NULL) printf("%c is found\n", ch); else printf("%c is NOT found\n", ch); } printf("count = %d\n", count(root)); printf("depth = %d\n", depth(root)); printf("balanced = %d\n", isbalanced(root)); #if 0 if (mypow(2, depth(root)) == count(root) + 1) printf("it is a FULL Btree\n"); else printf("it is not a FULL Btree\n"); #endif return 0; }