1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation <

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CS 201 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, §4.3 1Izmir University of Economics.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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,
© 2004 Goodrich, Tamassia Binary Search Trees   
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
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 Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
© 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
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
David Stotts Computer Science Department UNC Chapel Hill.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
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.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
CS261 Data Structures Binary Search Trees Concepts.
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.
BCA-II Data Structure Using C
Chapter 25 Binary Search Trees
Binary Search Trees A binary search tree is a binary tree
COSC160: Data Structures Binary 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
ITEC 2620M Introduction to Data Structures
Dynamic Dictionaries Primary Operations: Additional operations:
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
B- Trees D. Frey with apologies to Tom Anastasio
Binary Search Trees.
B- Trees D. Frey with apologies to Tom Anastasio
Binary Trees, Binary Search Trees
CSC 143 Binary Search Trees.
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Binary Trees, Binary Search Trees
Presentation transcript:

1 Trees 3: The Binary Search Tree Section 4.3

2 Binary Search Tree 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 in the subtree v.left, u < v –For any vertex v, and any descendent w in the subtree v.right, v < w root

3 Binary Search Tree Which one is NOT a BST?

4 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

5 Binary Search using BST Assumes nodes are organized in a binary search 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)

6 Binary Search using BST Runtime <= descending path length <= depth of tree If tree has “enough” branching, runtime is O(log n) –Worst case is O(n)

7 BST Class Template

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

9 BST: Public members calling private recursive functions

10 BST: Searching for an element

11 BST: Find the smallest element Tail recursion

12 BST: Find the biggest element Non-recursive

13 BST: Insertion (5) Before insertion After insertion

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

15 BST: Deletion of Leaf Before deleting (3) After deleting (3) Deleting a node with no child Deletion Strategy: Delete the node

16 BST: Delete a Node with One Child Before deleting (4) After deleting (4) Deleting a node with one child Deletion Strategy: Bypass the node being deleted

17 BST: Delete a Node with Two Children Before deleting (2) After deleting (2) Deleting a node with two children Replace the node with smallest node in the right subtree

18 BST: Deletion Code

19 BST Deletion Element: 5 Left: 208 Right: 0 Element: 3 Left: 0 Right: 160 Element: 4 Left: 0 Right: Address 208 Address 160 Element: 5 Left: 160 Right: 0

20 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

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

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

23 BST: Search using function objects

24 Average Search/Insert Time - 1 Average time is the average depth of a vertex –Let us compute the sum of the depths of all vertices and divide by the number of vertices –The sum of the depths is called the internal path length Give the internal path lengths for the following trees

25 Average Search/Insert Time Let D(N) be the internal path length of a tree with N vertices If the root has a left subtree with i nodes, then D(N) = D(i) + D(N-i-1) + N-1 because the depth of each vertex in the subtrees increases by

26 Average Search/Insert Time - 3 Root Subtree with N-1 nodes The average value of D(N) is given by the recurrence D(1) = 0 D(N) = 1/N[  i=0 N-1 D(i) + D(N-i-1)] + N - 1 = 2/N  i=0 N-1 D(i) + N - 1 Root Subtree with N-2 nodes Subtree with 1 node Root Subtree with N-3 nodes Subtree with 2 nodes

27 Average Search/Insert Time - 4 D(N) = 2/N  i=0 N-1 D(i) + N - 1 N D(N) = 2  i=0 N-1 D(i) + N(N - 1) (1) (N-1)D(N-1) = 2  i=0 N-2 D(i) + (N-1)(N - 2) (2) (2) - (1) gives ND(N) - (N-1)D(N-1) = 2D(N-1) + 2(N-1) ND(N) = (N+1)D(N-1) + 2(N-1) D(N)/(N+1) = D(N-1)/N + 2(N-1)/[N(N+1)] < D(N-1)/N + 2/N D(N)/(N+1) < D(N-1)/N + 2/N D(N-1)/(N) < D(N-2)/(N-1) + 2/(N-1) D(N-2)/(N-1) < D(N-3)/(N-2) + 2/(N-2)... D(2)/(3) < D(1)/2 + 2/2

28 Average Search/Insert Time - 5 D(N)/(N+1) < D(N-1)/N + 2/N < D(N-2)/(N-1) + 2/(N-1) + 2/N < D(N-3)/(N-2) + 2/(N-2) + 2/(N-1) + 2/N... < D(1)/(2) + 2/ /(N-2) + 2/(N-1) + 2/N = 2  i=2 N 1/i If we show that  i=2 N 1/i is O(log N), then we can prove that average D(N) = O(N Log N) and so the average depth is O(log N) D(N)/(N+1) < D(N-1)/N + 2/N D(N-1)/(N) < D(N-2)/(N-1) + 2/(N-1) D(N-2)/(N-1) < D(N-3)/(N-2) + 2/(N-2)... D(2)/(3) < D(1)/2 + 2/2

29 Deriving Time Complexity Using Integration Integration can be used to derive good bounds for sums of the form  i=a N f(i) when f(i) is monotonically increasing or decreasing if you know how to integrate f(x) / 2 f(x) = 1/x 1/ 3 1/ 4 Area under the rectangles is smaller than that under 1/x  i=2 4 1/i < ∫ 1 4 1/x dx  i=2 N 1/i < ∫ 1 N 1/x dx = ln (N) - ln (1) = O(log N)