CSE116/CSE504 IntroductiontoComputerScienceII Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Thursday 12:00 PM – 2:00 PM Friday 8:30 AM – 10:30 AM OR request appointment via e-mail PROFESSIONALISM Turn off and put away electronics: • • • • • cell phones pagers laptops tablets etc. ROADMAP Where we’ve been – Stack<E> – Queue<E> – Encapsulation discussion • private instance variables • exceptions • Coming up – – – – – – Trees – binary trees – binary search trees BRStruct<E> implementation and visitors array-based implementation (heaps/priority queues/JDT’s AST visitor) asymptotic notation: O(-) O(n2) and O(n log n) sorting algorithms Write-upnextweek One more visitor on LRStruct<E>, more involved. Study visitors in repo. Write-uptheweekAFTERnext visitor on BRStruct<E> Tree • An empty tree is a tree. • A node with a datum and children is a tree if the children are all trees. NODE (NON-EMPTY TREE) EMPTY TREE Terminology • Given a tree t with children c1…ck – t is the parent of the ci’s PARENT CHILDREN Terminology • Given a tree t with children c1…ck – t is the parent of the ci’s – the ci’s are siblings of each other SIBLINGS SIBLINGS Terminology • Given a tree t with children c1…ck – t is the parent of the ci’s – the ci’s are siblings of each other • A tree all of whose children are empty trees is called a leaf LEAF Terminology • Given a tree t with children c1…ck – t is the parent of the ci’s – the ci’s are siblings of each other • • A tree all of whose children are empty trees is called a leaf A node of a tree t with no parent in t is called the root of t ROOT Terminology • Given a tree t with children c1…ck – t is the parent of the ci’s – the ci’s are siblings of each other • • • A tree all of whose children are empty trees is called a leaf A node of a tree t with no parent in t is called the root of t A node which is neither a root nor a leaf is an interior node INTERIOR NODE Terminology • Given a tree t with children c1…ck – t is the parent of the ci’s – the ci’s are siblings of each other • • • • A tree all of whose children are empty trees is called a leaf A node of a tree t with no parent in t is called the root of t A node which is neither a root nor a leaf is an interior node The degree of a node is the number of children it has ROOT INTERIOR NODE LEAF EMPTY TREE Terminology • • height(empty) = -1 height(nonEmpty) = 1 + max(height(children)) HEIGHT: +2 HEIGHT: +1 HEIGHT: +1 HEIGHT: 0 HEIGHT: 0 HEIGHT: -1 Terminology • • depth(root) = 0 depth(nonRoot) = 1 + depth(parent) DEPTH: 0 DEPTH: 1 DEPTH: 2 DEPTH: 3 Terminology • • size(empty) = 0 size(nonEmpty) = 1 + Σ size(child) SIZE: 9 SIZE: 4 SIZE: 1 SIZE: 3 SIZE: 1 SIZE: 0 Binarytree • A tree in which the degree of every nonEmpty tree is 2. BRStruct design MAIN CHANGE FROM LRStruct Whatisbasic(invariant)treefunctionality? • insert new item at root – emptyState to nonEmptyState transition • remove item from root – nonEmptyState to emptyState transition • • • • set/get root set/get left / set/get right execute a visitor plus (in Java) methods inherited from Object (toString, equals, etc) Allowablestatetransitions insertRoot(item) item removeRoot() Largertreescanbeassembledfromsmallerones. insertions can only happen at empty trees removals can only happen when children are empty BinarySearchTree A binary tree which maintains the BST order condition: For every nonEmpty tree: everything in left subtree is < datum datum < everything in right subtree Note uniqueness assumption (no <= )