Trees Trees Set of elements or nodes one

  • Slides: 20
Download presentation
Trees

Trees

Trees • Set of elements or nodes – one node as root – every

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

Example height of tree = 3 root edge leaf leaf

Example height of tree = 3 root edge leaf leaf

Questions • What is the height of the tree? • What is the depth

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

Questions • What is the height of the tree? • What is the depth

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

Subtrees • The subtree rooted at x is the tree of all descendants of

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

Binary trees • A binary tree is a structure defined on a finite set

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

Left and right subtrees • In a binary tree – the left subtree of

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 x the tree rooted at the right child of x left subtree right subtree

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

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

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

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

Binary trees p q w x y z

Binary trees p q w x y z

Questions • In a complete binary tree of height 3, – how many nodes

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 are in the tree? – how many leaves are in the tree? • 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?

Complete binary tree of height 3 • 15 nodes • 8 leaves

Complete binary tree of height 3 • 15 nodes • 8 leaves

Complete binary tree of height 4 • 31 nodes • 16 leaves

Complete binary tree of height 4 • 31 nodes • 16 leaves

Binary trees binary tree In a binary tree of height h • the total

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

Decision trees • A decision tree begins with a decision you need to make

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

A decision tree yes Would you like to read about Einstein? He received the

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

A decision tree for insertion sort a b? sort three elements a, b, c

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

Insertion sort of 4 elements yes best case path a b? sort four elements

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

Decision-tree model • Decision trees are used to model comparison sorts – insertion sort,

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 = ½ (n 2 -n) • Length of longest path in decision tree for 4 elements = ½ (42 -4) = 6