06-trees

06-trees - Spring 2009 CS216: Program and Data...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
CS216: Program and Data Representation University of Virginia Computer Science Spring 2009 Aaron Bloomfield Trees
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Trees: Main Points • Definitions and terminology • Five types of trees: – Binary trees – Binary search trees – AVL trees – Splay trees – Red-black trees • Tree traversals – pre-order – post-order – in-order
Background image of page 2
3 Data Structures • If we have a good list implementation, do we need any other data structures? • For computing: no – We can compute everything with just lists (actually even less). The underlying machine memory can be thought of as a list. • For thinking : yes – Lists are a very limited way of thinking about problems.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 List Limitations L Node Info: Next: 1 Node Info: Next: 2 Info: Next: 3 Node In a list, every element has direct relationships with only two things: predecessor and successor Access time: O(n) Goal: O(log n)
Background image of page 4
5 Complex Relationships Bill Cheswick’s Map of the Internet http://research.lumeta.com/ches/map/gallery/
Background image of page 5

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

View Full DocumentRight Arrow Icon
Tree Terminology
Background image of page 6
7 List Tree • List: each element has relationships with up to 2 other elements: • Binary Tree: each element has relationships with up to 3 other elements: Element Predecessor Successor Element Parent Right Child Left Child Tree is a special case of List
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Tree Terms Root : a node with no parent – There can only be one root Leaf : a node with no children Sibling : same parent Height of a Node: length of the longest path from that node to a leaf Height of a Tree: maximum depth of a node in that tree = height of the root Depth of a Node: length of the path from the Root to that node
Background image of page 8
9 Tree Terms English German Anglo-Saxon Norwegian Czech Spanish Italian Romanian French Leaf Root Note that CS trees are usually drawn upside down! Height = 3 Height = 0 Depth = 4 Sibling
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 More Tree Terms Path: sequence of nodes n 1 , n 2 , …, n k such that n i is parent of n i+1 for 1 i k Length: number of edges in the path Internal path length: sum of the depths of all nodes
Background image of page 10
11 Other Examples of Trees • Files/folders on a computer • Compilers: parse tree a = (b+c) * d; • Genealogy trees – Becomes complicated with some complex family relationships • Lab 5 – expression trees = c * b + d a
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 First child/next sibling class TreeNode { private: string element; TreeNode *firstChild; TreeNode *nextSibling; public: // … } home school aaron cs216 coll. h lab1 cs120 Mail proj1 lab2 coll.cp p proj.h pers
Background image of page 12
13 Tree Traversals TreeNode:: printTree (TreeNode tnode) { tnode.print(); for each child c of tnode c. printTree (); } int TreeNode:: numNodes (TreeNode tnode) { if(tnode==NULL) return 0; else { sum=0; for each child c of tnode sum += numNodes (c); return 1 + sum; } } pre-order node first, then children post-order children first, then node
Background image of page 13

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

View Full DocumentRight Arrow Icon
14 Tree Preorder Traversal B A C J E K H D I F L G M
Background image of page 14
Traversals of trees • Pre: / * + 1 2 – 3 4 * 5 6 • In: (1+2) * (3-4) / (5*6) • Post: 1 2 + 3 4 - * 5 6 * / * / * 3 - 4 1 + 2 5 6
Background image of page 15

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

View Full DocumentRight Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/11/2009 for the course CS 216 taught by Professor Bloomfield during the Spring '08 term at UVA.

Page1 / 97

06-trees - Spring 2009 CS216: Program and Data...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online