Download presentation

Presentation is loading. Please wait.

Published byStephan Furby Modified about 1 year ago

1
Chapter 10: Trees

2
Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.

3
Theorem 1. An undirected graph is a tree iff there is a unique simple path between any two vertices. 2. A simple graph G = (V, E) is a tree iff |E| = |V| - 1

4
Definition A rooted tree is a tree in which one vertex is specified as the root & every edge is drawn away from the root root

5
Definition root vertex parent children Level 0 Level 1 Level 2 Level 3 Height=3

6
Definition Root: a vertex with no parent Leaf: a vertex with no children Internal node: vertex with children Descendants: all children and children of children Ancestors: parent and parents of parents Siblings: vertices with the same parent Height = number of levels - 1

7
Definition An m-ary tree is a rooted tree where the number of children of any internal vertex m A full m-ary tree is an m-ary tree where the number of children of any internal vertex = m if m=2, we call it binary tree

8
Full 3-ary tree 3-ary tree

9
Definition In an ordered rooted tree the children are ordered. For example, in an ordered binary tree, a vertex may have left child and right child Left child Right child Left childRight child root

10
Properties Any tree of size n has n-1 edges Any full m-ary tree with i internal vertices has m i + 1 vertices Any m-ary tree with height h has at most m h leaves.

11
Properties A full m-ary tree with n vertices has i =(n-1)/m internal and l = ((m-1)n+1)/m leaves. Notice that n=l+i i internal has n=mi+1 vertices and l= (m-1)i+1 leaves l leaves has n=(ml -1)/(m-1) vertices and i =(l-1)/(m-1) internal

12
Section 10.3 Tree Traversal

13
Tree Traversal Away for visiting ordered rooted trees

14
Pre-order Visit x, T1 in preorder, T2 in preorder, T3 in preorder....and so on x T1 T4T2 T3

15
In-order Visit T1 in inorder, x, T2 in inorder, T3 in inorder....and so on x T1 T4T2 T3

16
Post-order Visit T1 in post-order, T2 in post-order, T3 in post-order.... etc, and then x. x T1 T4T2 T3

17
Example Pre-order: a, b, e, f, g, h, i, j, c, d In-order: e, b, f, h, g, i, j, a, c, d Post-order: e, f, h, i, j, g, b, c, d, a a b c d e f g h i j

18
Tree that represents expressions Any arithmetic expression can be represented by a rooted tree (x+1)^2-(3/2) x1 + 232 ^/ -

19
Ambiguous expressions x + y * z = (x + y) * z ? = x + (y * z) ? Parentheses have to be used to avoid ambiguity

20
Infix, prefix, & postfix Any arithmetic expression can be written in an unambiguous form Prefix form: an pre-order traversal of the expression’s rooted tree Infix form: an in-order traversal of the expression’s rooted tree Postfix form: an post-order traversal of the expression’s rooted tree

21
Example Consider the expression (x+1)^2-(3/2) Infix form: x+1^2-3/2 Prefix: -^+x12/32 Postfix: x1+2^32/- x1 + 232 ^/ -

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google