{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L09cs2110fa09-6up

# L09cs2110fa09-6up - Tree Overview 2 Tree recursive data...

This preview shows pages 1–2. Sign up to view the full content.

9/24/2009 1 TREES Lecture 9 CS2110 – Fall 2009 Tree Overview 2 Tree : recursive data structure (similar to list) Each cell may have zero or more successors (children) Each cell has exactly one predecessor (parent) except 5 4 7 8 9 2 5 4 7 8 2 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 5 6 8 List-like tree Tree Terminology 3 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 G W 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 ...? P J D N H B S Class for Binary Tree Cells 4 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") ... Class for General Trees 5 class GTreeCell { private Object datum ; private GTreeCell left ; private GTreeCell sibling ; appropriate getter and 5 4 7 8 9 2 7 8 3 1 General tree setter methods } 5 4 7 8 9 2 7 8 3 1 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. Applications of Trees 6 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.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern