{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

06-trees

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

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

CS216: Program and Data Representation University of Virginia Computer Science Spring 2009 Aaron Bloomfield Trees

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

View Full Document
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
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.

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

View Full Document
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)
5 Complex Relationships Bill Cheswick’s Map of the Internet http://research.lumeta.com/ches/map/gallery/

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

View Full Document
Tree Terminology
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
14 Tree Preorder Traversal B A C J E K H D I F L G M
15 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

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

View Full Document
16 In-order Traversal void BST::print(BNode *curNode) { if(curNode != NULL) { print(curNode->left); cout << curNode->element; print(curNode->right); } } in-order left node first, then self then right node
Types of Trees

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 ]}