• Tidak ada hasil yang ditemukan

Tree

N/A
N/A
Protected

Academic year: 2025

Membagikan "Tree"

Copied!
71
0
0

Teks penuh

(1)

Chapter 5

Tree

(2)

Introduction to Trees:

◦ Basic Definitions

◦ Rooted Trees

Applications of Trees:

◦ Trees as Models

◦ Binary Search Trees

◦ Tree Traversal

(3)

A tree:

◦ Is a connected graph with no cycles

◦ It can not contain multiple edges or loops.

Theorems:

◦ A graph is a tree if and only if there is a unique

simple path between any two of its vertices. (Why?)

◦ |V|=|E|+1 (Why?)

(4)
(5)
(6)

Examples:

G1 and G2 are trees.

G3 and G4 are not a tree, G3 contains cycle, G4 not connected.

(7)
(8)

A forest:

◦ Is a graph where each of its connected components is a tree.

A graph with three connected components

(9)

A rooted tree:

◦ Is a directed graph T satisfying two conditions:

 It is a tree if the directions of the edges are ignored.

 There is a unique vertex

r

such that the indegree of

r

is 0 and the indegree of any other vertex is 1.

This vertex r is called the root of the rooted tree.

◦ We can change any unrooted tree into a rooted tree

by choosing any vertex as the root.

(10)

Examples:

Different choices of the root produce different rooted trees.

T With root a With root c

(11)
(12)

Rooted tree terminology:

◦ For a directed edge from vertex u to vertex v:

u is a parent of v, v is a child of u.

◦ For a vertex u:

Any vertices (excluding vertex u) on the path from the root to the vertex u are the ancestors of u.

Vertex u is the descendant of these vertices.

◦ A terminal vertex or a leaf is a vertex that has no children out degree = 0 . (There must be at least one leaf. Why?)

◦ An internal vertex or branch node is one that has children.

◦ The length of path from root to the vertex is vertex level

◦ A subtree is a subgraph of a tree.

◦ Two vertices of a common parent are referred to as siblings

(13)

Examples:

The left child of d is f.

The right child of d is g.

(b) and (c) show the left and right subtrees of c.

(14)
(15)

Rooted tree terminology (continued):

An m-ary tree:

Is a rooted tree in which each internal vertex has at most m children.

Special case: binary tree where m = 2.

A complete m-ary tree:

Is a rooted tree in which each internal vertex has exactly m children.

A balanced m-ary tree with height h :

Is a m-ary tree with all leaves being at levels h or h-1.

Level: distance from root

Height: maximum level

(16)

Examples:

◦ T1 is a complete binary tree. T1 is not balanced.

◦ T2 is a balanced complete 5-ary tree.

◦ T3 is a balanced 3-ary but not a complete m-ary tree for any m.

T3 T1 T2

(17)
(18)
(19)
(20)
(21)

Ordered rooted trees are often used to store information.

A binary search tree:

Is a binary tree in which each vertex is labeled with a key such that:

 No two vertices have the same key.

 If vertex u belongs to the left subtree of vertex v, then u  v.

 If vertex w belongs to the right subtree of vertex v, then v  w.

(22)

Binary search tree construction algorithm:

◦ Start with a tree containing just one vertex (the root).

◦ Let v = the root.

◦ To add a new item a, do the following until stop:

If a < v:

If v has a left child then v = left child of v (move to the left)

Else add a new left child to v with this item as its key. Stop.

If a > v:

If v has a right child then v = right child of v (move to the right).

Else add a new right child to v with this item as its key. Stop.

(23)

Examples:

Construct a binary search tree for the list 5, 9, 8, 1, 2,

4, 10, 6.

(24)

Binary search tree search algorithm:

◦ Let v = the root.

◦ To search the item a, do the following until stop:

If a = v then stop.

If a < v:

If v has a left child then v = left child of v (move to the left)

Else stop.

If a > v:

If v has a right child then v = right child of v (move to the right).

Else stop.

(25)

Examples:

Search for 7 in the following binary tree.

If not found, add it to the tree.

(26)

◦ An ordered rooted tree:

 Is a rooted tree where the children of each internal vertex are ordered.

◦ In an ordered binary tree, if an internal vertex u has two children:

 The first child is called the left child.

The tree rooted at the left child is called the left subtree of vertex u.

 The second child is called the right child.

The tree rooted at the right child is called the right subtree of vertex u.

(27)

Tree traversal:

Is a procedure that systematically visits every vertex of an ordered rooted tree.

 Visiting a vertex = processing the data at the vertex.

Three most commonly used algorithms:

 Preorder traversal.

 Inorder traversal.

 Postorder traversal.

(28)

Let T be an ordered rooted tree with root r .

Suppose that T

1

, T

2

, , T

n

are the subtrees at r from left to right in T .

T2

T1

T3

(29)

Preorder traversal:

Begins by visiting r .

Next traverses T

1

in preorder, then T

2

in preorder, , then T

n

in preorder.

Ends after T

n

has been traversed.

Preorder traversal

(30)

In binary tree the traversal is characterized by

Visiting a parent before its children and a left child before a right child.

Algorithm:

 Step 1: Visit the root.

 Step 2: Go to the left subtree.

If one exists, do a preorder traversal.

 Step 3: Go to the right subtree.

If one exists, do a preorder traversal.

(31)

Preorder traversal:

An example of recursive algorithm.

(32)

Examples:

Preorder traversal:

Visit root, visit subtrees left to right.

(33)
(34)

Applications: print a structured document

Make Money Fast!

1. Motivations 2. Methods References

2.1 Stock

Fraud 2.2 Ponzi Scheme

1.1 Greed 1.2 Avidity 2.3 Bank

Robbery

1

2

3

5

4 6 7 8

9

(35)

Inorder traversal:

Begins by traversing T

1

in inorder.

Next visits r , then traverses T

2

in inorder, , then T

n

in inorder.

Ends after T

n

has been traversed.

In order traversal

(36)
(37)

Examples:

◦ Inorder traversal:

Visit leftmost subtree, visit root, visit other subtrees left to right.

(38)
(39)

Applications: print out information stored in a binary search tree.

3 1

2

5 6

7 9

8 4

(40)

Postorder traversal:

Begins by traversing T

1

in postorder.

Next traverses T

2

in postorder, then traverses T

3

in postorder, , then T

n

in postorder.

Ends by visiting r .

Postorder traversal

(41)
(42)

Examples:

◦ Postorder traversal:

Visit subtrees left to right, visit root.

(43)
(44)

Applications: compute space used by files in

a directory and its subdirectories.

cs16/

homeworks/ todo.txt

programs/ 1K

DDR.java

10K Stocks.java h1c.doc 25K

3K h1nc.doc

2K Robot.java

20K

9

3

1

7

2 4 5 6

8

(45)

Shortcut to list the vertices in preorder, inorder, postorder:

Draw a curve around the ordered rooted tree starting

at the root and move along the edges.

(46)

◦ Preorder list is obtained by

listing each vertex the first time this curve passes it.

Result: a, b, d, h, e, i, j, c, f, g, k.

◦ Inorder list is obtained by

listing a leaf the first time the curve passes it and

listing each internal vertex the second time the curve passes it.

Result: h, d, b, i, e, j, a, f, c, k, g.

◦ Postorder list is obtained by

listing a vertex the last time it is passes on the way back up to its parent.

Result: h, d, i, j, e, b, f, k, g, c, a.

(47)

Spanning Trees

Minimum Spanning Trees

◦ Prim’s Algorithm

(48)

A spanning tree of a graph G :

Is a tree (formed by using edges and vertices of G) containing all the vertices of G .

Examples:

 A graph (of road system) and its spanning tree.

(49)

Examples:

Find a spanning tree of this graph G :

Solution:

 The graph G is connected, but is not a tree because it contains simple circuits.

 Remove an edge from each circuit until we produce a graph with no simple circuits.

(50)

 The resulting spanning tree is not unique.

(51)

Examples:

Spanning trees play an important role in transportation systems

 Highway construction

(52)

Examples:

Broadcast in Internet

(53)
(54)
(55)

Depth-first search: Strategy (for digraph,trees)

◦ choose a starting vertex, distance d = 0

◦ vertices are visited in order of increasing distance from the starting vertex,

◦ examine One edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)

◦ then, examine One edges leading from vertices at distance d+1 to distance d+2, and so on,

◦ until no new vertex is discovered, or dead end

◦ then, backtrack one distance back up, and try other edges, and so on

◦ until finally backtrack to starting vertex, with no more new vertex to be discovered.

(56)

and so on…

(57)
(58)
(59)
(60)
(61)

Breadth-first search: Strategy (for digraph)

◦ choose a starting vertex, distance d = 0

◦ vertices are visited in order of increasing distance from the starting vertex,

◦ examine all edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)

◦ then, examine all edges leading from vertices at distance d+1 to distance d+2, and so on,

◦ until no new vertex is discovered

(62)

e.g. Start from vertex A, at d = 0

◦ visit B, C, F; at d = 1

◦ visit D; at d = 2

e.g. Start from vertex E, at d = 0

◦ visit G; at d = 1

(63)
(64)
(65)
(66)

Minimum spanning tree in a weighted graph :

◦ Is a spanning tree that has the smallest possible sum of the weights of its edges.

(67)
(68)

Prim’s algorithm for constructing minimum spanning tree:

◦ Choose an arbitrary vertex as a start of the spanning tree.

◦ Successively add to the tree edges of minimum weight that are incident to a vertex already in the tree

Break ties arbitrarily

◦ Stop when n – 1 edges have been added.

n = number of vertices in the weighted graph

◦ Note: The resulting tree may not be unique, but with same total weight

(69)

Examples:

Use Prim’s algorithm to design a minimum-cost communications network connecting all the

computers in the following graph.

(70)
(71)

Introduction to Trees:

◦ Basic Definitions : Section 12.1.

◦ Rooted Trees : Section 12.2.

Applications of Trees:

◦ Tree Traversal : Section 12.2.

Minimum Spanning Trees : Section 13.2

◦ Prim’s Algorithm p.641

Referensi

Dokumen terkait

The tree cover number of a multigraph G, denoted T (G), is the minimum number of vertex disjoint simple trees occurring as induced subgraphs of G that cover all of the vertices of

If we want to compute a farthest distance tree connecting a source vertex s to several destination vertices, we can use the same transformations and then use the solution for

DFS (Depth-First Search Algorithm) merupakan algoritma untuk menelusuri vertek-vertek suatu graph, sehingga akan terbentuk suatu spanning tree dari graph tersebut..

Keywords: pastoral hill country, wide-spaced trees, carbon sequestration, greenhouse gas GHG mitigation Key messages • Soil C mass 0-1 m depth of pasture-tree PT systems involving

procedure depth-first search G: simple connected graphG with verticesv1, ...,vn T := an initial tree withvi as root and no more vertices visitvi {T is a spanning tree of G}...

Let be the number of levels of the theta graph and be the number of vertices in each level.For a strong edge monophonic set , choose a vertex from alternate levels which forms a

Results show that the increasing of sliding distance give the increasing of wear depth, wear scar diameter and wear volume of the asperity.. Wear at the center of the contacting rough

Depth-first and breadth-first search are the basis for many algorithms that process graphs in various ways, including determining whether two vertices are connected, determining whether