/* Balanced BST from sorted array [1,2,3,4,5,6,7]: 4 / \ 2 6 / \ / \ 1 3 5 7 Inorder traversal prints nodes in ascending order. */ void inorderTraversal(struct Node* root) { if (root == NULL) return; inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); } // Output of inorderTraversal(root) for the above tree?
Inorder traversal of a BST always prints elements in sorted order.
The inorder traversal visits left subtree, root, then right subtree. For a BST, this prints nodes in ascending order.
Height of balanced BST is about log2(n+1) - 1.
A balanced BST built from a sorted array has height approximately log base 2 of (n+1) minus 1. For 15 elements, height = 4 (levels 0 to 4), so height is 4. Height is counted as edges, so height = levels - 1 = 4.
Check base case and recursive calls carefully.
The code correctly handles base case (start > end) and recursively builds left and right subtrees. It allocates memory properly and returns root. No errors expected.
/*
Balanced BST structure:
40
/ \
20 60
/ \ / \
10 30 50 70
Preorder traversal visits root, left, right.
*/Preorder traversal visits root first, then left subtree, then right subtree.
Starting at root 40, preorder visits 40, then left subtree (20, 10, 30), then right subtree (60, 50, 70).
Minimum nodes in height h balanced BST follow recurrence: N(h) = 1 + N(h-1) + N(h-2)
Minimum nodes for height 0 is 1, height 1 is 2, height 2 is 4, height 3 is 7 nodes.
