Trees

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

4/1/2003 9:01 AM Trees 1 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery 4/1/2003 9:01 AM Trees 2 Outline and Reading Tree ADT (§6.1) Preorder and postorder traversals (§6.2.3) BinaryTree ADT (§6.3.1) Inorder traversal (§6.3.4) Euler Tour traversal (§6.3.4) Template method pattern (§6.3.5) Data structures for trees (§6.4) C++ implementation (§6.4.2) 4/1/2003 9:01 AM Trees 3 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation Applications: ± Organization charts ± File systems ± Programming environments Computers”R”Us Sales R&D Manufacturing Laptops Desktops US International Europe Asia Canada 4/1/2003 9:01 AM Trees 4 subtree Tree Terminology Root: node without parent (A) Internal node: node with at least one child (A, B, C, F) External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Descendant of a node: child, grandchild, grand-grandchild, etc. A B D C G H E F I J K Subtree: tree consisting of a node and its descendants 4/1/2003 9:01 AM Trees 5 Tree ADT We use positions to abstract nodes Generic methods: ± integer size () ± boolean isEmpty () ± objectIterator elements () ± positionIterator positions () Accessor methods: ± position root () ± position parent (p) ± positionIterator children (p) Query methods: ± boolean isInternal (p) ± boolean isExternal (p) ± boolean isRoot (p) Update methods: ± swapElements (p, q) ± object replaceElement (p, o) Additional update methods may be defined by data structures implementing the Tree ADT 4/1/2003 9:01 AM Trees 6 Preorder Traversal A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document Make Money Fast!

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