Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Trees Types and Operations
Binary Search Trees Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Binary Search Trees Chapter 7 Objectives
1 Trees 3: The Binary Search Tree Section Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 ),
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Binary Search Tree Qamar Abbas.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search 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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Red-Black Trees Definitions and Bottom-Up Insertion.
Red Black Trees Top-Down Deletion. Recall the rules for BST deletion 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Binary Search Trees Chapter 7 Objectives
Data Structure and Algorithms
Recitation 3 (Heap Sort and Binary Search Tree)
Chapter 25 Binary Search Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Search 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.
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
Red Black Trees Top-Down Deletion.
Chapter 12: Binary Search Trees
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Trees 3 The Binary Search Tree Section 4.3

Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search tree iff:  There is an order relation ≤ defined for the vertices of B  For any vertex v, and any descendant u of v.left, u ≤ v  For any vertex v, and any descendent w of v.right, v ≤ w

Binary Search Tree Which one is NOT a BST?

Binary Search Tree Consequences:  The smallest element in a binary search tree (BST) is the “left- most” node  The largest element in a BST is the “right-most” node  Inorder traversal of a BST encounters nodes in increasing order root

Binary Search using BST Assumes nodes are organized in a totally ordered binary tree  Begin at root node  Descend using comparison to make left/right decision if (search_value < node_value) “go to the left child” else if (search_value > node_value) “go to the right child” else return true // success  Until descending move is impossible  Return false (failure)

BST Class Template

BST Class Template (contd.) Internal functions used in recursive calls Pointer passed by reference (why?)

BST: Public members calling private recursive functions

BST: Searching for an element

BST: Search using function objects

BST: Find the smallest element Tail recursion

BST: Find the biggest element Non-recursive

BST: Insertion Before insertion After insertion

BST: Insertion (contd.) Strategy: Traverse the tree as in searching for t with contains() Insert if you cannot find the element t.

BST: Deletion Before delete(4) After delete(4) Deleting a node with one child Deletion Strategy: Bypass the node being deleted

BST: Deletion (contd.) Before delete(2) After delete (2) Deleting a node with two children Deletion Strategy: Replace the node with smallest node in the right subtree

BST: Deletion (contd.)

BST: Lazy Deletion Another deletion strategy  Don’t delete!  Just mark the node as deleted.  Wastes space  But useful if deletions are rare or space is not a concern.

BST: Destructor

BST: Assignment Operator

BST: Insertion Bias Start with an empty tree. Insert elements in sorted order What tree do you get? How do you fix it?

BST: Deletion Bias After large number of alternating insertions and deletions Why this bias? How do you fix it?