bool balanced(TreeNode *root, int &h) { if (root == NULL) { h = 0; return true; } int lh, rh; bool lcheck = balanced(root->left, lh); bool rcheck = balanced(root->right, rh); h = max(lh, rh) + 1; return abs(lh - rh) <= 1 && lcheck && rcheck; }
static int balanced (TreeNode *N) { int hleft, hright; if(N==NULL) return -1; hleft=balanced(N->left); if(hleft==-2) return -2; hright=balanced(N->right); if(hright==-2) return -2; if(hleft-hright<-1 || hleft-hright>1) return -2; if(hleft<hright) return 1+hright; else return 1+hleft; }
/** * Private method rebalance * Update heights and rebalances the tree * in order to mantain AVL property * @param n the node being rebalanced */ void AVLTree::rebalance(node *n) { if (!n) { return; } node *x = n; node *y = NULL; while (x->parent != NULL) { y = x->parent; y->height = max(height(y->leftChild), height(y->rightChild)) + 1; x = y; if (!balanced(x)) { if (height(x->rightChild) > height(x->leftChild)) { y = x->rightChild; if (height(y->rightChild) >= height(y->leftChild)) { rotateLeft(x); } else { rotateRight(y); rotateLeft(x); } } else { y = x->leftChild; if (height(y->leftChild) >= height(y->rightChild)) { rotateRight(x); } else { rotateLeft(y); rotateRight(x); } } } } }
bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function int depth = 0; return balanced(root,depth); }
Node*insert(Node*node,int data) { if(node==NULL) return (createNode(data)); if(data< node->data) node->left=insert(node->left,data); else node->right=insert(node->right,data); node->height=maxim(height(node->left),height(node->right))+1; int balan=balanced(node); if (balan>1 && data<node->left->data) return rotateRight(node); if (balan<-1 && data>node->right->data) return rotateLeft(node); if (balan>1 && data>node->left->data) { node->left=rotateLeft(node->left); return(rotateRight(node)); } if (balan<-1 && data<node->right->data) { node->right=rotateRight(node->right); return(rotateLeft(node)); } return node; }
int main() { srand(time(NULL)); // seed rng for (int i = 0; i < 9; ++i) { std::string s(generate(i)); std::cout << (balanced(s) ? " ok: " : "bad: ") << s << "\n"; } }
int main( void ) { tree *tp = read_tree(); if ((balanced(tp)) == 1) printf("BALANCED\n"); else printf("UNSTABLE\n"); return 0; }
void status() { switch (classes) { case BALANCED: balanced(); break; case WARRIOR: warrior(); break; case ARCHER: archer(); break; } }
bool balanced(TreeNode *root,int &depth){ if(!root){ depth = 0; return true; } if(!root->left && !root->right){ depth = 1; return true; } int d1,d2; if(balanced(root->left,d1) && balanced(root->right,d2)) { if(abs(d1-d2) <= 1){ depth = 1+max(d1,d2); return true; } return false; } return false; }
void insert(node *&o,const T &data){ if(!o->size){ o=new node(data); o->ch[0]=o->ch[1]=nil; }else{ o->size++; bool d=o->data<data; insert(o->ch[d],data); balanced(o,d); } }
int main() { int cases; scanf("%d\n", &cases); while(cases--) { gets(str); balanced()? puts("Yes") : puts("No"); } return 0; }
bool erase(node *&o,const T &data){ if(!o->size)return 0; if(o->data==data){ if(!o->ch[0]->size||!o->ch[1]->size){ node *t=o; o=o->ch[0]->size?o->ch[0]:o->ch[1]; delete t; }else{ o->size--; node *tmd=o->ch[1]; while(tmd->ch[0]->size)tmd=tmd->ch[0]; o->data=tmd->data; erase(o->ch[1],tmd->data); balanced(o,0); }return 1; } bool d=o->data<data; if(erase(o->ch[d],data)){ o->size--,balanced(o,!d); return 1; }else return 0; }
bool isBalanced(TreeNode *root) { int h; return balanced(root, h); }
/* Collect and execute code from stdin. The first byte of flags are passed to Jsi_ValueGetDString(). */ int Jsi_Interactive(Jsi_Interp* interp, int flags) { int rc = 0, done = 0, len, quote = (flags & 0xff), istty = 1; char *prompt = "# ", *buf; Jsi_DString dStr; Jsi_DSInit(&dStr); #ifndef __WIN32 istty = isatty(fileno(stdin)); #else istty = _isatty(_fileno(stdin)); #endif #ifdef HAVE_READLINE Jsi_DString dHist = {}; char *hist = NULL; if(interp->noreadline == 0 && !interp->parent) { hist = Jsi_NormalPath(interp, "~/.jsish_history", &dHist); if (hist) read_history(hist); } #endif interp->level++; while (done==0 && interp->exited==0) { buf = get_inputline(istty, prompt); if (buf) { Jsi_DSAppend(&dStr, buf, NULL); free(buf); } else { done = 1; } len = Jsi_DSLength(&dStr); if (done && len == 0) break; buf = Jsi_DSValue(&dStr); if (done == 0 && (!balanced(buf))) { prompt = "> "; if (len<5) break; continue; } prompt = "# "; while ((len = Jsi_Strlen(buf))>0 && (isspace(buf[len-1]))) buf[len-1] = 0; if (buf[0] == 0) continue; /* Convenience: add semicolon to "var" statements (required by parser). */ if (strncmp(buf,"var ", 4) == 0 && strchr(buf, '\n')==NULL && strchr(buf, ';')==NULL) strcat(buf, ";"); rc = Jsi_EvalString(interp, buf, JSI_EVAL_RETURN); if (interp->exited) break; if (rc == 0) { if (interp->ret.vt != JSI_VT_UNDEF || interp->noUndef==0) { Jsi_DString eStr = {}; fputs(Jsi_ValueGetDString(interp,&interp->ret, &eStr, quote), stdout); Jsi_DSFree(&eStr); fputs("\n", stdout); } } else if (!interp->exited) { fputs("ERROR\n", stderr); } Jsi_DSSetLength(&dStr, 0); len = 0; } interp->level--; #ifdef HAVE_READLINE if (hist) { stifle_history(100); write_history(hist); } Jsi_DSFree(&dHist); #endif Jsi_DSFree(&dStr); if (interp->exited && interp->level <= 0) { rc = interp->exitCode; Jsi_InterpDelete(interp); } return rc; }
int Balanced (Tree *T) { if(balanced(T->root)==-2) return 0; return 1; }