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

    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

    Cse 373 Final Note

    • 955 Words
    • 4 Pages

    An ordered or sorted binary tree, is a node based binary tree data structure that has the following properties:…

    • 955 Words
    • 4 Pages
    Good 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

    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
  • Better Essays

    Symbol Table

    • 1792 Words
    • 8 Pages

    A symbol table mechanism must allow us to add new entries and find existing entries. The two symbol table mechanisms are linear lists and hash tables. Each scheme is evaluated on the basis of time required to add n entries and make e inquiries. A linear list is the simplest to implement, but its performance is poor when n and e are large. Hashing schemes provide better performance for greater programming effort and space overhead.…

    • 1792 Words
    • 8 Pages
    Better 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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

    All elements to be sorted are inserted into a heap, and the heap organizes the elements added to it in such a way that either the largest value (in a max-heap) or the smallest value (in a min-heap) can be quickly extracted. Moreover, because this operation preserves the heap's structure, the largest/smallest value can be repeatedly extracted until none remain. Each time we delete (extract) the maximum, we place it in the last location of the array not yet occupied, and…

    • 3413 Words
    • 14 Pages
    Powerful Essays