Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.

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

Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Trees Types and Operations
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.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
CS 171: Introduction to Computer Science II
A balanced life is a prefect life.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Trees and Red-Black Trees Gordon College Prof. Brinton.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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 ),
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CSIT 402 Data Structures II
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
2-3 Trees, Trees Red-Black Trees
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.
Search Trees: BSTs and B-Trees David Kauchak cs161 Summer 2009.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
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 Trees (BST)
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.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Week 15 – Friday.  What did we talk about last time?  Student questions  Review up to Exam 2  Recursion  Binary trees  Heaps  Tries  B-trees.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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)
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Non Linear Data Structure
Trees Chapter 15.
B/B+ Trees 4.7.
Week 7 - Friday CS221.
Week 6 - Wednesday CS221.
Week 11 - Friday CS221.
Hashing Exercises.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Wednesday, April 18, 2018 Announcements… For Today…
Advanced Associative Structures
2-3-4 Trees Red-Black Trees
Presentation transcript:

Week 10 - Friday

 What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search

 Base case  Tells recursion when to stop  Can have multiple base cases  Have to have at least one or the recursion will never end  Recursive case  Tells recursion how to proceed one more step  Necessary to make recursion able to progress  Multiple recursive cases are possible

 Factorial: public static int factorial( int n ) { if( n == 1 ) return 1; else return n * factorial( n – 1); }

 We can define the running time of a function as a relation of the form: T(1) = c T(n) = a T(f(n)) + g(n) Example using factorial: T(1) = 1 T(n) = T(n - 1) + 1

 For recursion that on a problem size n that:  Makes a recursive calls  Divides the total work by b for each recursive call  Does f(n) non-recursive work at each call  Its running time can be given in the following form, suitable for use in the Master Theorem:

 A tree is a data structure built out of nodes with children  A general tree node can have any non- negative number of children  Every child has exactly one parent node  There are no loops in a tree  A tree expressions a hierarchy or a similar relationship

 The root is the top of the tree, the node which has no parents  A leaf of a tree is a node that has no children  An inner node is a node that does have children  An edge or a link connects a node to its children  The depth of a node is the length of the path from a node to its root  The height of the tree is the greatest depth of any node  A subtree is a node in a tree and all of its children  Level: the set of all nodes at a given depth from the root

Root Inner Nodes Leaves

 A binary tree is a tree such that each node has two or fewer children  The two children of a node are generally called the left child and the right child, respectively

 Full binary tree: every node other than the leaves has two children  Perfect binary tree: a full binary tree where all leaves are at the same depth  Complete binary tree: every level, except possibly the last, is completely filled, with all nodes to the left  Balanced binary tree: the depths of all the leaves differ by at most 1

 A binary search tree is binary tree with three properties: 1. The left subtree of the root only contains nodes with keys less than the root’s key 2. The right subtree of the root only contains nodes with keys greater than the root’s key 3. Both the left and the right subtrees are also binary search trees

 Keeping data organized  Easy to produce a sorted order in O(n) time  Find, add, and delete are all O(log n) time

public class Tree { private static class Node { public int key; public String value; public Node left; public Node right; } private Node root = null; … }

 Visiting every node in a tree is called a traversal  There are four traversals that we are interested in  Preorder  Postorder  Inorder  Level Order  The first three are depth first, the last is breadth first

 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

 We can have a balanced tree by:  Doing red-black (or AVL) inserts  Balancing a tree by construction (sort, then add)  DSW algorithm: completely unbalance then rebalance

 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

 We can do an insertion with a red-black tree using a series of rotations and recolors  We do a regular BST insert  Then, we work back up the tree as the recursion unwinds  If the right child is red and the left is black, we rotate the current node left  If the left child is red and the left child of the left child is red, we rotate the current node right  If both children are red, we recolor them black and the current node red  You have to do all these checks, in order!  Multiple rotations can happen  It doesn't make sense to have a red root, so we always color the root black after the insert

We perform a left rotation when the right child is red Y Y X X B B A A C C Current Y Y X X B B A A C C

We perform a right rotation when the left child is red and its left child is red Z Z Y Y B B A A D D Current X X C C Z Z Y Y B B A A D D X X C C

We recolor both children and the current node when both children are red Y Y X X B B A A D D Current Z Z C C Y Y X X B B A A D D Z Z C C

 Learn how to do 2-3 tree insertions really well  Then, learn how you can map a 2-3 tree onto a red-back tree  It's much easier to make a 2-3 tree and then figure out the corresponding red-black tree than it is to build a red-black tree from scratch

 We make a huge array, so big that we’ll have more spaces in the array than we expect data values  We use a hashing function that maps items to indexes in the array  Using the hashing function, we know where to put each item but also where to look for a particular item

 We are using a hash table for a space/time tradeoff  Lots of space means we can get down to O(1)  How much space do we need?  When the table gets too full, we may need to rehash everything  How do we pick a good hashing function?  What happens if two values collide (map to the same location)

 With open addressing, we look for some empty spot in the hash table to put the item  There are a few common strategies  Linear probing  Quadratic probing  Double hashing  Alternatively, we can use chaining

 T(n) = 9T(n/3) + 13n 2 log 3 n  Sometimes it helps to think about how I create questions:  Generate a recurrence relation that fits Case 1  Generate a recurrence relation that fits Case 2  Generate a recurrence relation that fits Case 3

 Write a method that counts the number of nodes in a BST, using the definition of a BST on a previous slide private static int count(Node node) Proxy: public int count() { return count( root ); }

 Write a method that tests to see if a BST maintains its ordering property (left is smaller, right is larger) private static boolean isSearchTree(Node node, int min, int max) Proxy: public boolean isSearchTree() { return isSearchTree(root, Integer.MIN_VALUE, Integer.MAX_VALUE); }

 Write a method to determine if two String objects are permutations of each other  Do it in Θ(n) time where n is the length of the String values  Hint: Use a hash table: HashMap

 Exam 2!

 Keep working on Project 3  Finish Assignment 5  Due tonight before midnight!  Study for Exam 2