CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.

Slides:



Advertisements
Similar presentations
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.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Dan Grossman Spring 2010.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
CSE 326 Binary Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CSE 326: Data Structures Binary Search Trees. Today’s Outline Dictionary ADT / Search ADT Quick Tree Review Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
CSE 326: Data Structures AVL Trees
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
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 Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CPSC 221: Data Structures Lecture #5 Branching Out Steve Wolfman 2014W1 1.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
Starting at Binary Trees
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
Data Structures AVL Trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Dan Grossman Fall 2013.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Kevin Quinn Fall 2015.
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.
CSE 373 Data Structures Lecture 7
CSE373: Data Structures & Algorithms Priority Queues
Instructor: Lilian de Greef Quarter: Summer 2017
CC 215 Data Structures Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Binary Search Trees.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CSE 373 Data Structures Lecture 7
Cse 373 April 26th – Exam Review.
Binary Search Trees Why this is a useful data structure. Terminology
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Trees CSE 373 Data Structures.
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions Binary Search Trees
CSE 373 Data Structures and Algorithms
Binary SearchTrees [CLRS] – Chap 12.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Presentation transcript:

CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007

2 Tree Calculations Recall: height is max number of edges from root to a leaf Find the height of the tree... t Can you do this in less than O(n) time?

3 Tree Calculations Example A E B DF C G IH KJL M L N How high is this tree?

4 Binary Trees Binary tree is –a root –left subtree (maybe empty) –right subtree (maybe empty) Representation: A B DE C F HG JI Data right pointer left pointer

5 Binary Tree: Representation A right pointer left pointer A B DE C F B right pointer left pointer C right pointer left pointer D right pointer left pointer E right pointer left pointer F right pointer left pointer

6 Binary Tree: Special Cases A B DE C GF IH A B DE C F A B DE C GF Full Tree Complete TreePerfect Tree

7 Binary Tree: Some Numbers! For binary tree of height h: –max # of leaves: –max # of nodes: –min # of leaves: –min # of nodes:

8 More Recursive Tree Calculations: Tree Traversals A traversal is an order for visiting all the nodes of a tree Three types: Pre-order:Root, left subtree, right subtree In-order:Left subtree, root, right subtree Post-order:Left subtree, right subtree, root + * 24 5 (an expression tree)

9 Traversals void traverse(BNode t){ if (t != NULL) traverse (t.left); print t.element; traverse (t.right); } Which kind of traversal is this?

10 ADTs Seen So Far Stack –Push –Pop Queue –Enqueue –Dequeue What about decreaseKey? Priority Queue –Insert –DeleteMin

11 The Dictionary ADT Data: –a set of (key, value) pairs Operations: –Insert (key, value) –Find (key) –Remove (key) The Dictionary ADT is sometimes called the “Map ADT” blerner Ben Lerner, OH: Tues, 10:45—12:00 CSE 212 ioannis Ioannis Giotis, OH: M 11:00 CSE 430 gyanit Gyanit Singh, OH: M 11:00 CSE 430 insert(blerner, ….) find(ioannis) ioannis Ioannis Giotis, …

12 A Modest Few Uses Sets Dictionaries Networks: Router tables Operating systems: Page tables Compilers: Symbol tables Probably the most widely used ADT!

13 Implementations Unsorted Linked-list Unsorted array Sorted array insert delete find

14 Binary Search Tree Data Structure Structural property –each node has  2 children –result: storage is small operations are simple average depth is small Order property –all keys in left subtree smaller than root’s key –all keys in right subtree larger than root’s key –result: easy to find any given key What must I know about what I store?

15 Example and Counter-Example BINARY SEARCH TREE NOT A BINARY SEARCH TREE 7 15

16 Find in BST, Recursive Node Find(Object key, Node root) { if (root == NULL) return NULL; if (key < root.key) return Find(key, root.left); else if (key > root.key) return Find(key, root.right); else return root; } Runtime:

17 Find in BST, Iterative Node Find(Object key, Node root) { while (root != NULL && root.key != key) { if (key < root.key) root = root.left; else root = root.right; } return root; } Runtime:

18 Insert in BST Runtime: Insert(13) Insert(8) Insert(31) Insertions happen only at the leaves – easy!

19 BuildTree for BST Suppose keys 1, 2, 3, 4, 5, 6, 7, 8, 9 are inserted into an initially empty BST. Runtime depends on the order! –in given order –in reverse order –median first, then left median, right median, etc.

20 Bonus: FindMin/FindMax Find minimum Find maximum

21 Deletion in BST Why might deletion be harder than insertion?

22 Delete Operation Delete is a bit trickier…Why? Suppose you want to delete 10 Strategy: –Find 10 –Delete the node containing 10 Problem: When you delete a node, what do you replace it by?

23 Delete Operation Problem: When you delete a node, what do you replace it by? Solution: –If it has no children, by NULL –If it has 1 child, by that child –If it has 2 children, by the node with the smallest value in its right subtree (the successor of the node)

24 Delete “5” - No children Find 5 node Then Free the 5 node and NULL the pointer to it

25 Delete “24” - One child Find 24 node Then Free the 24 node and replace the pointer to it with a pointer to its child

26 Delete “10” - two children Find 10, Copy the smallest value in right subtree into the node Then recursively Delete node with smallest value in right subtree Note: it does not have two children

27 Delete “11” - One child Remember 11 node Then Free the 11 node and replace the pointer to it with a pointer to its child

28 Runtimes Find? Insert? Delete? What is the average height of a BST? What is the maximum height? What happens when we insert nodes in sorted order?

29 Balanced BST Observation BST: the shallower the better! For a BST with n nodes –Average height is O(log n) –Worst case height is O(n) Simple cases such as insert(1, 2, 3,..., n) lead to the worst case scenario Solution: Require a Balance Condition that 1.ensures depth is O(log n) – strong enough! 2.is easy to maintain – not too strong!

30 Potential Balance Conditions 1.Left and right subtrees of the root have equal number of nodes 2.Left and right subtrees of the root have equal height

31 Potential Balance Conditions 3.Left and right subtrees of every node have equal number of nodes 4.Left and right subtrees of every node have equal height

32 The AVL Balance Condition Left and right subtrees of every node have equal heights differing by at most 1 Define: balance(x) = height(x.left) – height(x.right) AVL property: –1  balance(x)  1, for every node x Ensures small depth –Will prove this by showing that an AVL tree of height h must have a lot of (i.e. O(2 h )) nodes Easy to maintain –Using single and double rotations

33 The AVL Tree Data Structure Structural properties 1.Binary tree property 2.Balance property: balance of every node is between -1 and 1 Result: Worst case depth is O(log n) Ordering property –Same as for BST 15