Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees.

Similar presentations


Presentation on theme: "Trees."— Presentation transcript:

1 Trees

2 Trees Set of elements or nodes one node as root
every other node has a single predecessor (parent) and zero or more successors (children) nodes without successors are called leaves Nodes are connected by edges The depth of a node is the number of edges on the path to the root The height of the tree is the number of edges on the longest path from root to any leaf A complete tree has all leaves at same depth, all other nodes have the same number of children

3 Example leaf leaf leaf leaf leaf leaf leaf leaf root
height of tree = 3  edge leaf leaf leaf leaf leaf leaf leaf leaf

4 Questions What is the height of the tree? What is the depth of node n?
What is the depth of node p? Is the tree complete? n p

5 Questions What is the height of the tree? What is the depth of node n?
What is the depth of node p? What is the depth of node q? p q n

6 Subtrees The subtree rooted at x is the tree of all descendants of x that has node x as root q p subtree rooted at q subtree rooted at p

7 Binary trees A binary tree is a structure defined on a finite set of nodes that either contains no nodes is composed of three disjoint sets of nodes a root node a binary tree called its left subtree a binary tree called its right subtree

8 Left and right subtrees
In a binary tree the left subtree of x is the tree rooted at the left child of x the right subtree of x is the tree rooted at the right child of x x left subtree right subtree

9 Binary trees p q w x y z left subtree of root right subtree of root

10 Binary trees p q w x y z subtree rooted at p subtree rooted at q
left subtree of p right subtree of p left subtree of q right subtree of q subtree rooted at p subtree rooted at q

11 Binary trees p q w x y z

12 Questions In a complete binary tree of height 3,
how many nodes are in the tree? how many leaves are in the tree? In a complete binary tree of height 4, How many nodes in a complete binary tree of height h? How many leaves in a complete binary tree of height h? How many nodes in a binary tree of height h? How many leaves in a binary tree of height h?

13 Complete binary tree of height 3
15 nodes 8 leaves

14 Complete binary tree of height 4
31 nodes 16 leaves

15 Binary trees In a binary tree of height h
the total number of nodes is at most …+2h = 2h+1 –1 the number of leaf nodes is at most 2h If the tree is complete there are exactly 2h+1 – 1 nodes there are exactly 2h leaves binary tree complete binary tree

16 Decision trees A decision tree begins with a decision you need to make
start with an initial decision node ask a question Structure for investigating options and the possible outcomes of choosing those options result of a decision can be another decision outcome is a terminal node Tree should have unique paths from the decision node to each of the terminal nodes. help you to choose between several courses of action

17 A decision tree yes yes no yes no no He received the
Physics Prize in 1921. yes Would you like to read about Einstein? yes Try the Medicine Prize in 1962. no Would you like to read about a scientist? Look up the Peace Prize in 1991. yes no Would you prefer a humanitarian? Try the Prize in Literature, 1970. no

18 A decision tree for insertion sort
a  b? sort three elements a,b,c yes no b  c? a  c? yes no yes no a  c? b  c? a  b  c b  a  c yes no yes no a  c  b c  a  b b  c  a c  b  a

19 Insertion sort of 4 elements
a  b? sort four elements a,b,c,d best case path yes no b  c? a  c? yes no c  d? b  c? worst case path no a  d? a  b  c  d no b  d? no c  d? d  c  b  a

20 Decision-tree model Decision trees are used to model comparison sorts
insertion sort, mergesort, quicksort Each possible permutation of n elements must appear as a leaf in the tree at least n! leaves Worst-case number of comparisons = length of longest path from root to leaf Insertion sort worst case: number of comparisons: 0+1+ … + n-1 = ½ (n2-n) Length of longest path in decision tree for 4 elements = ½ (42-4) = 6


Download ppt "Trees."

Similar presentations


Ads by Google