Download presentation

Presentation is loading. Please wait.

1
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University

2
Outline Trees (Chapter 4) Sorting Algorithms (Chapter 7)

3
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.

4
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.

5
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.

6
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.

7
Height & Depth: Example 1 H = 0; D = 0

8
Height & Depth: Example 2 H=0; D=1 H=1; D=0

9
Height & Depth: Example 3 H=2; D=0 H=1; D=1 H=0;D=2

10
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

11
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.

12
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.

13
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…

14
Full Tree: Example A B C EF

15
Complete Tree: Example A B C DEFG

16
Binary Tree: Heights and Leaves For any binary tree with L leaves and height H:

17
A Decision Tree Each internal node represents a decision. Each leaf represents an outcome.

18
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.

19
A Decision Tree: Example a < b a < cb < c acbc YES NO YESNOYESNO Decision Tree for Finding a Minimum of Three Numbers.

20
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.

Similar presentations

© 2021 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google