Preview

Binary Search Trees

Satisfactory Essays
Open Document
Open Document
776 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Binary Search Trees
Ordered Dictionaries Binary Search Trees
<
2 1 6 9 4 = 8

Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k) closestKeyAfter(k) closestElemAfter(k)
1 Binary Search Trees 2

>

Binary Search Trees

Binary Search (§3.1.1)
Binary search performs operation findElement(k) on a dictionary implemented by means of an array-based sequence, sorted by key similar to the high-low game at each step, the number of candidate items is halved terminates after O(log n) steps

Lookup Table (§3.1.1)
A lookup table is a dictionary implemented by means of a sorted sequence
We store the items of the dictionary in an array-based sequence, sorted by key We use an external comparator for the keys

Example: findElement(7)
0 1 1 1 1 3 3 4 4 4 5 5 5 7 7 8 9 9 9 9 11 11 11 11 14 14 14 14 16 16 16 16 18 18 18 18 3 19

Performance: m 8 8 8

l
0

h
19 19 19

l
0 0

m
3 3

h
7

findElement takes O(log n) time, using binary search insertItem takes O(n) time since in the worst case we have to shift n/2 items to make room for the new item removeElement take O(n) time since in the worst case we have to shift n/2 items to compact the items after the removal

l
4

m
5

h
7

l=m =h
Binary Search Trees

The lookup table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations)
Binary Search Trees 4

Binary Search Tree (§3.1.2)
A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property:
Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) ≤ key(v) ≤ key(w)

Search (§3.1.3)
An inorder traversal of a binary search trees visits the keys in increasing order
6 2 1 4 8 9 Algorithm findElement(k, v) To search for a

You May Also Find These Documents Helpful

  • Good Essays

    Nt1420 Unit 6

    • 1145 Words
    • 5 Pages

    INSTRUCTIONS: 1. THERE ARE SIX (6) QUESTIONS IN THIS PAPER. 2. ANSWER FIVE (5) QUESTIONS ONLY. Question 1 Arrays are used when storing a large number of values. You are required to create an array named a and answer the following questions regarding array manipulation. a. Write a method fillRandom(int[] a, int min, int max), fill the array a with a random integer value. (Note: Math.random() returns a double in the range of 0.0 and 1.0, therefore it is cast to an integer number, between the minimum and maximum value). [6 marks] b. Write the Bubble sort method to sort array a into descending order. [10 marks] c. In the quicksort, an algorithm an element is chosen from the unsorted list. This element is called the…

    • 1145 Words
    • 5 Pages
    Good Essays
  • Good Essays

    Exercise 1: Review of array-based lists Create a project using the classes in the DocSharing area labeled “User-defined array list." Compile it, run it, and review the code that is given carefully. This code tests the ArrayList class provided in the lecture.…

    • 714 Words
    • 3 Pages
    Good Essays
  • Good Essays

    ECET 370 Week 5 Lab 5

    • 650 Words
    • 3 Pages

    Exercise 1: Review of the Lecture Content Create a project using the ArrayList class and the Main class provided in DocSharing. The ArrayList class contains implementations of the first three search methods explained in this week's lecture: sequential, sorted, and binary search. The Main class uses these three methods. These programs test the code discussed in the lecture. Compile the project, run it, and review the code that is given carefully.…

    • 650 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Nt1310 Unit 1 Test Paper

    • 381 Words
    • 2 Pages

    4. Create a delete function that searches the value in the tree, if it is present it deletes that value and return true else return false.…

    • 381 Words
    • 2 Pages
    Good Essays
  • Powerful Essays

    Searching and Sorting Streams and Files Graphics GUI Components and Events Mouse, Keyboard, Sounds, and Images Big-O Analysis of Algorithms The Java Collections Framework Lists and Iterators Stacks and Queues Recursion Revisited Binary Trees Lookup Tables and Hashing Heaps and Priority Queues Design Patterns…

    • 3908 Words
    • 16 Pages
    Powerful Essays
  • Powerful Essays

    Kudler

    • 5465 Words
    • 22 Pages

    2 3 3 3 4 4 5 5 5 5 6 6 7 7 7 8 8 8 9 9 9 10 10 11 11 11 12 12 12 12 13 13 14 15 16 17 18 19 20 21…

    • 5465 Words
    • 22 Pages
    Powerful Essays
  • Good Essays

    Assingment2

    • 906 Words
    • 3 Pages

    Indexes are used to boost performance in a database. Finding an individual record or set of records most efficiently done by Index. Index key is the reference point where an index is an ordered arrangement of keys and pointers. Each key is appointed to the location of the data recognized by the key. For example: when we print out at the NEU library we have given our User ID so when we print out we can easily find the documents.…

    • 906 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Hash Tables

    • 565 Words
    • 3 Pages

    The term Hash Search refers to a search, in which they key, through an algorithmic function, determines the location of the data. In essence, the goal is to find the data with only one search. The hash search runs very quickly because of this. It's Big O, or orders of power, approaches O(1) and will never exceed O(n). To accomplish this search, a program is written in which the key is passed into the hash function, which in turn, finds the address of the data needed. To start with, the function will take in a string of characters. It will then convert those characters to an ASCII value and add them together giving it a yield or position.…

    • 565 Words
    • 3 Pages
    Good Essays
  • Powerful Essays

    as a function of n? As a function of the number of bits in the binary…

    • 7778 Words
    • 48 Pages
    Powerful Essays
  • Good Essays

    Insertion Sort

    • 690 Words
    • 3 Pages

    The main operation of the algorithm is insertion. The task is to insert a value into the sorted part of the array. Let us see the variants of how we can do it.…

    • 690 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    /* ------------------------------------------------------------------------- */ // declaration of prototype function that will be used in the following program bTree Insert_Node(bTree, int); // insert node of binary tree bTree Create_Btree(int *, int); // create binary tree…

    • 404 Words
    • 12 Pages
    Satisfactory Essays
  • Powerful Essays

    Column Oriented Database

    • 3317 Words
    • 14 Pages

    Daniel Lemire, Owen Kaser, Kamel Aouiche, "Sorting improves word-aligned bitmap indexes", Data & Knowledge Engineering, Volume 69, Issue 1 (2010), pp. 3-28.…

    • 3317 Words
    • 14 Pages
    Powerful Essays
  • Satisfactory Essays

    Algorith Analysis Text Book

    • 387199 Words
    • 1549 Pages

    Introduction 1 3 The Role of Algorithms in Computing 5 1.1 Algorithms 5 1.2 Algorithms as a technology 11 Getting Started 16 2.1 Insertion sort 16 2.2 Analyzing algorithms 23 2.3 Designing algorithms 29 Growth of Functions 43 3.1 Asymptotic notation 43 3.2 Standard notations and common functions…

    • 387199 Words
    • 1549 Pages
    Satisfactory Essays
  • Powerful Essays

    This assignment has three purposes. The first is to give you experience with representations based on list structures and with methods for list processing that manipulate them. To complete the exercise, you must implement a system that stores information using embedded lists and that alters these structures through a series of operations.…

    • 1466 Words
    • 6 Pages
    Powerful Essays
  • Better Essays

    Binary Search Tree

    • 1292 Words
    • 6 Pages

    //Program – Binary Search Tree #include<iostream> using namespace std; class node { public: int data; node *left, *right; node() { left=right=NULL; } node(int val) { left=right=NULL; data=val; } }; class bst { private: node *root; void insertNode(node *&rootptr, node *pnew); void deleteNode(node *&root, int delval); int least(node *rootptr); int max(node *rootptr); void pre(node *rootptr); void post(node *rootptr); void in(node *rootptr); int countinternal(node *rootptr, int &count); void printTree(node *p, int level); int search(node *rootptr, int data); public: bst(); void callsearch(int data); void insertBST(int datain); void deleteBST(int data); void displayin(); void displaypost(); void displaypre(); void count(); void print(); void leastele(); void maxele(); }; bst::bst() { root= NULL; } void bst::print() { printTree(root, 0); }; void bst::callsearch(int data) { search(root, data); } int bst::search(node *rootptr, int data) { if(rootptr==NULL) { cout<<"Data not found"; return 0; } else if(rootptr->data==data) cout<<"Element found"<<endl; else if(data<rootptr->data) search(rootptr->left, data); else search(rootptr->right, data); } void bst::count() { int c=0; cout<<"The number of internal nodes is: "<<countinternal(root, c)<<endl; } int bst::countinternal(node *rootptr,int &count) { if(rootptr!=NULL) { countinternal(rootptr->left, count); if(rootptr->right!=NULL || rootptr->left!=NULL)…

    • 1292 Words
    • 6 Pages
    Better Essays