Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13: Tree Traversals Algorithms on Trees.

Similar presentations


Presentation on theme: "Lecture 13: Tree Traversals Algorithms on Trees."— Presentation transcript:

1 Lecture 13: Tree Traversals Algorithms on Trees

2 Edge-List Representation of a Tree

3 Types of Tree Traversal

4 Depth-First Traversal of a Tree

5 Breadth-First Traversal of a Tree

6 Introduction (1) Search Trees (2) Decision Trees (3) Huffman Coding

7 Binary Search Trees

8 Building a Binary Search Sort Tree

9 Locating and Adding Items to a Binary Search Tree

10 Making a Binary Tree Full

11 Find the Counterfeit Coin
7 of the 8 coins weigh the same. The counterfeit is lighter. Find it in two weighings on a balance scale.

12 Decision Tree

13 Huffman Coding

14 Huffman Coding for 5 Symbols

15

16 Tic-Tac-Toe

17 Partial Game Tree for Tic-Tac-Toe

18 The Min-Max Game Strategy

19 Min-Max Game Strategy Values for NIM
alpha-beta pruning

20 Spanning Tree

21

22 Web Spiders

23 Web Spider: Screen Scrape
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace screenscrape { class Program static void Main(string[] args) WebClient webClient = new WebClient(); const string strUrl = " byte[] reqHTML; reqHTML = webClient.DownloadData(strUrl); UTF8Encoding objUTF8 = new UTF8Encoding(); string html = objUTF8.GetString(reqHTML); Console.WriteLine(objUTF8.GetString(reqHTML)); Console.ReadKey(); }

24 Web Spider: Screen Scrape
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace screenscrape { class Program static void Main(string[] args) WebClient webClient = new WebClient(); const string strUrl = " byte[] reqHTML; reqHTML = webClient.DownloadData(strUrl); UTF8Encoding objUTF8 = new UTF8Encoding(); string html = objUTF8.GetString(reqHTML); Console.WriteLine(objUTF8.GetString(reqHTML)); Console.ReadKey(); }

25 N-Queens Problem A classic backtracking algorithm is the solution to the N-Queens problem. In this problem you are to place queens (chess pieces) on an NxN chessboard in such a way that no two queens are directly attacking one another. That is no two queens share the same row, column or diagonal on the board. Backtracking Approach - Version 1: Until all queens are placed, choose the first available location and put the next queen in this position. If queens remain to be placed and no space is left, backtrack (by removing the last queens placed and placing it in the next available position).

26 N-Queens: Version Two Some analysis of this problem shows that, since N queens must be placed on an NxN board, every row and column will have exactly one queen. That is, no two queens can share a row or column, otherwise they would be attacking each other. Using this simple observation we can redefine our algorithm to one in which we are to associate each queen with 1 of n values. That is find a row number i for each queen Qj (the queen of the jth column). 1 4 2 3 Q1 = 2 Q1 = 4 Q1 = 1 Q1 = 3

27 Minimum Spanning Trees
The minimum spanning tree problem is to find the minimum weight tree embedded in a weighted graph that includes all the vertices. C D F E A G B 4 2 3 5 1 Weighted graph data representations edge list AB 1 AE 2 BC 1 BD 2 BE 5 BF 2 BG 2 CG 4 DE 3 DG 1 EF 1 FG 2 matrix A B C D E F G A B C D E F G Which data representation would you use in an implementation of a minimum spanning tree algorithm? Why?

28 Prim's Algorithm Given a weighted graph G consisting of a set of vertices V and a set of edges E with weights, where Prepare a vertex set and an edge set to hold elements selected by Prim's Algorithm. 1. Choose an arbitrary starting vertex vj 2. Find the smallest edge e incident with with a vertex in the vertex set whose inclusion in the edge set does not create a cycle. 3. Include this edge in the edge list and its vertices in the vertex list. 4.Repeat Steps 2 and 3 until all vertices are in the vertex list. C D F E A G B 4 2 3 5 1 2

29 Summary Spanning Tree A spanning tree includes all the nodes of a graph A graph is connected IFF is has a spanning tree Multicast Spanning Tree Web Spiders N-Queens Problem Prim's & Kruskal's Algorithms


Download ppt "Lecture 13: Tree Traversals Algorithms on Trees."

Similar presentations


Ads by Google