Docsity

Lecture 9: Trees in CS2110 - Fall 2008, Study notes of Computer Science

Toggle sidebar
bg1
Trees
Lecture 9
CS2110 – Fall 2008
2
Announcements
!A3 will be up shortly – check the website
!A2 is graded
"Submit regrades online
"Regrades accepted until 10/3
!Please include Cornell netId in email correspondence
"e.g., [email protected] does not help us
3
Tree Overview
!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
78 9
2
General tree
5
4
78
2
Binary tree
5
4
7 8
Not a tree
5
6
8
List-like tree
4
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
PJD
NHB S
5
Class for Binary Tree Cells
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
}
... new TreeCell<String>("hello") ...
6
Class for General Trees
class GTreeCell {
private Object datum;
private GTreeCell left;
private GTreeCell sibling;
appropriate getter and
setter methods
}
5
4
78 9
2
78 3 1
5
4
78 9
2
78 3 1
General
tree
Tree
represented
using
GTreeCell
!Parent node points directly
only to its leftmost child
!Leftmost child has pointer
to next sibling, which points
to next sibling, etc.
pf3
pf4
pf5

Partial preview of the text

Download Lecture 9: Trees in CS2110 - Fall 2008 and more Study notes Computer Science in PDF only on Docsity!

Trees

Lecture 9

CS2110 – Fall 2008

2

Announcements

! A3 will be up shortly – check the website

! A2 is graded

" Submit regrades online " Regrades accepted until 10/

! Please include Cornell netId in email correspondence

" e.g., [email protected] does not help us 3

Tree Overview

! 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

General tree

Binary tree 5 4 7 8 Not a tree

List-like tree 4

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

class TreeCell {

private T datum;

private TreeCell left, right;

public TreeCell(T x) { datum = x; }

public TreeCell(T x, TreeCell lft,

TreeCell rgt) {

datum = x;

left = lft;

right = rgt;

more methods: getDatum, setDatum,

getLeft, setLeft, getRight, setRight

... new TreeCell("hello") ...

Class for General Trees

class GTreeCell { private Object datum; private GTreeCell left; private GTreeCell sibling; appropriate getter and setter methods }

General tree Tree represented using GTreeCell

! Parent node points directly

only to its leftmost child

! Leftmost child has pointer

to next sibling, which points

to next sibling, etc.

7

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

8

Example

! Expression grammar:

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 9

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

10

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); } 9 8 3 5 7

! 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

Binary Search Tree (BST)

! 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

19

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

20

• 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

21

Suffix Trees

a cadabra$

abracadabra$

bra ra cadabra$ dabra$ $ cadabra$ dabra$ cadabra$ (^) cadabra$ $

$ bra

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)

22

Huffman Trees

23

s e a e t a s t 197 63 40 26

Fixed length encoding

Huffman encoding

Huffman Compression of “Ulysses”

' 'e' ' (^242125139496 0010000001100101 33 ) 't' 'a' (^9566089651 0111010001100001 44 ) 'o' 'n' (^8888478465 0110111101101110 44 ) 'i' 's' (^7650573186 0110100101110011 44 ) 'h' 'r' (^6862568320 0110100001110010 55 ) 'l' 'u' (^5265732942 0110110001110101 56 ) 'g' 'f' (^2620125248 0110011101100110 66 ) '.' 'p' (^2136120661 0010111001110000 66 ) ... '7' '/' (^6858 0011011100101111 1515 ) 'X' '&' (^193 0101100000100110 1618 ) '%' '+' (^32 0010010100101011 1919 ) original compressed size size (^119043206822151) 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