trees cosc 450 programming paradigms 08 the definition of
play

Trees CoSc 450: Programming Paradigms 08 The definition of a tree - PowerPoint PPT Presentation

CoSc 450: Programming Paradigms 08 Trees CoSc 450: Programming Paradigms 08 The definition of a tree CoSc 450: Programming Paradigms 08 The definition of a tree The empty tree is a tree. A nonempty tree tree has three parts. root


  1. CoSc 450: Programming Paradigms 08 Trees

  2. CoSc 450: Programming Paradigms 08 The definition of a tree

  3. CoSc 450: Programming Paradigms 08 The definition of a tree • The empty tree is a tree. • A nonempty tree tree has three parts. • root — an element. • left-subtree — a tree. • right-subtree — a tree.

  4. CoSc 450: Programming Paradigms 08 my-tree (define my-tree '(4 (2 (1 () ()) (3 () ())) (6 (5 () ()) (7 () ()))))

  5. CoSc 450: Programming Paradigms 08 my-tree (define my-tree '(4 (2 (1 () ()) (3 () ())) (6 (5 () ()) (7 () ()))))

  6. CoSc 450: Programming Paradigms 08 my-tree (define my-tree '(4 (2 (1 () ()) (3 () ())) (6 (5 () ()) (7 () ())))) 4 2 6 1 3 5 7

  7. CoSc 450: Programming Paradigms 08 The definition of a binary search tree (BST)

  8. CoSc 450: Programming Paradigms 08 The definition of a binary search tree (BST) • Every element in the left subtree is less than the root. • Every element in the right subtree is greater than the root. • The left subtree is a BST. • The right subtree is a BST.

  9. CoSc 450: Programming Paradigms 08 Preorder traversal Returns a list

  10. CoSc 450: Programming Paradigms 08 Preorder traversal Returns a list If the tree is not empty • Visit the root. • Do a preorder traversal of the left subtree. • Do a preorder traversal of the right subtree.

  11. CoSc 450: Programming Paradigms 08 Preorder traversal Returns a list 4 2 6 1 3 5 7 What is the preorder traversal?

  12. CoSc 450: Programming Paradigms 08 Preorder traversal Returns a list 4 2 6 1 3 5 7 (4 2 1 3 6 5 7)

  13. CoSc 450: Programming Paradigms 07 (preorder-onto ‘(a b c)) 4 2 6 1 3 5 7

  14. CoSc 450: Programming Paradigms 07 (preorder-onto ‘(a b c)) 4 2 6 1 3 5 7

  15. CoSc 450: Programming Paradigms 07 (preorder-onto ‘(a b c)) 4 2 6 1 3 5 7 (preorder-onto ‘(a b c)) 6 5 7

  16. CoSc 450: Programming Paradigms 07 (preorder-onto ‘(a b c)) 4 2 6 1 3 5 7 (preorder-onto ‘(a b c)) 6 5 7 (6 5 7 a b c)

  17. CoSc 450: Programming Paradigms 07 (preorder-onto ‘(a b c)) 4 2 6 1 3 5 7

  18. CoSc 450: Programming Paradigms 07 (preorder-onto ‘(a b c)) 4 2 6 1 3 5 7 (preorder-onto ‘(6 5 7 a b c)) 2 1 3

  19. CoSc 450: Programming Paradigms 07 (preorder-onto ‘(a b c)) 4 2 6 1 3 5 7 (preorder-onto ‘(6 5 7 a b c)) 2 1 3 (2 1 3 6 5 7 a b c)

  20. CoSc 450: Programming Paradigms 07 (preorder-onto ‘(a b c)) 4 2 6 1 3 5 7 (preorder-onto ‘(6 5 7 a b c)) 2 1 3 (2 1 3 6 5 7 a b c) 4

  21. CoSc 450: Programming Paradigms 08 Inorder traversal Returns a list If the tree is not empty • Do an inorder traversal of the left subtree. • Visit the root. • Do an inorder traversal of the right subtree.

  22. CoSc 450: Programming Paradigms 08 The definition of an expression tree • A number is an expression tree. • A non-number tree has three parts. • A left operand — an expression tree. • An operator name. • A right operand — an expression tree.

  23. CoSc 450: Programming Paradigms 08 my-expression (define my-expression '(1 + (2 * (3 - 5)))) + ∗ 1 – 2 3 5

Recommend


More recommend