Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.

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

Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
BST Data Structure A BST node contains: A BST contains
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 7 Objectives
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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:
Lecture 17 Non-Linear data structures Richard Gesick.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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 ),
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
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.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CS 367 Introduction to Data Structures Lecture 8.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
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.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 11 (continued)
Week 7 - Friday CS221.
Trees Chapter 11 (continued)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
October 30th – Priority QUeues
Trees.
Week 11 - Friday CS221.
March 31 – Priority Queues and the heap
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.
Lecture 12 CS203 1.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Heaps By JJ Shepherd.
Trees.
Non-Linear data structures
CS203 Lecture 14.
Binary Trees, Binary Search Trees
Presentation transcript:

Week 8 - Monday

 What did we talk about last time?  BST traversals  Get range implementation

 Maybe we still have a binary tree, but we don’t have any guarantees about ordering  How would you search for something?  We could use preorder or postorder traversals  These are types of depth first searches  You go to the bottom of the tree before you come back  What if we thought what we are looking for might be close to the top?

 The most logical breadth first traversal visits each level of a tree in order: 

From:

 For depth first traversals, we used a stack  What are we going to use for a BFS?  A queue!  Algorithm: 1. Put the root of the tree in the queue 2. As long as the queue is not empty: a)Dequeue the first element and process it b)Enqueue all of its children

 Write a level order (breadth first) traversal  Hint: Use an explicit queue  Non-recursive! public void levelOrder()

private static Node delete(Node node, int key) Proxy: public void delete(int key) { root = delete( root, key ); } 1. Find the node 2. Find its replacement (smallest right child or largest left child) 3. Swap out the replacement We may need some subroutines: private static Node largest(Node node, Node parent) Note: This can cause an unbalanced tree.

Student Lecture

Balance in all things

 A 2-3 search tree is a data structure that maintains balance  It is actually a ternary tree, not a binary tree  A 2-3 tree is one of the following three things:  An empty tree (null)  A 2-node (like a BST node) with a single key, smaller data on its left and larger values on its right  A 3-node with two keys and three links, all key values smaller than the first key on the left, between the two keys in the middle, and larger than the second key on the right

 The key thing that keeps a 2-3 search tree balanced is that all leaves are on the same level  Only leaves have null links  Thus, the maximum depth is somewhere between the log 3 n (the best case, where all nodes are 3-nodes) and log 2 n (the worst case, where all nodes are 2-nodes)

 We build from the bottom up  Except for an empty tree, we never put a new node in a null link  Instead, you can add a new key to a 2-node, turning it into a 3-node  Adding a new key to a 3-node forces it to break into two 2-nodes

 Starting with an empty tree, we put in a new 2-node: X Add 19 19

 When adding to a 2-node, make it a 3-node, we put in a new 2-node: Add

 When adding to a 3-node, the 3-node breaks into two 2-nodes, bringing the middle value up: Add

 When breaking a 3-node into two parts, you move the middle value up  If the node above is empty, it's a new 2-node  If the node above is a 2-node, it becomes a 3- node  If the node above is another 3-node, it also breaks into 2-nodes, which might cascade up the tree

 Add the following keys to a 2-3 tree:  62  11  32  7  45  24  88  25  28  90

 Because of the guarantees about the depth of the tree, we the following running times for 2-3 search trees  Θ(log n) insert  Θ(log n) delete (messy, but true)  Θ(log n) find (not that different from a BST find)

 How do we implement a 2-3 tree?  Answer: We don't.  It is (of course) possible, but it involves having weird 2-nodes and 3-nodes that inherit from some abstract node class or interface  It's a huge pain  Note that 2-3 trees are essentially a special case of a B-tree, and someone does have to implement those  Instead, we use red-black trees which are structurally the same as 2-3 trees (if you squint)

 Implementing red-black trees  Balancing trees by construction

 Start working on Project 3  Form teams by Friday!  Start working on Assignment 4  Due on Friday  Keep reading section 3.3