# c36204 - 2 Trees 2 Trees 2.1 Preliminaries 2.2 Binary trees...

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

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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.

Unformatted text preview: 2. Trees 2. Trees 2.1 Preliminaries 2.2 Binary trees 2.3 Binary search trees 2.4 AVL and Splay trees 2.5 B-trees CS340 Fall 2010 1 2.1 Preliminaries 2.1 Preliminaries A B C D E F G H I J K L M N P Q Root Leaves Height=3 CS340 Fall 2010 2 2.1 Preliminaries Terms • Child • Parent • Sibling : nodes that share a common parent. • Leaf : a node with no children. • Node depth : the number of links on the path from the root to the node. • Tree height : the depth of the deepest node. CS340 Fall 2010 3 2.1 Preliminaries Recursive view A tree is either : • Empty • Contains a root and N subtrees (N ≥ 0). CS340 Fall 2010 4 2.1 Preliminaries Implementation of Trees • The tree is a collection of nodes : struct TreeNode { Object element; TreeNode * firstChild; TreeNode * nextSibling; } ; • The tree stores a reference to the root node , which is the starting point. CS340 Fall 2010 5 2.1 Preliminaries Implementation of Trees A B C D E F G H I J K L M N P Q Root Height=3 CS340 Fall 2010 6 2.1 Preliminaries Tree Traversals // List a directory in a hierarchical file system void FileSystem::listAll(int depth = 0) const { printName( depth ); if (isDirectory()) for each file c in this directory(for each child) c.listAll(depth + 1); } ; // Calculate the size of a directory void FileSystem::size( ) const { int totalSize = sizeOfThisFile(); if (isDirectory()) for each file c in this directory(for each child) totalSize += c.size(); return totalSize; } ; CS340 Fall 2010 7 2.2 Binary Trees 2.2 Binary Trees Recursive view A binary tree is either : • Empty • Contains a root and N binary subtrees (0 ≤ N ≤ 2). CS340 Fall 2010 8 2.2 Binary Trees Implementation struct BinaryNode { Object element; // the data in the node BinaryNode * left; // left child BinaryNode * right; // right child } ; CS340 Fall 2010 9 2.2 Binary Trees Expression trees • Inorder traversal ⇒ infix notation. • postorder traversal ⇒ postfix notation. • preorder traversal ⇒ prefix notation. + + a * b c * + * d e f g CS340 Fall 2010 10 2.2 Binary Trees Constructing an expression tree Converting a postfix expression into an expression tree : • Same principle as evaluating an expression in postfix notation. • Use a stack of pointers. For infix expression : Step 1 : Use the algorithm seen in chapter 3 to convert the expression in postfix notation. Step 2 : Use the algorithm above to produce the corresponding expression tree. CS340 Fall 2010 11 2.3 Binary Search Trees 2.3 Binary Search Trees Goals : • Binary search supports Find in O (log N ) worst-case time, but Insert and Remove are O ( N ) . • Would like to support all three operations in O (log N ) worst-case time. • Today’s result : can support all three operations in O (log N ) average-case time....
View Full Document

{[ snackBarMessage ]}

### Page1 / 66

c36204 - 2 Trees 2 Trees 2.1 Preliminaries 2.2 Binary trees...

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

View Full Document
Ask a homework question - tutors are online