 # Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.

## Presentation on theme: "Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest."— Presentation transcript:

Chapter 10: Trees

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

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

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

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

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

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

Full 3-ary tree 3-ary tree

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

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.

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

Section 10.3 Tree Traversal

Tree Traversal Away for visiting ordered rooted trees

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

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

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

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

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

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

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

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 ^/ -

Download ppt "Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest."

Similar presentations