Computer Science 112 - PowerPoint PPT Presentation

doctor
computer science 112 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Computer Science 112 PowerPoint Presentation
Download Presentation
Computer Science 112

play fullscreen
1 / 38
Download Presentation
Computer Science 112
48 Views
Download Presentation

Computer Science 112

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Computer Science 112 Fundamentals of Programming II Introduction to Trees

  2. Each item has at most one predecessor Each item can have many successors What Is a Tree?

  3. Start with a single node, called the root What Is a Tree? Root node

  4. Start with a single node, called the root Eachsuccessor node is a child or daughter node What Is a Tree? Root node Daughters of root node

  5. Successors are also called descendants What Is a Tree? Root node Descendants of root node

  6. Nodes without successors are called leaf nodes The set of leaf nodes is the frontier What Is a Tree? Root node Leaf nodes (the frontier)

  7. Nodes with at least one successor are called interior nodes What Is a Tree? Root node Interior nodes

  8. Predecessors are also called ancestors The immediate predecessor is called the parent What Is a Tree? Root node X Ancestors of node X

  9. Nodes with the same parent are called siblings What Is a Tree? Root node Siblings

  10. Levels in a tree are numbered from 0 What Is a Tree? Root node Level 0 Level 1 Level 2 Level 3 There are three nodes in level 3

  11. A binary tree allows at most two successors per node What Is a Tree? Root node

  12. A general tree allows any number of successors per node What Is a Tree? Root node

  13. Trees as Recursive Data Structures • A tree is either • empty, or • consists of a node containing • a datum • one or more subtrees Each subtree is itself another tree

  14. Tree Traversals • We’d like to visit each data item in a tree • Are the items randomly ordered, as in a bag or set? • Think of visiting the data in a node, and its left and right subtrees, in some order

  15. Preorder Traversal D Order of nodes visited: D B F G A E C Visit the data Visit the left subtree Visit the right subtree

  16. Preorder Traversal D Order of nodes visited: D B B F G A E C Visit the data Visit the left subtree Visit the right subtree

  17. Preorder Traversal D Order of nodes visited: D B A B F G A E C Visit the data Visit the left subtree Visit the right subtree

  18. Preorder Traversal D Order of nodes visited: D B A C B F G A E C Visit the data Visit the left subtree Visit the right subtree

  19. Preorder Traversal D Order of nodes visited: D B A C F B F G A E C Visit the data Visit the left subtree Visit the right subtree

  20. Preorder Traversal D Order of nodes visited: D B A C F E B F G A E C Visit the data Visit the left subtree Visit the right subtree

  21. Preorder Traversal D Order of nodes visited: D B A C F E G B F G A E C Visit the data Visit the left subtree Visit the right subtree

  22. Inorder Traversal D Order of nodes visited: A B F G A E C Visit the left subtree Visit the data Visit the right subtree

  23. Inorder Traversal D Order of nodes visited: A B B F G A E C Visit the left subtree Visit the data Visit the right subtree

  24. Inorder Traversal D Order of nodes visited: A B C B F G A E C Visit the left subtree Visit the data Visit the right subtree

  25. Inorder Traversal D Order of nodes visited: A B C D B F G A E C Visit the left subtree Visit the data Visit the right subtree

  26. Inorder Traversal D Order of nodes visited: A B C D E B F G A E C Visit the left subtree Visit the data Visit the right subtree

  27. Inorder Traversal D Order of nodes visited: A B C D E F B F G A E C Visit the left subtree Visit the data Visit the right subtree

  28. Inorder Traversal D Order of nodes visited: A B C D E F G B F G A E C Visit the left subtree Visit the data Visit the right subtree

  29. Postorder Traversal D Order of nodes visited: A B F G A E C Visit the left subtree Visit the right subtree Visit the data

  30. Postorder Traversal D Order of nodes visited: A C B F G A E C Visit the left subtree Visit the right subtree Visit the data

  31. Postorder Traversal D Order of nodes visited: A C B B F G A E C Visit the left subtree Visit the right subtree Visit the data

  32. Postorder Traversal D Order of nodes visited: A C B E B F G A E C Visit the left subtree Visit the right subtree Visit the data

  33. Postorder Traversal D Order of nodes visited: A C B E G B F G A E C Visit the left subtree Visit the right subtree Visit the data

  34. Postorder Traversal D Order of nodes visited: A C B E G F B F G A E C Visit the left subtree Visit the right subtree Visit the data

  35. Postorder Traversal D Order of nodes visited: A C B E G F D B F G A E C Visit the left subtree Visit the right subtree Visit the data

  36. Level Order Traversal D Order of nodes visited: D B F A C E G B F G A E C For each level Visit data from left to right

  37. Tree Applications • File directories • Processing sentences (computer programs or natural languages) • Searchable data structures • Heaps (implement heap sort, priority queues)

  38. For Wednesday Binary Search Trees