CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
Published byModified over 6 years ago
Presentation on theme: "CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University."— Presentation transcript:
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University
Outline Trees (Chapter 4) Sorting Algorithms (Chapter 7)
Trees: Terminology A tree is a set of nodes and a set of directed edges. A rooted tree has the following properties: –One node is the root. –Every node, other than the root, is connected by an edge from exactly one other node. The first node is called a child and the second a parent. Every child has exactly a unique parent. –A path is a sequence of nodes such that every preceding node is the parent of its successor. –There is a unique path from the root to every node in the tree. –The length of the path is the number of edges in it.
Tree: Recursive Definition An empty set of nodes and edges is a tree. A tree consists of a root and zero or more non-empty sub-trees T 1, …, T n, each of whose roots receives an edge from the root.
Tree: Example A B CD EF H G A is the parent of B,C, D. A, B, F, H is a path. A, C, G is not a path.
Depth and Height There are two important numbers associated with every node in a tree: depth and height. The depth of a node N is the length of the path from the root to N. Depth goes from the root to the node. The height of a node N is the length of the longest path from N to a leaf. Height goes from the node to a leaf.
Height & Depth: Example 4 A B CD EF H G H=3;D=0 H=2;D=1H=0;D=1 H=1;D=1 H=0;D=2H=1;D=2 H=0;D=3 H=0;D=2
Binary Trees An empty tree is binary. If a binary tree is not empty, it has a unique node called root. Each node in a binary tree has exactly two sub-trees: the left sub-tree and the right sub-tree. The order of the sub-trees matters.
Binary Tree: Complete and Full A full binary tree is a tree where every node is either a leaf or has exactly two children. A complete binary tree of height h is a tree where every position is filled at every level from 0 to h.
Complete vs. Full … And they wandered in the wilderness until one of them uttered: Completeness is full, but fullness is incomplete. And they all became enlightened…
Binary Tree: Heights and Leaves For any binary tree with L leaves and height H:
A Decision Tree Each internal node represents a decision. Each leaf represents an outcome.
Decision Trees: Observations Different leaves may represent the same outcome because there may be several paths to the same outcome. The number of leaves should be at least as large as the number of outcomes. Number of decisions, in the worst case, is equal to the height of the decision tree for some algorithm.
A Decision Tree: Example a < b a < cb < c acbc YES NO YESNOYESNO Decision Tree for Finding a Minimum of Three Numbers.
Decision Trees for Comparison Sorting Algorithms Given an array A of N elements. Each outcome (leaf) is a sorted array. Each outcome is a permutation of N elements. We will call it P(A). There are N! possible outcomes. Each decision node is a comparison of two elements.