Trees Lecture 12 CS2110 – Spring 2017.

Slides:



Advertisements
Similar presentations
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Advertisements

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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Trees Weiss sec (preview) ch. 18 (sort of).
Fundamentals of Python: From First Programs Through Data Structures
Binary Trees Chapter 6.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
TREES Lecture 12 CS2110 – Fall Announcements  Prelim #1 is tonight!  Olin 155  A-L  5:30  M-Z  5:30  A4 will be posted today  Mid-semester.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Data Structures TREES.
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.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
TREES Lecture 11 CS2110 – Spring Readings and Homework  Textbook, Chapter 23, 24  Homework: A thought problem (draw pictures!)  Suppose you use.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
(c) University of Washington20-1 CSC 143 Java Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
CSE 373 Data Structures Lecture 7
Instructor: Lilian de Greef Quarter: Summer 2017
Trees Chapter 15.
CC 215 Data Structures Trees
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Week 6 - Wednesday CS221.
Trees.
Trees Lecture 12 CS2110 – Fall 2017.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees Lecture 12 CS2110 – Fall 2016.
Trees.
CSE 373 Data Structures Lecture 7
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Trees Lecture 12 CS2110 – Spring 2018.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Trees CSE 373 Data Structures.
Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Trees Lecture 12 CS2110 – Fall 2018.
General Trees & Binary Trees
Binary Trees, Binary Search Trees
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Java Trees.
Tree.
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees Lecture 12 CS2110 – Spring 2017

Important Announcements A4 is out! Due two weeks from today. Follow the timetable and enjoy a stress-free A4 experience! Mid-semester TA evaluations are open; please participate! Your feedback can help our staff improve YOUR experience for the rest of this semester. Next week’s recitation is canceled! All Tuesday sections will be office hours instead (held in same room as recitation unless noted on Piazza)

Tree Overview Tree: data structure with nodes, similar to linked list 5 4 7 8 9 2 General tree 5 4 7 8 2 Binary tree Tree: data structure with nodes, similar to linked list Each node may have zero or more successors (children) Each node has exactly one predecessor (parent) except the root, which has none All nodes are reachable from root Binary tree: tree in which each node can have at most two children: a left child and a right child 5 4 7 8 Not a tree 5 6 8 List-like tree

Binary trees were in A1! You have seen a binary tree in A1. A PhD object phd has one or two advisors. Here is an intellectual ancestral tree! phd ad1 ad2 ad1 ad2 ad1

Tree terminology M: root of this tree G: root of the left subtree of M B, H, J, N, S: leaves (they have no children) N: left child of P; S: right child of P P: parent of N M and G: ancestors of D P, N, S: descendants of W J is at depth 2 (i.e. length of path from root = no. of edges) The subtree rooted at W has height (i.e. length of longest path to a leaf) of 2 A collection of several trees is called a ...? M G W D J P B H N S

Class for binary tree node Points to left subtree (null if empty) class TreeNode<T> { private T datum; private TreeNode<T> left, right; /** Constructor: one-node tree with datum x */ public TreeNode (T d) { datum= d; left= null; right= null;} /** Constr: Tree with root value x, left tree l, right tree r */ public TreeNode (T d, TreeNode<T> l, TreeNode<T> r) { datum= d; left= l; right= r; } Points to right subtree (null if empty) more methods: getValue, setValue, getLeft, setLeft, etc.

Binary versus general tree In a binary tree, each node has up to two pointers: to the left subtree and to the right subtree: One or both could be null, meaning the subtree is empty (remember, a tree is a set of nodes) In a general tree, a node can have any number of child nodes (and they need not be ordered) Very useful in some situations ... ... one of which may be in an assignment!

Class for general tree nodes class GTreeNode<T> { private T datum; private List<GTreeNode<T>> children; //appropriate constructors, getters, //setters, etc. } 5 4 2 Parent contains a list of its children General tree 7 8 9 7 8 3 1

Class for general tree nodes class GTreeNode<T> { private T datum; private List<GTreeNode<T>> children; //appropriate constructors, getters, //setters, etc. } Java.util.List is an interface! It defines the methods that all implementation must implement. Whoever writes this class gets to decide what implementation to use — ArrayList? LinkedList? Etc.? 5 4 2 General tree 7 8 9 7 8 3 1

Applications of Tree: Syntax Trees Most languages (natural and computer) have a recursive, hierarchical structure This structure is implicit in ordinary textual representation Recursive structure can be made explicit by representing sentences in the language as trees: Abstract Syntax Trees (ASTs) ASTs are easier to optimize, generate code from, etc. than textual representation A parser converts textual representations to AST

Applications of Tree: Syntax Trees In textual representation: Parentheses show hierarchical structure In tree representation: Hierarchy is explicit in the structure of the tree We’ll talk more about expressions and trees in next lecture Text Tree Representation -34 -34 - (2 + 3) - + 2 3 ((2+3) + (5+7)) + + + 2 3 5 7

Got that? (1 + (9 – 2)) * 7 * 7 + – 1 9 2 F T *, +, and 7 are ancestors of 1  9's parent is -  The tree's height is 4  1 is a leaf node  9 is at depth 3  The root is 7

Recursion on trees Trees are defined recursively: A binary tree is either (1) empty or (2) a value (called the root value), a left binary tree, and a right binary tree

Recursion on trees Trees are defined recursively, so recursive methods can be written to process trees in an obvious way. Base case empty tree (null) leaf Recursive case solve problem on each subtree put solutions together to get solution for full tree

Class for binary tree nodes class BinTreeNode<T> { private T datum; private BinTreeNode<T> left; private BinTreeNode<T> right; //appropriate constructors, getters, //setters, etc. } 5 4 2 Binary tree 7 9 7 1

Looking at trees recursively Binary tree 2 9 8 3 5 7

Looking at trees recursively value left subtree right subtree

Searching in a Binary Tree /** Return true iff x is the datum in a node of tree t*/ public static boolean treeSearch(T x, TreeNode<T> t) { if (t == null) return false; if (x.equals(t.datum)) return true; return treeSearch(x, t.left) || treeSearch(x, t.right); } Analog of linear search in lists: given tree and an object, find out if object is stored in tree Easy to write recursively, harder to write iteratively 2 9 8 3 5 7

Searching in a Binary Tree /** Return true iff x is the datum in a node of tree t*/ public static boolean treeSearch(T x, TreeNode<T> t) { if (t == null) return false; if (x.equals(t.datum)) return true; return treeSearch(x, t.left) || treeSearch(x, t.right); } VERY IMPORTANT! We sometimes talk of t as the root of the tree. But we also use t to denote the whole tree. 2 9 8 3 5 7

Some useful methods – what do they do? /** Method A ??? */ public static boolean A(Node n) { return n != null && n.left == null && n.right == null; } /** Method B ??? */ public static int B(Node n) { if (n== null) return -1; return 1 + Math.max(B(n.left), B(n.right)); /** Method C ??? */ public static int C(Node n) { if (n== null) return 0; return 1 + C(n.left) + C(n.right);

Some useful methods /** Return true iff node n is a leaf */ public static boolean isLeaf(Node n) { return n != null && n.left == null && n.right == null; } /** Return height of node n (postorder traversal) */ public static int height(Node n) { if (n== null) return -1; //empty tree return 1 + Math.max(height(n.left), height(n.right)); /** Return number of nodes in n (postorder traversal) */ public static int numNodes(Node n) { if (n== null) return 0; return 1 + numNodes(n.left) + numNodes(n.right);

Binary Search Tree (BST) If the tree data is ordered and has no duplicate values: in every subtree, All left descendents of a node come before the node All right descendents of a node come after the node Search can be made MUCH faster 2 3 7 9 5 8 < 5 > 5

Binary Search Tree (BST) If the tree data is ordered and has no duplicate values: in every subtree, All left descendents of a node come before the node All right descendents of a node come after the node Search can be made MUCH faster 2 3 7 9 5 8 Compare binary tree to binary search tree: boolean searchBT(n, v): if n==null, return false if n.v == v, return true return searchBST(n.left, v) || searchBST(n.right, v) boolean searchBST(n, v): if n==null, return false if n.v == v, return true if v < n.v return searchBST(n.left, v) else return searchBST(n.right, v) 2 recursive calls 1 recursive call

Building a BST To insert a new item Pretend to look for the item Put the new node in the place where you fall off the tree This can be done using either recursion or iteration Example Tree uses alphabetical order Months appear for insertion in calendar order jan feb mar apr jun may jul

What can go wrong? A BST makes searches very fast, unless… apr A BST makes searches very fast, unless… Nodes are inserted in increasing order In this case, we’re basically building a linked list (with some extra wasted space for the left fields, which aren’t being used) BST works great if data arrives in random order feb jan jul jun mar may

Printing contents of BST /** Print BST t in alpha order */ private static void print(TreeNode<T> t) { if (t== null) return; print(t.left); System.out.print(t.datum); print(t.right); } Because of ordering rules for a BST, it’s easy to print the items in alphabetical order Recursively print left subtree Print the node Recursively print right subtree show code

Tree traversals Other standard kinds of traversals preorder traversal Process root Process left subtree Process right subtree postorder traversal level-order traversal Not recursive: uses a queue (we’ll cover this later) “Walking” over the whole tree is a tree traversal Done often enough that there are standard names Previous example: in-order traversal Process left subtree Process root Process right subtree Note: Can do other processing besides printing

Useful facts about binary trees 5 4 7 8 2 depth 1 Height 2, minimum number of nodes maximum number of nodes Max # of nodes at depth d: 2d If height of tree is h min # of nodes: h + 1 max #of nodes in tree: 20 + … + 2h = 2h+1 – 1 Complete binary tree All levels of tree down to a certain depth are completely filled

Things to think about What if we want to delete data from a BST? A BST works great as long as it’s balanced How can we keep it balanced? This turns out to be hard enough to motivate us to create other kinds of trees jan feb mar apr jun may jul

Tree Summary A tree is a recursive data structure Each node has 0 or more successors (children) Each node except the root has exactly one predecessor (parent) All node are reachable from the root A node with no children (or empty children) is called a leaf Special case: binary tree Binary tree nodes have a left and a right child Either or both children can be empty (null) Trees are useful in many situations, including exposing the recursive structure of natural language and computer programs