Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Similar documents
Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Trees. (Trees) Data Structures and Programming Spring / 28

Advanced Tree Data Structures

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Lecture 13: AVL Trees and Binary Heaps

TREES. Trees - Introduction

Binary Trees, Binary Search Trees

Why Do We Need Trees?

Binary Trees

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Algorithms. AVL Tree

Building Java Programs

Analysis of Algorithms

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

CSCI2100B Data Structures Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

COMP 250. Lecture 22. binary search trees. Oct. 30, 2017

Trees. Eric McCreath

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Trees. CSE 373 Data Structures

Data Structures Question Bank Multiple Choice

Tree Structures. A hierarchical data structure whose point of entry is the root node

COMP : Trees. COMP20012 Trees 219

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Cpt S 122 Data Structures. Data Structures Trees

Binary Trees. Height 1

CMPE 160: Introduction to Object Oriented Programming

3137 Data Structures and Algorithms in C++

Unit III - Tree TREES

Balanced Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Data Structures and Algorithms

csci 210: Data Structures Trees

CS102 Binary Search Trees

Lecture 26. Introduction to Trees. Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Lecture 23: Binary Search Trees

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Trees. A tree is a directed graph with the property

CMSC 341 Lecture 14: Priority Queues, Heaps

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Principles of Computer Science

Algorithms and Data Structures

Outline. Binary Tree

Data Structures and Algorithms for Engineers

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

CS24 Week 8 Lecture 1

Analysis of Algorithms

CISC 235: Topic 4. Balanced Binary Search Trees

Search Trees. The term refers to a family of implementations, that may have different properties. We will discuss:

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

A set of nodes (or vertices) with a single starting point

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Binary Search Trees. Analysis of Algorithms

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Self-Balancing Search Trees. Chapter 11

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Midterm solutions. n f 3 (n) = 3

CSI33 Data Structures

Programming II (CS300)

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

CS301 - Data Structures Glossary By

Basic Data Structures (Version 7) Name:

Topic 14. The BinaryTree ADT

CMSC 341. Binary Search Trees CMSC 341 BST

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

CS 261 Data Structures. AVL Trees

Lecture 6: Analysis of Algorithms (CS )

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

CSC 421: Algorithm Design Analysis. Spring 2013

Binary search trees (chapters )

Trees. Tree Structure Binary Tree Tree Traversals

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Binary search trees (chapters )

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

Chapter 20: Binary Trees

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

Transcription:

Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps Break around 11:45am 1

Hierarchical data structures So far, we ve been studying linear structures An array s memory layout and a linked list s link pointers define an underlying linear order Now, hierarchical or branching structures Trees! 2

Motivation for trees For arrays: the insert operation is Θ(n) searching is Θ(n) or Θ(log n) For linked lists: the insert operation is Θ(1) (but Θ(n) to find the right spot) searching is Θ(n) (even if the list is sorted) Is there a data structure with Θ(log n) performance for all three operations: insert, remove, and search? Try tree! Reminder: n vs. log n n = 1,048,576 log 2 n = 20 3

Tree overview A tree is a collection of nodes with the following properties: each node contains or references data (the payload). If the collection is not empty, there is one node called the root which is the top of a hierarchy. All other nodes are connected to the root node with a unique path (implies nodes only have 1 parent node). Some examples: Family trees Organization charts Directory hierarchies Parse trees in a compiler a = (b + c) * d + d a = * b c 4

Trees tree: A directed, acyclic structure of linked nodes. directed : Has one-way links between nodes. acyclic : No path wraps back around to the same node twice. binary tree: One where each node has at most two children. A binary tree is either: empty, or a root node that contains: data, a left subtree, and 5 root 7 3 a right subtree. 4 5 3 2 Recursive definition of a recursive data structure! 5

Terminology node: an object containing a data value and left/right children root: topmost node of a tree leaf: a node that has no children internal node; neither the root nor a leaf parent: child: sibling: a node with a common parent root 7 5 3 4 5 3 2 6

Terminology (cont.) subtree: the tree of nodes reachable to the left/right from the current node height: length of the longest path from the root to any node (single element tree is height of 0) level or depth: length of the path from a root to a given node full tree: one where every internal node has 2 children Level 0 Level 1 height = 2 5 root 7 3 Level 2 4 5 3 2 7

A tree node for integers A basic tree node object stores data and refers to left/right left data right 42 Multiple nodes can be linked together into a larger tree left data right 42 left data right left data right 59 27 left data right 86 8

Recursive data structures ---> recursive functions Number of nodes (size) in binary tree t: If t is empty size(t) = 0 else size(t) = 1 + size(leftsubtree(t)) + size(rightsubtree(t)) The height of binary tree t: If t is empty height(t) = -1 // want the height of a single node tree to be 0 else height(t) = 1 + max(height(leftsubtree(t)), height(rightsubtree(t))) 9

Full and complete trees A binary tree is full if each node is either a leaf node or a node that has 2 children. A perfect binary tree is a full binary tree with all leaf nodes at the same level. A binary tree T is complete if all levels except possibly the last are completely full, and the last level has all its nodes to the left side. Similar definitions but logically independent. 10

11

Max number of nodes The number of nodes in a perfect binary tree with h levels (0 through h)? At level h, there are 2 h nodes, so maxnodes = 2 0 + 2 1 + 2 2 +... + 2 h = 2 h+1 1 12

Min and max number of levels, h What is the minimum number of levels of a full binary tree with n nodes? Let s use results from previous slide. n = 2 h 1 h min = log 2 (n+1) What is the maximum number of levels of a binary tree? This is a degenerate case... essentially a linked list! So: h max = n 13

Tree traversal Often we traverse a tree for various operations Three simple approaches: pre-order in-order post-order These are linear-time, depth-first traversals 14

Example: traversals A B C D E F G As you traverse depth-first, left-to-right, Preorder: print the element the first time you encounter it including null pointers A B D E C F G Inorder: print the element the second time you encounter it D B E A F C G Postorder: print the element the last time you encounter it D E B F G C A 15

Pre-order traversal Pre-order: root-left-right preorder(t) { if (t is not empty) { access the root element of t; preorder(leftsubtree(t)); preorder(rightsubtree(t)); 16

In-order traversal In-order: left-root-right inorder(t) { if (t is not empty) { inorder(leftsubtree(t)); access root element of t; inorder(rightsubtree(t)); 17

Post-order traversal Post-order: left-right root postorder(t) { if (t is not empty) { postorder(leftsubtree(t)); postorder(rightsubtree(t)); access the root element of t; 18

Tree traversals Level-order traversal Example of breadth-first traversal Pre-order, in-order, post-order traversals Example of depth-first traversal For a general tree (not a binary) In-order traversal not well defined Can do level-order, pre-order, post-order 19

Level-order traversal Begins at root, visits nodes one level at a time: 1, 2, 3,..., 10, 11 20

Example implementation: levelorder traversal You can see an implementation (levelorderprint) in BST.java that we will see later (see after you study the BST implementation first) Implementation uses a queue! 21

Binary Search Trees (BSTs) A BST t is a binary tree such that either t is empty, or 1. each element in leftsubtree(t) is less than root(t), and 2. each element in rightsubtree(t) is greater than root(t), and 3. both leftsubtree(t) and rightsubtree(t) are BSTs. 7 3 13 2 5 11 17 22

Use of BSTs A tree as a set, e.g., java.util.treeset Spell checker Tree sort... A tree as a map, e.g., java.util.treemap Dictionary Address book... 23

Valid BST? Which of the trees shown are legal binary search trees? g m q 42 5 8 11 b k x 2 7 10 18 7.2 e 4 20-5 1.9 9.6 18-1 -7 8.1 21.3 24

Searching a BST Describe an algorithm for searching the tree below for 31. Then search for value 6. root What is the maximum number of nodes you would need to examine to perform any search? 12 18 35 4 15 22 58-2 7 13 16 19 31 40 87 25

Recursive search pseudocode searchtreerec(node, key) { if (node is empty) return null if (key < node s key) return searchtreerec(node s left child, key) else if (key > node s key) return searchtreerec(node s right child, key) else return node 26

Iterative search pseudocode searchtree(node, key) { while (node is not empty) { if (key < node s key) node = node s left child else if (key > node s key) node = node s right child else return node 27

Intuitive search performance of BST For now, let s assume the binary tree splits in half at every node. Every time we descend to a child node we cut the number of remaining nodes to be searched in half. Sounds familiar? Θ(log n)! 28

Balanced? A balanced tree 2 An unbalanced tree 7 3 13 3 2 5 11 17 5 7 13 11 17 29

BSTs Performance: Search: Θ(log n) Insert: Θ(log n) Delete: Θ(log n)... on average... worst case is Θ(n) but this is rare there are strategies for avoiding the worst case 30

BST comparison Operation Theta Comparison (Average Case) BST Array-based List Linked List retrieve Θ(logN) Θ(logN) (?) Θ(N) insert Θ(logN) Θ(N) Θ(N) delete Θ(logN) Θ(N) Θ(N) 31

Some properties of BSTs The minimum node in a BST (or any of its subtrees) is the left-most node. To find the minimum node, we traverse down the left subtree until we find a node with an empty left child reference. The maximum node in a BST (or any of its subtrees) is the right-most node. To find the maximum node, we traverse down the right subtree until we find a node with an empty right child reference. An inorder traversal generates a list of the nodes in sorted order. This is the idea behind Tree Sort: put a random input sequence into a BST and then use an inorder traversal when you want the sorted sequence in a list. See successor method in BST.java (which is used in iterator) 32

Minimum and maximum value 50 20 75 no left child so, minimum node 11 31 98 no right child so, maximum node 13 23 39 80 150 77 99 33

findmin, iterative // returns the minimum value from this BST public int findmin() { if (root == null) { throw new NoSuchElementException(); return findminnode(root).element; private Node findminnode(node nptr) { if (nptr == null) return null; while (nptr.left!= null) { nptr = nptr.left; return nptr; -3 42 60 91 29 root 55 87 34

findmin, recursive // returns the minimum value from this BST. public int findmin() { if (root == null) { throw new NoSuchElementException(); return findmin(root); private int findmin(node nptr) { if (nptr.left == null) { return nptr.element; else { return findmin(nptr.left); 29 root 55 87-3 42 60 91 35

Adding to a BST Add 14, and 3 in that order 8 5 11 If the tree is empty, where should a new value be added? What is the general algorithm? 2 7 4 10 18 20 Is the resulting tree unique when you add an element? 36

Adding an entry into a BST Add new entry as a leaf node. Descend down the tree checking each node s left or right subtrees to see if the new node can be added there. If the tree already has the element, don t add a duplicate. Otherwise, continue. When an empty left or right reference is found, create a node and attach it there. 37

Example add method Let s look at an iterative add() method. See BST.java, UseBST.java, Student.java Let s try a recursive add() method 38

An incorrect solution // Adds the given value to this BST in sorted order. public void addrec(int value) { add(root, value); private void add(node nptr, int value) { if (nptr == null) { nptr = new Node(value); else if (nptr.element > value) { add(nptr.left, value); else if (nptr.element < value) { add(nptr.right, value); // else nptr.element == value; // a duplicate (don't add) Why doesn't this solution work? What happens if you are adding a node to an empty tree! root 55 29 87-3 42 60 91 39

A recursive add public void addrec(int val) { if (root == null) { root = new Node(val, null); else { addrec(root, val); private static void addrec(node nptr, int val) { if (val > nptr.element) { if (nptr.right == null) nptr.right = new Node(val, nptr); // 2nd is parent else addrec(nptr.right, val); else if (val < nptr.element) { if (nptr.left == null) nptr.left = new Node(val, nptr); else addrec(nptr.left, val); 40

Another recursive add // Adds the given value to this BST in sorted order. public void add(int value) { root = add(root, value); private IntTreeNode add(node nptr, int value) { if (nptr == null) { nptr = new Node(value); else if (nptr.element > value) { nptr.left = add(nptr.left, value); else if (nptr.element < value) { nptr.right = add(nptr.right, value); // else a duplicate root 55 return nptr; 29 87-3 42 60 91 41

Removing a node First we must find the node to be deleted Once we ve found the node, there are three cases to consider. Case 1: The found node is a leaf node. Simplest case. Simply set the appropriate link in the parent to null. (Yes, each node has a parent reference!) Case 2: The found node is an interior node with 1 child Set the appropriate link in the parent to the found node s child. 42

Cases 1 and 2 for removal a leaf: replace with null a node with a left child only: replace with left child a node with a right child only: replace with right child root root root root 55 55 55 42 29 29 42-3 42 42 t1.remove(55); t1.remove(-3); t1.remove(29); 43

Removing a node (cont.) Case 3: The found node is a node with 2 children. One approach is to find a successor node and replace the found node with it. The successor node is the node that would immediately follow the found node if all the nodes in the tree were laid out in a sorted linear manner (the next node from the found node in inorder traversal order). Where is this node? It s the minimum node in the right subtree i.e., the left-most node in the right subtree. Replace the found node s value with the successor node s value. Remove the successor node from the right subtree. Is the resulting tree unique when you remove a node? 44

Case 3 for removal a node with both children: replace it with min from right subtree root 55 t1.remove(55); root 60 29 87 29 87-3 42 60 91-3 42 91 45

Example remove method See BST.java, UseBST.java, Student.java 46

Recursive remove // Removes the given value from this BST, if it exists. public void remove(int value) { root = remove(root, value); private Node remove(node root, int value) { if (root == null) { return null; else if (root.element > value) { root.left = remove(root.left, value); else if (root.element < value) { root.right = remove(root.right, value); else { // root.element == value; remove this node if (root.right == null) { return root.left; // no R child; replace w/ L else if (root.left == null) { return root.right; // no L child; replace w/ R else { // both children; replace w/ min from R root.element = getmin(root.right); root.right = remove(root.right, root.element); return root; 47

Iterator Initialize it to point to the smallest element in the tree. Next() returns the next element in the inorder order in succession using the successor function. See BST.java 48

Tree shape We ve seen that the performance of search, add, and remove is dependent on the shape of the tree... specifically the tree s height. A balanced tree has a minimal height which gives the average case performance of Θ(log n). However, the shape of the tree is dependent on the order which entries are added. What kind of sequences are the worst balance-wise? 49

Tree balancing How to get a balanced tree? One approach: Sort the array of elements to be added to the tree Add the middle element of the input array, then the middle elements of the 2 halves, then the middle elements of the 4 fourths, etc. Problem: only works if we are given the input array upfront. In many cases the elements arrive asynchronously. What if we could balance the tree on the fly as elements are added and removed? 50

Self-balancing trees Two well-known algorithms for maintaining balance in a tree are: AVL Trees older technique that serves as a model for more modern techniques. Created by Russian mathematicians Georgii Adelson-Velskii and Evgenii Landis in 1962. Red Black Trees popular and efficient technique used in the Java Collections Framework. Developed in its modern form by Leonidas Guibas and Robert Sedgewick in 1978. Both algorithms utilize Tree Rotations. 51

Overview of AVL algorithm The AVL algorithm keeps track of how balanced a tree is by using a balance factor for each node. A node s balance factor is the height of the right subtree minus the height of the left subtree. A tree is unbalanced when the absolute value of the root s balance factor is greater than 1. When a new node is added that unbalances the tree, balance can be restored by performing either a single rotation or a double rotation. 52

Tree rotations See https://en.wikipedia.org/wiki/tree_rotation 53

Trees in Java Collections Framework The TreeSet class provides an implementation of the Set interface that utilizes a tree for storage. Objects are stored in sorted, ascending order. This is a balanced tree implementation. Note: as a set duplicates are not allowed. The TreeMap class provides storage and sorting of key-value pairs (see Map.Entry<K,V>). Entries are sorted by keys. Utilizes a red-black tree for storage. Note: keys as a set duplicate keys are not allowed although duplicate values are allowed. 54

What next? Study BST.java, UseBST.java, Student.java first Then: Priority queues and binary heaps next 55