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

trees cosc 450 programming paradigms 08 the definition of
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CoSc 450: Programming Paradigms

Trees

08

slide-2
SLIDE 2

CoSc 450: Programming Paradigms The definition of a tree 08

slide-3
SLIDE 3

CoSc 450: Programming Paradigms

  • The empty tree is a tree.
  • A nonempty tree tree has three parts.
  • root — an element.
  • left-subtree — a tree.
  • right-subtree — a tree.

08 The definition of a tree

slide-4
SLIDE 4

CoSc 450: Programming Paradigms 08

my-tree

(define my-tree '(4 (2 (1 () ()) (3 () ())) (6 (5 () ()) (7 () ()))))

slide-5
SLIDE 5

CoSc 450: Programming Paradigms 08

my-tree

(define my-tree '(4 (2 (1 () ()) (3 () ())) (6 (5 () ()) (7 () ()))))

slide-6
SLIDE 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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

CoSc 450: Programming Paradigms

  • 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.

08 The definition of a binary search tree (BST)

slide-9
SLIDE 9

CoSc 450: Programming Paradigms 08 Preorder traversal Returns a list

slide-10
SLIDE 10

CoSc 450: Programming Paradigms 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.

08 Preorder traversal Returns a list

slide-11
SLIDE 11

CoSc 450: Programming Paradigms 08 Preorder traversal Returns a list

4 2 6 1 3 5 7

What is the preorder traversal?

slide-12
SLIDE 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)

slide-13
SLIDE 13

CoSc 450: Programming Paradigms 07

(preorder-onto ‘(a b c))

4 2 6 1 3 5 7

slide-14
SLIDE 14

CoSc 450: Programming Paradigms 07

(preorder-onto ‘(a b c))

4 2 6 1 3 5 7

slide-15
SLIDE 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

slide-16
SLIDE 16

CoSc 450: Programming Paradigms

(6 5 7 a b c)

07

(preorder-onto ‘(a b c))

4 2 6 1 3 5 7

(preorder-onto ‘(a b c))

6 5 7

slide-17
SLIDE 17

CoSc 450: Programming Paradigms 07

(preorder-onto ‘(a b c))

4 2 6 1 3 5 7

slide-18
SLIDE 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

slide-19
SLIDE 19

CoSc 450: Programming Paradigms

(2 1 3 6 5 7 a b c)

07

(preorder-onto ‘(a b c))

4 2 6 1 3 5 7

(preorder-onto ‘(6 5 7 a b c))

2 1 3

slide-20
SLIDE 20

CoSc 450: Programming Paradigms

(2 1 3 6 5 7 a b c)

07

(preorder-onto ‘(a b c))

4 2 6 1 3 5 7

(preorder-onto ‘(6 5 7 a b c))

2 1 3

4

slide-21
SLIDE 21

CoSc 450: Programming Paradigms 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.

08 Inorder traversal Returns a list

slide-22
SLIDE 22

CoSc 450: Programming Paradigms

  • 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.

08 The definition of an expression tree

slide-23
SLIDE 23

CoSc 450: Programming Paradigms 08

my-expression

(define my-expression '(1 + (2 * (3 - 5))))

– 3 5 + ∗ 2 1