Example #1
0
/* This function takes a sorted array with a range and returns a balanced BST.
 * Hint: think about how we did binary search. Each time take the middle element to form the node. Construct its children recursively.
 * Parameters:
 * array - the array provides the data fields of nodes of bst. Assume it's already sorted.
 * startIndex & endIndex - only elements in the range defined by them (inclusive) will used to construct the bst
 * Returns:
 * the pointer to the root of the balanced bst
 */
Node* sortedArrayToBalancedBst(int* array, int startIndex, int endIndex) {
	if(startIndex > endIndex)
        return NULL;
    int mid = startIndex + (endIndex - startIndex) /2;
    Node* node = createNode(array[mid]);
    node->left = sortedArrayToBalancedBst(array,startIndex,mid-1);
    node->right = sortedArrayToBalancedBst(array,mid+1,endIndex);
    return node;
}
struct Node* sortedArrayToBalancedBst(int a[], int beg, int end){
	
	if(beg > end){
		return NULL;
	}
	
	int mid = beg + (end-beg)/2;
	struct Node *root = newNode(a[mid]);
	
	if(beg == end){
		return root;
	}
	
	root->left = sortedArrayToBalancedBst(a,beg,mid-1);
	root->right = sortedArrayToBalancedBst(a,mid+1,end);
	
	return root;
}
int main(){
	struct Node *root = NULL;
	root = insert(root,10);
	insert(root,7);
	insert(root,16);
	insert(root,9);
	insert(root,8);
	insert(root,5);
	insert(root,22);
	
	printf("\nInorder : ");
	inorder(root);
	
	int key = 10;
	struct Node *keyNode = search(root,key);
	printf("\nSearch for %d : %d and parent: %d",key, keyNode == NULL ? -1 : keyNode->data);
	
	//printf("\nDeleting key %d : %d",key,delete_key(root,key)->data);
	printf("\nInorder : ");
	inorder(root);
	
	printf("\nIs tree BST : %d",isBst(root));
	int key1 = 8;
	int key2 = 22;
	struct Node  *lca = lowestCommonAncestor(root,key1,key2);
	printf("\nLowest Common Ancestor for %d and %d is : %d",key1,key2,lca == NULL ? -1 : lca->data);
	
	int a[] = {4, 2, 5, 1, 3};
	int sa = sizeof(a)/sizeof(a[0]);
	printf("\nArray is printed in ascending order : ");
	printSorted(a,0,sa);
	
	int item = 5;
	struct Node* succ = inorder_successor(root,item);
	printf("\nInorder Successor for %d is : %d",item,succ == NULL ? -1 : succ->data);
	
	int k = 5;
	printf("\n%d smallest key in BST is %d",k,kth_smallest_bst(root,k));
	
	{
		int key1 = 9,key2 = 22;
		printf("\nPrinting keys in range of %d and %d : ",key1,key2);
		printKeysInRange(root,key1,key2);
	}
	
	{
		int a[] = {1, 2, 3, 4, 5, 6, 7};
		int sa = sizeof(a)/sizeof(a[0]);
		struct Node *root = sortedArrayToBalancedBst(a,0,sa-1);
		printf("\nInorder : ");
		inorder(root);
	}
	
	{
		Node* root = NULL;
    	root = insert(root, 6);
    	root = insert(root, -13);
    	root = insert(root, 14);
    	root = insert(root, -8);
    	root = insert(root, 15);
    	root = insert(root, 13);
    	root = insert(root, 7);
 
    	printf("\nInorder traversal of the given tree is: ");
    	inorder(root);
 
    	root = removeKeysOutsideRange(root, -10, 13);
 
    	printf("\nInorder traversal of the modified tree is: ");
    	inorder(root);
	}
	return 0;
}
Example #4
0
int main(int argc, char **argv)
{

	FILE *in = NULL;
	int num_read, array_size = 0;

	if(argc != 2){
		printf("hw4 <input-file>\n");
		return 1;
	}

	in = fopen(argv[1], "r");

	if(in == NULL){
		printf("File can not be opened.\n");
		return 2;
	}

	// declare the array
	int array[MAX_SIZE];

	// read from the second line to get each element of the array
	while(!feof(in)){
		fscanf(in, "%d", &num_read);
		if(isValidID(array, array_size, num_read) == 0) {
			array[array_size] = num_read;
			array_size++;
			printf("%d added to array\n", num_read);
		}
		else if(isValidID(array, array_size, num_read) == 1) {
			printf("ERROR - %d bad ID. Not in range [0, 99].\n", num_read);
		}
		else if(isValidID(array, array_size, num_read) == 2) {
			printf("ERROR - %d bad ID. Duplicate.\n", num_read);
		}
	}
	fclose(in);

	Node *root1 = NULL, *root2 = NULL, *root3 = NULL;

	int i;
	// task1: construct a bst from the unsorted array
	printf("=== task1: construct a bst from the unsorted array ===\n");
	for (i = 0; i < array_size; i++) {
		root1 = bstInsert(root1, array[i]);
	}
	displayTree(root1, 0);
	printf("Height of bst1 is %d\n", getBstHeight(root1));

	// task2: sort the array and use the sorted array to construct a bst, each time taking an element in order
	printf("=== task2: sort the array and use the sorted array to construct a bst ===\n");
	bsort1(array, array_size);
	for (i = 0; i < array_size; i++) {
		root2 = bstInsert(root2, array[i]);
	}
	displayTree(root2, 0);
	printf("Height of bst2 is %d\n", getBstHeight(root2));

	// task3: use the sorted array to construct a balanced bst
	printf("=== task3: use the sorted array to construct a balanced bst ===\n");
	root3 = sortedArrayToBalancedBst(array, 0, array_size-1);
	displayTree(root3, 0);
	printf("Height of bst3 is %d\n", getBstHeight(root3));

	return 0;
}