Trees Lecture 10 CS2110 – Spring 2013.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
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.
Computer Science C++ High School Level By Guillermo Moreno.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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).
ABSTRACT DATA TYPES; LISTS & TREES Lecture 10 CS2110 – Fall 2014.
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.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
CSE 373 Data Structures Lecture 7
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Tree.
Data Structures and Design in Java © Rick Mercer
Recursive Objects (Part 4)
Trees Lecture 12 CS2110 – Spring 2017.
Week 6 - Wednesday CS221.
Trees Lecture 12 CS2110 – Fall 2017.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees Lecture 12 CS2110 – Fall 2016.
Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Trees Lecture 12 CS2110 – Spring 2018.
TREES General trees Binary trees Binary search trees AVL trees
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Trees Lecture 12 CS2110 – Fall 2018.
Binary Trees, Binary Search Trees
Non-Linear Structures
Trees.
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Java Trees.
Trees.
Trees.
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees Lecture 10 CS2110 – Spring 2013

Tree Overview Tree: recursive data structure (similar to list) 5 4 7 8 9 2 General tree 5 4 7 8 2 Binary tree Tree: recursive data structure (similar to list) Each cell may have zero or more successors (children) Each cell has exactly one predecessor (parent) except the root, which has none All cells are reachable from root Binary tree: tree in which each cell 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

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

Class for Binary Tree Cells Points to left subtree class TreeCell<T> { private T datum; private TreeCell<T> left, right; public TreeCell(T x) { datum = x; } public TreeCell(T x, TreeCell<T> lft, TreeCell<T> rgt) { datum = x; left = lft; right = rgt; } more methods: getDatum, setDatum, getLeft, setLeft, getRight, setRight Points to right subtree ... new TreeCell<String>("hello") ...

Binary versus general tree In a binary tree each node has exactly two pointers: to the left subtree, and to the right one Of course one or both could be null In a general tree a node can have any number of child nodes Very useful in some situations... ... one of which will be our assignments!

Class for General Tree nodes 5 class GTreeCell { private Object datum; private GTreeCell left; private GTreeCell sibling; appropriate getter and setter methods } 4 2 General tree 7 8 9 7 8 3 1 5 Parent node points directly only to its leftmost child Leftmost child has pointer to next sibling, which points to next sibling, etc. 4 2 Tree represented using GTreeCell 7 8 9 7 8 3 1

Applications of 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

Example Expression grammar: In textual representation E → integer E → (E + E) In textual representation Parentheses show hierarchical structure In tree representation Hierarchy is explicit in the structure of the tree Text AST Representation -34 -34 (2 + 3) + 2 3 ((2+3) + (5+7)) + + + 2 3 5 7

Recursion on Trees Recursive methods can be written to operate on trees in an obvious way Base case empty tree leaf node Recursive case solve problem on left and right subtrees put solutions together to get solution for full tree

Searching in a Binary Tree public static boolean treeSearch(Object x, TreeCell node) { if (node == null) return false; if (node.datum.equals(x)) return true; return treeSearch(x, node.left) || treeSearch(x, node.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

Binary Search Tree (BST) 2 3 7 9 5 8 If the tree data are ordered – in any subtree, All left descendents of node come before node All right descendents of node come after node This makes it much faster to search public static boolean treeSearch (Object x, TreeCell node) { if (node == null) return false; if (node.datum.equals(x)) return true; if (node.datum.compareTo(x) > 0) return treeSearch(x, node.left); else return treeSearch(x, node.right); }

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 feb A BST makes searches very fast, unless… Nodes are inserted in alphabetical order In this case, we’re basically building a linked list (with some extra wasted space for the left fields that aren’t being used) BST works great if data arrives in random order jan jul jun mar may

Printing Contents of BST Because of the ordering rules for a BST, it’s easy to print the items in alphabetical order Recursively print everything in the left subtree Print the node Recursively print everything in the right subtree /** * Show the contents of the BST in * alphabetical order. */ public void show () { show(root); System.out.println(); } private static void show(TreeNode node) { if (node == null) return; show(node.lchild); System.out.print(node.datum + " "); show(node.rchild);

Tree Traversals “Walking” over the whole tree is a tree traversal This is done often enough that there are standard names The previous example is an inorder traversal Process left subtree Process node Process right subtree Note: we’re using this for printing, but any kind of processing can be done There are other standard kinds of traversals Preorder traversal Process node Process left subtree Process right subtree Postorder traversal Level-order traversal Not recursive Uses a queue

Some Useful Methods //determine if a node is a leaf public static boolean isLeaf(TreeCell node) { return (node != null) && (node.left == null) && (node.right == null); } //compute height of tree using postorder traversal public static int height(TreeCell node) { if (node == null) return -1; //empty tree if (isLeaf(node)) return 0; return 1 + Math.max(height(node.left), height(node.right)); //compute number of nodes using postorder traversal public static int nNodes(TreeCell node) { if (node == null) return 0; return 1 + nNodes(node.left) + nNodes(node.right);

Useful Facts about Binary Trees 5 4 7 8 2 depth 1 Height 2, minimum number of nodes maximum number of nodes 2d = maximum number of nodes at depth d If height of tree is h Minimum number of nodes in tree = h + 1 Maximum number of nodes in tree = 20 + 21 + … + 2h = 2h+1 – 1 Complete binary tree All levels of tree down to a certain depth are completely filled

Tree with Parent Pointers In some applications, it is useful to have trees in which nodes can reference their parents Analog of doubly-linked lists 5 4 2 7 8

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? jan feb mar apr jun may jul

Suffix Trees Given a string s, a suffix tree for s is a tree such that each edge has a unique label, which is a nonnull substring of s any two edges out of the same node have labels beginning with different characters the labels along any path from the root to a leaf concatenate together to give a suffix of s all suffixes are represented by some path the leaf of the path is labeled with the index of the first character of the suffix in s Suffix trees can be constructed in linear time

Suffix Trees abracadabra$ a cadabra$ bra ra dabra$ $ dabra$ cadabra$ $

Suffix Trees Useful in string matching algorithms (e.g., longest common substring of 2 strings) Most algorithms linear time Used in genomics (human genome is ~4GB)

Huffman Trees Fixed length encoding 197*2 + 63*2 + 40*2 + 26*2 = 652 1 1 1 1 1 e e t a s 1 t 197 63 40 26 a s Fixed length encoding 197*2 + 63*2 + 40*2 + 26*2 = 652 Huffman encoding 197*1 + 63*2 + 40*3 + 26*3 = 521

Huffman Compression of “Ulysses” ' ' 242125 00100000 3 110 'e' 139496 01100101 3 000 't' 95660 01110100 4 1010 'a' 89651 01100001 4 1000 'o' 88884 01101111 4 0111 'n' 78465 01101110 4 0101 'i' 76505 01101001 4 0100 's' 73186 01110011 4 0011 'h' 68625 01101000 5 11111 'r' 68320 01110010 5 11110 'l' 52657 01101100 5 10111 'u' 32942 01110101 6 111011 'g' 26201 01100111 6 101101 'f' 25248 01100110 6 101100 '.' 21361 00101110 6 011010 'p' 20661 01110000 6 011001 ... '7' 68 00110111 15 111010101001111 '/' 58 00101111 15 111010101001110 'X' 19 01011000 16 0110000000100011 '&' 3 00100110 18 011000000010001010 '%' 3 00100101 19 0110000000100010111 '+' 2 00101011 19 0110000000100010110 original size 11904320 compressed size 6822151 42.7% compression 24

BSP Trees BSP = Binary Space Partition Used to render 3D images composed of polygons Each node n has one polygon p as data Left subtree of n contains all polygons on one side of p Right subtree of n contains all polygons on the other side of p Order of traversal determines occlusion!

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