bool Solution::is_bst(TreeNode* node, long int& prev) { if(node == NULL) return true; bool ret = false; if(node->left != NULL) { ret = is_bst(node->left, prev); if(ret == false) return ret; } long int cur_val = node->val; if(cur_val <= prev) { return false; } else { prev = node->val; } if(node->right != NULL) { ret = is_bst(node->right, prev); if(ret == false) return ret; } return true; }
int is_bst(bst *b) { if(!b || (!b->left && !b->right) ) return 1; int min = min_value(b->left); int max = max_depth(b->right); if( !( b->val >= min && b->val < max)) return 0; return is_bst(b->left) && is_bst(b->right); }
void bst_insert(bst B, elem x) { REQUIRES(is_bst(B)); REQUIRES(x != NULL); B->root = tree_insert(B->root, x, B); ENSURES(is_bst(B)); return; }
bool is_bst(const Node *const pt_to_node) { if (!pt_to_node) return true; if (pt_to_node->pt_to_left_node && compare_values(pt_to_node->pt_to_left_node->value, pt_to_node->value) >= 0) return false; if (pt_to_node->pt_to_right_node && compare_values(pt_to_node->value, pt_to_node->pt_to_right_node->value) >= 0) return false; return is_bst(pt_to_node->pt_to_left_node) && is_bst(pt_to_node->pt_to_right_node); }
bool is_bst(NodeBase* node) { if (is_nil(node)) { return true; } auto m = node; m = max_node(m, max_element(node->left)); m = min_node(m, min_element(node->right)); return m == node && is_bst(node->left) && is_bst(node_>right); }
int main(void) { struct node *bst = create_good_bst(); printf("==1st==\n"); traverse_bst(bst); printf("%s\n", (is_bst(bst) ? "good" : "bad") ); bst = create_bad_bst(); printf("==2nd==\n"); traverse_bst(bst); printf("%s\n", (is_bst(bst) ? "good" : "bad") ); return 0; }
elem bst_lookup(bst B, elem x) { REQUIRES(is_bst(B) && x != NULL); elem r = tree_lookup(B->root, x, B); ENSURES(r == NULL || B->elem_compare(r, x) == 0); return r; }
Node<T>* min_node(Node<T>* node) { assert(is_bst(node)); if(is_nil(node)) { return node; } while(is_real(node->left)) { node = node->left; } return node; }
void _tree () { bnode* root = NULL; root = insert(root, 10); insert(root, 20); insert(root, 30); insert(root, 11); insert(root, 13); insert(root, 15); insert(root, 40); insert(root, 90); inorder(root); printf ("\n deleting 90\n"); root = deletenode(root, 90); inorder(root); printf ("\n find 30 is %d\n", find(root, 300)->data); printf ("this is %s a bst\n",is_bst(root)?"":"not"); printf ("sum is %d\n",sum(root)); }
int main(int argc, char** argv) { random_bst(); struct node* bst_root = generate_bst(20); printf("Pre-order\n"); pre_order_bst(bst_root); printf("\n"); printf("level-order\n"); level_order_bst(bst_root); printf("\n"); printf("Is a bst? %d\n", is_bst(bst_root)); in_order_bst(bst_root); printf("\n"); struct dll list; bst2dll(bst_root, &list); print_dll(list); destruct_dll(list); return 0; }
void adjust_daylight_saving() { /*No need to adjust for leap years, because it will never be BST in Feb.*/ int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /*Add an hour to get BST, skip to the next day if it is close to midnight*/ if(is_bst()){ if(utc_hour == 23) { hour = 0; if(utc_day == days_in_month[month-1]) { day = 1; month = utc_month + 1; } else { day = utc_day + 1; month = utc_month; } } else { hour = utc_hour + 1; day = utc_day; month = utc_month; } } else { hour = utc_hour; day = utc_day; month = utc_month; } }
bool isValidBST(TreeNode* root) { long int buf = LLONG_MIN; bool ret = is_bst(root,buf); }