A Binary Search Tree 17 1026 1462034 113137. Binary Search Trees.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 6.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
353 3/30/98 CSE 143 Trees [Sections , 13.6,13.8]
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Trees Chapter 15.
Data Structure and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Binary Search Tree (BST)
CS 302 Data Structures Trees.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Binary Trees, Binary Search Trees
Trees Chapter 10.
Trees.
CSC 143 Java Trees.
Copyright  1997 Oxford University Press All Rights Reserved
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

A Binary Search Tree

Binary Search Trees

An ordered tree ADT BSTs are ordered BSTs provide for fast retrieval and insertion BSTs also support sequential processing of elements

Definition of BST A Binary Search Tree (BST) is either 1. An empty tree, or 2. A tree consisting of a node, called the root, and two children called left and right, each of which is also a BST. Each node contains a value such that the root is greater than all node values stored in its left subtree and less than all values stored in the right subtree.

The BST invariant The invariant is the ordering property “less than goes left, greater than goes right.”

Not a BST: invariant is violated

Not a BST: subtree is not a BST

Binary Search Tree ADT Characteristics A Binary Search Tree ADT T stores data of some type (btElementType) Obeys definition of BST (see earlier slide)

BST ADT Prerequisites Prerequisites The data type btElementType must implement the < and == operators. (operator overloading)

BST ADT Operations isEmpty()// check for empty BST getData()// accessor insert()// inserts new node retrieve()// returns pointer to a BST left()// returns left child right()// returns right child

isEmpty bool T.isEmpty() Precondition: None. Postcondition: None. Returns: true if and only if T is an empty tree.

getData() btElementType T.getData() Precondition: !T.isEmpty() Postcondition: None. Returns: The data associated with the root of the tree

Insert() void T.insert(btElementType d) Precondition: !T.isEmpty(); T meets the BST invariant Postcondition: T.retrieve(d).getData() == d; T meets the BST invariant.

retrieve() BinaryTree T.retrieve(btElementType d) Precondition: T meets the BST invariant. Postcondition: T meets the BST invariant. Returns:if T contains a node with data d, then T.retrieve(d).getData() == d; otherwise, T.retrieve(d).isEmpty().

left() and right() BinaryTree T.left() Precondition: !T.isEmpty() Postcondtion: None. Returns: The left child of T BinaryTree T.right() Precondition: !T.isEmpty() Postcondtion: None. Returns: The right child of T

Where 12 would be inserted

After 12 inserted

BST class definition template class BST { public: BST(); bool isEmpty() const; // Precondition: None. // Postcondition: None. // Returns: true if and only if T is an empty tree

getData() btElementType getData() const; // Precondition: !this->isEmpty() // Postcondition: None // Returns: The data associated with the root of the tree

Insert() void insert(const btElementType & d); // Precondition: if d is a left child, then d must be getData(); // if d is a right child, then d must be > parent->getData(); // Postconditions: T->retrieve(d)->getData() == d

retrieve() BST * retrieve(const btElementType & d); // Precondition: none // Postcondition: none // Returns: if T contains a node matching d, // then T->retrieve(d)->getData() == d; otherwise, T->isEmpty()

left(), right() BST * left(); // Precondition: !this->isEmpty() // Postcondition: None // Returns: (a pointer to) the left child of T BST * right(); // Precondition: !this->isEmpty() // Postcondition: None // Returns: (a pointer to) the right child of T

private section private: bool nullTree; btElementType treeData; BST * leftTree; BST * rightTree; }; nullTree leftright treeData

Implementation: constructor template BST :: BST() { nullTree = true; leftTree = 0; rightTree = 0; } true 00 nullTree leftright ? treeData DANGER

isEmpty() template bool BST :: isEmpty() const { return nullTree; } true 00 nullTree leftright data treeData

getData() template btElementType BST :: getData() const { assert(!isEmpty()); return treeData; } true 00 nullTree leftright data treeData

insert() template void BST :: insert(const btElementType & d) { if (nullTree) { nullTree = false; leftTree = new BST; rightTree = new BST; treeData = d; } true false 00 nullTree leftright d treeData

insert (if not empty) else if (d == treeData) ; // do nothing -- it's already here! else if (d < treeData) leftTree->insert(d); // insert in left subtree else rightTree->insert(d); // insert in right subtree }

retrieve() template BST * BST :: retrieve(const btElementType & d) { if (nullTree || d == treeData) // return pointer to tree for which retrieve was called return this; else if (d < treeData) return leftTree->retrieve(d); // recurse left else return rightTree->retrieve(d); // recurse right }

left() template BST * BST :: left() { assert(!isEmpty()); return leftTree; } false nullTree leftright d treeData

right() template BST * BST :: right() { assert(!isEmpty()); return rightTree; } false nullTree leftright d treeData

Client of BST int main() { typedef BST intBST; typedef intBST * intBSTPtr; intBSTPtr b(new intBST); b->insert(17); b->insert(10); b->insert(26); b->insert(6); b->insert(14); b->insert(20); b->insert(28); b->insert(11); b->insert(31); b->insert(37); b->insert(12);

BST result

Retrieval // is 11 in the tree? intBSTPtr get11(b->retrieve(11)); if (get11->isEmpty()) cout << "11 not found.\n"; else cout << "11 found.\n"; // is 13 in the tree? intBSTPtr get13(b->retrieve(13)); if (get13->isEmpty()) cout << "13 not found.\n"; else cout << "13 found.\n"; return 0; }

found 13 not found

Reuse through inheritance Important OOP language features 1.Encapsulation: for abstractions like classes, types, objects. 2. Inheritance: abstractions can be reused 3. Polymorphism: statements that use more than one abstraction, with the language choosing the right one while the program is running.

“is-a” relations Defining one abstraction in terms of another. The Binary Tree ADT is a general class of which binary search trees are one type. Therefore, if we define a Binary Tree ADT we should be able to define a BST as a special case which inherits the characteristics of the Binary Tree ADT. A BST “is a” Binary Tree, with special features

Inheritance terminology Base class - the one from which others inherit Derived class - one which inherits from others So, the BST is a derived class of the Binary Tree base class.

Inheritance diagram BST BinaryTree

Implementing inheritance in C++ Base classes are designed so that they can be inherited from. One crucial aspect of inheritance is that the derived class may wish to implement member functions of the base case a little differently. Good examples are insertion for BST (which must follow the ordering principle) and constructors.

Virtual functions A virtual function is one which can be inherited. Base class functions should be virtual. Example: virtual BinaryTree* right();

Protected members We have used public and private sections of our class definitions. There is also ‘protected’. Protected means that the members are hidden from access (private) for everything except derived classes, which have public access rights. Derived classes can use the data members of base classes directly, without accessors

Example: Binary Tree ADT base

Binary Tree base class template class BinaryTree { public: BinaryTree();

isEmpty() and getData() virtual bool isEmpty() const; // Precondition: None. // Postcondition: None. // Returns: true if and only if T is an empty tree virtual btElementType getData() const; // Precondition: !this->isEmpty() // Postcondition: None // Returns: data associated with the root of the tree

insert() and retrieve() virtual void insert(const btElementType & d); // Precondition: none // Postconditions: this->getData() == d; !this->isEmpty() virtual BinaryTree * retrieve(const btElementType & d); // Precondition: none // Postcondition: none // Returns: this // Note: A useless stub, will be redefined by child class

left() and right() virtual BinaryTree * left(); // Precondition: !this->isEmpty() // Postcondition: None // Returns: (a pointer to) the left child of T virtual BinaryTree * right(); // Precondition: !this->isEmpty() // Postcondition: None // Returns: (a pointer to) the right child of T

makeleft() and makeRight() virtual void makeLeft(BinaryTree * T1); // Precondition: !this->isEmpty(); // this->left()->isEmpty() // Postcondition: this->left() == T1 virtual void makeRight(BinaryTree * T1); // Precondition: !this->isEmpty() // this->right()->isEmpty() // Postcondition: this->right() == T1

Binary tree class: protected section protected: bool nullTree; btElementType treeData; BinaryTree * leftTree; BinaryTree * rightTree; }; nullTree leftTreerightTree treeData

Public inheritance Inheritance notation is done like this class BST : public BinaryTree public means that BST client code must be able to access the public members of the base class. (public inheritance) It means that derived functions will only redefine member functions that they must (like insert() for BST). All other functions will be the public ones in the base class.

Derived class example: BST

BST class: the whole thing! template class BST : public BinaryTree { public: BST(); virtual void insert(const btElementType & d); virtual BinaryTree * retrieve(const btElementType & d); };

Why so short? The BST derived class definition was so short because it only needed to declare member functions that represented changes from the public functions of the base class. All other functions come from the already defined base class.

BST Derived Class Binary Tree Base Class public: protected: insert()retrieve() getData() retrieve() BinaryTree() isEmpty() insert() left() right() makeLeft() makeRight() nullTree treeData leftTreerightTree BST()

Derived class constructors To construct a BST we must first call the Binary Tree constructor (the base class) Once this has been called, then the body of the BST constructor can add additional things on to it. In this case, we have nothing to add (see next slide)

Constructor (inheritance) template BST :: BST() : BinaryTree () { }

Performance of binary trees Shape and balance are very important Short and wide trees are better than long and narrow ones. The depth of the tree is the main consideration in all traversal routines. Traversals are used by insertion and retrieval functions which must first look up a location in the tree. Examples using the preorder traversal

Reminder of the preorder traversal if the tree is not empty visit the root preOrderTraverse(left child) preOrderTraverse(right child)

A tree that is expensive to process

An efficient tree

Shallowest trees for n=1,3,7, and 15

Recurrence relation Let d stand for the depth of the tree (0,1,2...) The maximum number of nodes a balanced tree of a given depth can contain is given by the equation below (note recursive definition). With base case f(0) = 1 f(d) = f(d - 1) + f(d - 1) + 1 = 2f(d - 1) + 1

results f(0) = 1 f(1) = 3 f(2) = 7 f(3) = 15 f(4) = 31

Non-recursive recurrence relation

How many nodes as f(d)

How deep is a tree with n nodes? hence:

So, if a tree has n nodes, and is balanced, its depth can be approximated. Example: n = 37 2^5 = 32, 2^6 = 64 Therefore,the tree must be at least 5 levels deep, no less.

Analysis Earlier we talked about how an inorder BST can access data in sorted order using a preorder traversal. What is the efficiency of this kind of sort?

Efficiency of Treesort Placing all items in the tree: –There are n items –On average, if tree is balanced, it takes logn (log(base2)n) operations to find the place to insert each item. –Therefore, we have O(nlogn) To print it out is O(n) So the whole operation is O(nlogn) + O(n)

In the final analysis The total complexity of sorting and printing is O(nlogn) + O(n) Taking the highest order term we get O(nlogn) This is on the same order as Quicksort and Mergesort. - VERY FAST

Chapter Summary Tree represent data in a hierarchical manner. In a Binary Tree, each node has a left and a right child. Expression Trees are a Binary Tree that can represent arithmetic expressions. The data in a tree can be visited using in-order, pre-order, and post-order traversals. Function pointers can be used to pass one function to another as an argument. The Binary Search Tree is an ordered tree ADT.

Chapter Summary Binary Search Trees support efficient retrieval by key Inheritance can be used to model is-a relations. Inheritance can be implemented in C++ through base and derived classes. Inheritance supports code reuse. Binary Search Tree can have bad performance if they’re unbalanced, but very good performance when balanced. Treesort sorts a list using a Binary Search Tree.