1 / 11

Introduction to Trees and Tree Traversal Algorithms

This class overview covers the basics of trees, binary trees, general trees, and their traversal algorithms. It also includes examples and properties of trees.

rpatti
Download Presentation

Introduction to Trees and Tree Traversal Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Trees Neil Tang01/26/2010 CS223 Advanced Data Structures and Algorithms

  2. Class Overview • Basics • Binary Tree • Binary Tree Traversal • General Tree CS223 Advanced Data Structures and Algorithms

  3. Basics • Tree: A tree consists of a distinguished node r (root) and zero or more nonempty subtrees T1, T2, …Tk, each of whose roots are connected by a direct edge from r. • Child, parent, leaf, sibling • Path and path length • Depth of a node, depth of a tree • Height of a node, height of a tree • Ancestor, descendant CS223 Advanced Data Structures and Algorithms

  4. An Example CS223 Advanced Data Structures and Algorithms

  5. Properties • No cycle. • There only exists a unique path between a pair of nodes on a tree. • The height of a tree = the depth of that tree. CS223 Advanced Data Structures and Algorithms

  6. Binary Tree • Binary tree: A binary tree is a tree in which no node can have more than two children. • Implementation • The depth of a worst-case binary tree: N-1 CS223 Advanced Data Structures and Algorithms

  7. Binary Tree Traversal • Inorder-Tree-Traversal(t) if t  null then Inorder-Tree-Traversal(t.left) print(t.element) Inorder-Tree-Traversal(t.right) • Preorder-Tree-Traversal(t) if t  null then print(t.element) Preorder-Tree-Traversal(t.left) Preorder-Tree-Traversal(t.right) How about Postorder-Tree-Traversal? CS223 Advanced Data Structures and Algorithms

  8. An Example • Inorder: • x*a*b+c • Preorder: • *x+*abc • Postorder: • xab*c+* CS223 Advanced Data Structures and Algorithms

  9. General Tree • Implementation CS223 Advanced Data Structures and Algorithms

  10. General Tree Traversal • Preorder • What is the first node visted? Last? CS223 Advanced Data Structures and Algorithms

  11. General Tree Traversal • Postorder • What is the first node visited? Last? CS223 Advanced Data Structures and Algorithms

More Related