notes47

notes47 - Note: These notes cover the material on trees,...

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

View Full Document Right Arrow Icon
Note: These notes cover the material on trees, priority queues, and heaps discussed on Feb. 19, 21, and 23rd by lecturer Gustavo Rodriguez-Rivera. HTML and graphics by: Steven Maus (smaus@purdue.edu) Trees, Priority Queues, and Heaps! Trees - It is a data structure that reprensents a hierarchy. - When trees are well constructed [balance] lookup can take O(log(n)) - A is the root node - B is the parent of D and E nodes - C is the sibling of B - D and E are the children of B - D, E, F, and G are external nodes or leaves - A, B, and C are internal nodes - the depth or level of E is 2 (distance of E to the root) - the height of the tree is 3 - the degree of B (# of children) is 2 Binary Trees - It is a tree with all internal nodes of degree equal to 2. Example of a binary tree
Background image of page 1

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

View Full DocumentRight Arrow Icon
- A, B, C, and D are internal nodes and their degree is 2 Example of a tree that is not a binary tree - Not a binary tree because degree of B is 1 and B is an internal node (B needs to be degree 2). Properties of binary trees - # of external nodes = # internal nodes + 1 - # of external nodes <= 2 height - height >= log 2 ( # external nodes ) Traversal of a Tree Preorder Traversal visit parent before children preorder( Node v ) { mark v; //parent for each child ch of v call: preorder(ch); preorder(A); - visit: A, B, D, H, I, E, C, F, J, G, K
Background image of page 2
Postorder Traversal visit(mark) children before the parent postorder( Node v ) { for each child ch of v: postorder(v); mark v; } postorder(A) - visit: H, I, D, E, B, J, F, K, G, C, A
Background image of page 3

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

View Full DocumentRight Arrow Icon
Inorder Traversal visit left child, then parent, then right child inorder( Node v ) { inorder( v->_left ); // visit left child mark parent; inorder( v->_right ); //visit right child } inorder(A); visit: H, D, I, B, E, A, J, F, C, K, G
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

notes47 - Note: These notes cover the material on trees,...

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

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