1 CSC 321: Data Structures Fall 2012 Proofs & trees  proof techniques –direct proof, proof by contradiction, proof by induction  trees  tree recursion.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 Divide & conquer  divide-and-conquer approach  familiar examples: binary search, merge sort,
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
Binary Trees, Binary Search Trees COMP171 Fall 2006.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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,
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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 ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 CSC 421: Algorithm Design & Analysis Spring 2013 Divide & conquer  divide-and-conquer approach  familiar examples: merge sort, quick sort  other examples:
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Divide & conquer (part 1)  divide-and-conquer approach  familiar examples: binary search,
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 TreeSets and TreeMaps  tree structure, root, leaves  recursive tree algorithms: counting,
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Building Java Programs Binary Trees reading: 17.1 – 17.3.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2004 Trees and nonlinear structures  tree structure, root, leaves  recursive tree algorithms:
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
Lecture 19: Binary Trees reading: 17.1 – 17.3
CSC 321: Data Structures Fall 2016 Trees & recursion
CSC 427: Data Structures and Algorithm Analysis
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
CSC 427: Data Structures and Algorithm Analysis
Recursive Objects (Part 4)
Building Java Programs
CSC 321: Data Structures Fall 2016 Counting and proofs
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
CSC 421: Algorithm Design & Analysis
CSC 321: Data Structures Fall 2017 Trees & recursion
CMSC 341 Introduction to Trees.
Trees.
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Trees.
Data Structures and Algorithms for Information Processing
CSC 321: Data Structures Fall 2018 Counting and proofs
Binary Trees, Binary Search Trees
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSC 321: Data Structures Fall 2018 Trees & recursion
Building Java Programs
CSC 143 Java Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

1 CSC 321: Data Structures Fall 2012 Proofs & trees  proof techniques –direct proof, proof by contradiction, proof by induction  trees  tree recursion  BinaryTree class

Direct proofs the simplest kind of proof is a logical explanation or demonstration CLAIM: The best case for sequential search is O(1) PROOF: Suppose the item to be found is in the first index. Then sequential search will find it on the first check. You can't find something in fewer than one check. 2 CLAIM: you can add to either end of a doubly-linked list in O(1) time. PROOF: add at front front = new DNode(3, null, front);  O(1) if (front.getNext() == null) {  O(1) back = front;  O(1) } else { front.getNext.setPrevious(front);  O(1) } add at back back = new DNode(3, back, null);  O(1) if (back.getPrevious() == null) {  O(1) front = back  O(1) } else { back.getPrevious.setNext(back);  O(1) }

Proof by contradiction to disprove something, all you need to do is find a counter-example CLAIM: every set has an even number of elements. DISPROOF: { 4 } however, you can't prove a general claim just by showing examples CLAIM: every even number is divisible by 4. PROOF?: 4 % 4 = 0, 8 % 4 = 0, 12 % 4 = 0, … 3 to prove a claim by contradiction  assume the opposite and find a logical contradiction CLAIM: there is no largest integer PROOF: Assume there exists a largest integer. Call that largest integer N. But N+1 is also an integer (since the sum of two integers is an integer), and N+1 > N. This contradicts our assumption, so the original claim must be true.

Proof by induction inductive proofs are closely related to recursion  prove a parameterized claim by building up from a base case To prove some property is true for all N ≥ C (for some constant C): BASE CASE: Show that the property is true for C. HYPOTHESIS: Assume the property is true for all n < N INDUCTIVE STEP: Show that that the property is true for N. 4 CLAIM: 1+2+…+N = N(N+1)/2 BASE CASE: N =1. 1 = 1(1+1)/2 ✓ HYPOTHESIS: Assume the relation holds for all n < N, e.g., 1+2+…+(N-1) = (N-1)N/2. INDUCTIVE STEP: Then 1+2+…+(N-1)+N= [1+2+…+(N-1)]+N = (N-1)N/2 + N = (N 2 – N)/2 + 2N/2 = (N 2 + N)/2 = N(N+1)/2 ✓

Proof by induction 5 CLAIM: The recurrence relation Cost(N) = Cost(N-1) + C has the closed- form solution Cost(N) = CN BASE CASE: N =1. Cost(1) = Cost(0) + C = C HYPOTHESIS: Assume the relation holds for n < N, e.g., Cost(N-1) = C(N-1) INDUCTIVE STEP: Then Cost(N) = Cost(N-1) + Cby definition = C(N-1) + Cby induction hypothesis = C((N-1) + 1) = CN ✓ FUNDAMENTAL THEOREM OF ARITHMETIC: every integer N > 1 is either prime or the product of primes BASE CASE? INDUCTION?

6 Trees a tree is a nonlinear data structure consisting of nodes (structures containing data) and edges (connections between nodes), such that:  one node, the root, has no parent (node connected from above)  every other node has exactly one parent node  there is a unique path from the root to each node (i.e., the tree is connected and there are no cycles) nodes that have no children (nodes connected below them) are known as leaves

7 Recursive definition of a tree trees are naturally recursive data structures:  the empty tree (with no nodes) is a tree  a node with subtrees connected below is a tree tree with 7 nodes a tree where each node has at most 2 subtrees (children) is a binary tree empty tree tree with 1 node (empty subtrees)

8 Trees in CS trees are fundamental data structures in computer science example: file structure  an OS will maintain a directory/file hierarchy as a tree structure  files are stored as leaves; directories are stored as internal (non-leaf) nodes descending down the hierarchy to a subdirectory  traversing an edge down to a child node DISCLAIMER: directories contain links back to their parent directories, so not strictly a tree

9 Recursively listing files to traverse an arbitrary directory structure, need recursion to list a file system object (either a directory or file): 1.print the name of the current object 2.if the object is a directory, then ─recursively list each file system object in the directory in pseudocode: public static void ListAll(FileSystemObject current) { System.out.println(current.getName()); if (current.isDirectory()) { for (FileSystemObject obj : current.getContents()) { ListAll(obj); }

10 Recursively listing files public static void ListAll(FileSystemObject current) { System.out.println(current.getName()); if (current.isDirectory()) { for (FileSystemObject obj : current.getContents()) { ListAll(obj); } this method performs a pre-order traversal : prints the root first, then the subtrees

11 UNIX du command in UNIX, the du command lists the size of all files and directories from the ~davereed directory: unix> du –a 2./public_html/index.html 3./public_html/Images/reed.jpg 3./public_html/Images/logo.gif 7./public_html/Images 10./public_html 1./mail/dead.letter 2./mail 13. public static int du(FileSystemObject current) { int size = current.blockSize(); if (current.isDirectory()) { for (FileSystemObject obj : current.getContents()) { size += du(obj); } System.out.println(size + " " + current.getName()); return size; } this method performs a post-order traversal : prints the subtrees first, then the root

12 How deep is a balanced tree? Proof (by induction): BASE CASES: when H = 0, = 0 nodes when H = 1, = 1 node HYPOTHESIS: Assume for all h < H, e.g., a tree with height H-1 can store up to 2 H-1 -1 nodes. INDUCTIVE STEP: A tree with height H has a root and subtrees with height up to H-1. By our hypothesis, T1 and T2 can each store 2 H-1 -1 nodes, so tree with height H can store up to 1 + (2 H-1 -1) + (2 H-1 -1) = 2 H H-1 -1 = 2 H -1 nodes equivalently: N nodes can be stored in a binary tree of height  log 2 (N+1)  CLAIM: A binary tree with height H can store up to 2 H -1 nodes.

13 Trees & recursion since trees are recursive structures, most tree traversal and manipulation operations are also recursive  can divide a tree into root + left subtree + right subtree  most tree operations handle the root as a special case, then recursively process the subtrees  e.g., to display all the values in a (nonempty) binary tree, divide into 1.displaying the root 2.(recursively) displaying all the values in the left subtree 3.(recursively) displaying all the values in the right subtree  e.g., to count number of nodes in a (nonempty) binary tree, divide into 1.(recursively) counting the nodes in the left subtree 2.(recursively) counting the nodes in the right subtree 3.adding the two counts + 1 for the root

BinaryTree class public class BinaryTree { protected class TreeNode { … } protected TreeNode root; public BinaryTree() { this.root = null; } public void add(E value) { … } public boolean remove(E value) { … } public boolean contains(E value) { … } public int size() { … } public String toString() { … } } 14 to implement a binary tree, need to link together tree nodes  the root of the tree is maintained in a field (initially null for empty tree)  the root field is "protected" instead of "private" to allow for inheritance  recall: a protected field is accessible to derived classes, otherwise private

TreeNode class protected class TreeNode { private E data; private TreeNode left; private TreeNode right; public TreeNode(E d, TreeNode l, TreeNode r) { this.data = d; this.left = l; this.right = r; } public E getData() { return this.data; } public TreeNode getLeft() { return this.left; } public TreeNode getRight() { return this.right; } public void setData(E newData) { this.data = newData; } public void setLeft(TreeNode newLeft) { this.left = newLeft; } public void setRight(TreeNode newRight) { this.right = newRight; } 15 virtually same as DNode class  change the field & method names to reflect the orientation of nodes  uses left/right instead of previous/next

16 size method public int size() { return this.size(this.root); } private int size(TreeNode current) { if (current == null) { return 0; } else { return this.size(current.getLeft()) + this.size(current.getRight()) + 1; } } recursive approach: BASE CASE: if the tree is empty, number of nodes is 0 RECURSIVE: otherwise, number of nodes is (# nodes in left subtree) + (# nodes in right subtree) + 1 for the root note: a recursive implementation requires passing the root as parameter  will have a public "front" method, which calls the recursive "worker" method

17 contains method public boolean contains(E value) { return this.contains(this.root, value); } private boolean contains(TreeNode current, E value) { if (current == null) { return false; } else { return value.equals(current.getData()) || this.contains(current.getLeft(), value) || this.contains(current.getRight(), value); } } recursive approach: BASE CASE: if the tree is empty, the item is not found BASE CASE: otherwise, if the item is at the root, then found RECURSIVE: otherwise, search the left and then right subtrees

18 toString method public String toString() { if (this.root == null) { return "[]"; } String recStr = this.toString(this.root); return "[" + recStr.substring(0,recStr.length()-1) + "]"; } private String toString(TreeNode current) { if (current == null) { return ""; } return this.toString(current.getLeft()) + current.getData().toString() + "," + this.toString(current.getRight()); } must traverse the entire tree and build a string of the items  there are numerous patterns that can be used, e.g., in-order traversal BASE CASE: if the tree is empty, then nothing to traverse RECURSIVE: recursively traverse the left subtree, then access the root, then recursively traverse the right subtree

19 Alternative traversal algorithms private String toString(TreeNode current) { if (current == null) { return ""; } return current.getData().toString() + "," + this.toString(current.getLeft()) + this.toString(current.getRight()); } private String toString(TreeNode current) { if (current == null) { return ""; } return this.toString(current.getLeft()) + this.toString(current.getRight()) + current.getData().toString() + ","; } pre-order traversal: BASE CASE: if the tree is empty, then nothing to traverse RECURSIVE: access root, recursively traverse left subtree, then right subtree post-order traversal: BASE CASE: if the tree is empty, then nothing to traverse RECURSIVE: recursively traverse left subtree, then right subtree, then root

20 Exercises the number of times value occurs in the tree with specified root */ public int numOccur(TreeNode root, E value) { } the sum of all the values stored in the tree with specified root */ public int sum(TreeNode root) { } the maximum value in the tree with specified root */ public int max(TreeNode root) { }

21 add method public void add(E value) { this.root = this.add(this.root, value); } private TreeNode add(TreeNode current, E value) { if (current == null) { current = new TreeNode (value, null, null); } else if (this.size(current.getLeft()) <= this.size(current.getRight())) { current.setLeft(this.add(current.getLeft(), value)); } else { current.setRight(this.add(current.getRight(), value)); } return current; } how do you add to a binary tree?  ideally would like to maintain balance, so (recursively) add to smaller subtree  big Oh?  we will consider more efficient approaches for maintaining balance later

22 remove method how do you remove from a binary tree?  tricky, since removing an internal node means rerouting pointers  must maintain binary tree structure simpler solution 1.find node (as in search) 2.if a leaf, simply remove it 3.if no left subtree, reroute parent pointer to right subtree 4.otherwise, replace current value with a leaf value from the left subtree (and remove the leaf node) DOES THIS MAINTAIN BALANCE? (you can see the implementation in BinaryTree.java)

Induction and trees which of the following are true? prove it  in a full binary tree, there are more nodes on the bottom (deepest) level than all other levels combined  in any binary tree, there will always be more leaves than non-leaves  in any binary tree, there will always be more empty children (i.e., null left or right fields within nodes) than children (i.e., non-null fields)  the number of nodes in a binary tree can be defined by the recurrence relation: numNodes(T) = numNodes(left subtree of T) + numNodes(right subtree of T)