Trees Trees Set of elements or nodes one
- Slides: 20
Trees
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
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 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 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 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 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 of root
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
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 4 • 31 nodes • 16 leaves
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 – 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 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 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 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, 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
- Total set awareness set consideration set
- Training set validation set test set
- Act 1 scene 1 hamlet
- Set partitioning in hierarchical trees
- One god one empire one emperor
- One one one little dogs run
- One king one law one faith
- One empire one god one emperor
- One ford behaviors
- See one do one teach one
- One price policy
- Willow cabin speech
- Studiendekanat uni bonn
- One vision one identity one community
- Graphic organizer with the aims of la liga filipina
- Bounded set vs centered set
- Fuzzy logic
- Crisp set vs fuzzy set
- Crisp set vs fuzzy set
- What is the overlap of data set 1 and data set 2?
- Correspondence function examples