



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




33-0: Sorting
33-1: Sorted Lists
33-2: Sorted Lists
33-3: Sorted Lists
33-4: Binary Search
33-5: Binary Search
boolean binarySearch(int A[], int elem) { int lowIndex = 0; int highInidex = A.length - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) / 2; if (A[midIndex] == elem) return true; else if (A[midIndex] < elem) highIndex = midIndex - 1; else lowIndex = midIndex + 1; } return false; }
33-6: Binary Search
boolean binarySearch(int A[], int elem, int lowIndex, int highIndex) { if (lowIndex > highIndex) return false; int midIndex = (lowIndex + highIndex) / 2; if (A[midIndex] == elem) return true; else if (A[midIndex] < elem) return binarySearch(A, elem, lowIndex, midIndex - 1); else return binarySearch(A, elem, midIndex + 1, highIndex); }
33-7: Sorted Lists
33-8: Binary Search
33-9: Binary Search
4 8 12 15 22 25 28
33-10: Binary Search
33-16: Example Binary Search Trees
4
3
2
1
4
2 6
1 3 5 7
1
2
3
4
33-17: Binary Search Trees
class BSTNode { private BSTNode left; private BSTNode right; private int data; public BSTNode(BSTNode left, BSTNode right, int data) { this.left = left; this.right = right; this.data = data; } public BSTNode left() { return left; }public BSTNode right() public int data() { { return right; return data; } } }
33-18: Finding an Element in a BST
33-19: Writing a Recursive Algorithm
33-20: Finding an Element in a BST
33-21: Finding an Element in a BST
33-22: Finding an Element in a BST
33-23: Finding an Element in a BST
33-24: Finding an Element in a BST
33-25: Finding an Element in a BST
33-26: Finding an Element in a BST
33-27: Finding an Element in a BST To find an element e in a Binary Search Tree T :
33-33: Printing out a BST
void print(Node tree) { if (tree != null) { print(tree.left()); System.out.println(tree.element()); print(tree.right()); } }