Download presentation
Presentation is loading. Please wait.
Published byWilla Ward Modified over 9 years ago
1
CSCI 115 Chapter 7 Trees
2
CSCI 115 §7.1 Trees
3
§7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there is a unique path from v 0 to every other vertex in A, but no path from v 0 to v 0.
4
§7.1 – Trees Root Rooted tree (Notation: (T, v 0 )) Vertex Parent Offspring Siblings Descendant Levels Height Leaf
5
§7.1 – Trees Theorem 7.1.1 –Let (T, v 0 ) be a rooted tree. Then: i) There are no cycles in T ii) v 0 is the only root of T iii) Each vertex in T other than v 0 has in-degree 1 and v 0 has in-degree 0
6
§7.1 – Trees Theorem 7.1.2 –Let (T, v 0 ) be a rooted tree on a set A. Then: i) T is irreflexive ii) T is asymmetric iii) If (a,b) T and (b,c) T, then (a, c) T a, b, c T
7
§7.1 – Trees Let n Z +. T is an n-Tree if every vertex in T has at most n offspring. If all vertices other than the leaves have exactly n offspring, T is said to be a complete n-Tree. A 2-tree is called a binary tree, and a 3-tree is called a tertiary tree.
8
§7.1 – Trees Let (T, v 0 ) be a rooted tree on A, with v T. Let B be the set of v and all of its descendents (B A). Then T(v) is the restriction of T to B. Theorem 7.1.3 –If (T, v 0 ) is a rooted tree and v T then T(v) is also a rooted tree with root v. We say T(v) is a subtree of T beginning at v.
9
CSCI 115 §7.2 Labeled Trees
10
§7.2 – Labeled Trees Labeled Trees –Vertices labeled accordingly Positional trees –n-tree has at most n offspring per root –n ‘positions’ Binary positional trees as Data Structures –Doubly linked lists
11
§7.2 – Labeled Trees Procedure to find visual representation of computer representation 1.Construct doubly linked list representation. 2.Create 4 arrays, each with one more element than there are vertices in T. Call the arrays Index, Left, Data, and Right. 3.Fill in Index and Data arrays according to the linked list representation constructed in 1. 4.Fill in the last 2 elements in each row, doing one row at the time.
12
CSCI 115 §7.3 Tree Searching
13
§7.3 – Tree Searching Tree searching (walking, traversing) –Visiting each vertex in some specified order –Visit is defined accordingly Terminology for binary tree –V L is the left offspring of a vertex, V R is the right offspring –If V L exists, T(V L ) is the left subtree of v –If V R exists, T(V R ) is the right subtree of v
14
§7.3 – Tree Searching Algorithms for searching binary trees –Preorder Polish (prefix) notation Result is unambiguous (algebraically) –Inorder Infix notation Result is ambiguous(and less useful) –Postorder Postfix (reverse polish) notation Result is unambiguous (algebraically)
15
§7.3 – Tree Searching Binary tree search algorithm 1 – Preorder Search 1.Visit v 0 2.If v L exists, apply algorithm to T(v L ) 3.If v R exists, apply algorithm to T(v R ) Evaluation of polish (prefix) notation 1.Move left to right until a 3 byte string is found in the form Fxy where F is a binary operation, and x and y are numbers or variables 2.Evaluate xFy and substitute the answer for Fxy 3.Repeat until 1 number or an algebraic expression remains
16
§7.3 – Tree Searching Binary tree search algorithm 2 – Inorder Search 1.If v L exists, apply algorithm to T(v L ) 2.Visit v 0 3.If v R exists, apply algorithm to T(v R ) Evaluation of infix notation –Since the inorder search results in an ambiguous expression, there is no algorithm to evaluate it
17
§7.3 – Tree Searching Binary tree search algorithm 3 – Postorder Search 1.If v L exists, apply algorithm to T(v L ) 2.If v R exists, apply algorithm to T(v R ) 3.Visit v 0 Evaluation of reverse polish (postfix) notation 1.Move left to right until a 3 byte string is found in the form xyF where F is a binary operation, and x and y are numbers or variables 2.Evaluate xFy and substitute the answer for xyF 3.Repeat until 1 number or an algebraic expression remains
18
§7.3 – Tree Searching Searching general trees (i.e. non binary trees) –Any ordered tree T with A being the set of vertices can be represented by a binary tree B(T) by the following algorithm If v A, then: –v L (the left offspring) of v in B(T) is the 1 st offspring of v in T if it exists –v R (the right offspring) of v in B(T) is the next sibling of v in T if it exists
19
§7.3 – Tree Searching Searching general trees (i.e. non binary trees) 1.Find binary representation 2.Apply desired search algorithm
20
CSCI 115 §7.4 Undirected Trees
21
§7.4 – Undirected Trees Undirected tree –Symmetric closure of the corresponding tree Terminology –Undirected edge –Adjacent vertices –Simple path No 2 edges correspond to the same undirected edge –Simple cycle Simple path that is also a cycle –Acyclic Contains no simple cycles –Connected
22
§7.4 – Undirected Trees Theorem 7.4.1 –Let R by a symmetric relation on a set A. Then the following are equivalent (TFAE): i) R is an undirected tree ii) R is connected and acyclic
23
§7.4 – Undirected Trees Theorem 7.4.2 –Let R by a symmetric relation on a set A. R is an undirected tree iff either of the following is true: i) R is acyclic, and if any undirected edge is added to R, the new relation is not acyclic ii) R is connected, and if any undirected edge is removed from R, the new relation is not connected
24
§7.4 – Undirected Trees Theorem 7.4.3 –A tree with n vertices has n – 1 edges
25
§7.4 – Undirected Trees Spanning trees of connected relations –Let R be a symmetric connected relation on A. T is a spanning tree for R if T is a tree with the same vertices as R, and T can be obtained from R by deleting edges of R. An undirected spanning tree is the symmetric closure of the corresponding spanning tree.
26
§7.4 – Undirected Trees Algorithm for finding an undirected spanning tree for a symmetric connected relation R. –Remove undirected edges from R until the removal of one more edge would result in disconnection Disadvantage of this algorithm –Performance time – checking for connectedness
27
§7.4 – Undirected Trees Prim’s Algorithm for finding a spanning tree for a symmetric connected relation R on a set A = {v 1, v 2, …, v n } 1.Choose a vertex of v 1 in R and find M R s.t. the 1 st row corresponds to v 1. 2.Choose a vertex v 2 of R s.t. (v 1, v 2 ) R. Merge v 1 & v 2 into a new vertex v 1 ‘ representing {v 1, v 2 } and replace v 1 by v 1 ‘. Compute the matrix of the resulting relation R’. The vertex v 1 ‘ is called a merged vertex. 3.Repeat steps 1 & 2 on R’ and all subsequent relations until a relation with a single vertex is obtained. At each step, keep a record of the set of original vertices that is represented by each merged vertex. 4.Construct the spanning tree as follows. At each stage, when merging vertices a & b, select an edge in R from one of the original vertices represented by a to one of the original vertices represented by b.
28
CSCI 115 §7.5 Minimal Spanning Trees
29
§7.5 – Minimal Spanning Trees Weighted graph –Graph where each edge is labeled with a numerical value (its weight) Nearest neighbor of a vertex –The adjacent vertex the smallest distance away Nearest neighbor of a set of vertices V –The vertex adjacent to any member of V which is the smallest distance away (may be a member of V) Minimal spanning tree –Undirected spanning tree for which the total weight of the edges is as small as possible
30
§7.5 – Minimal Spanning Trees Prim’s Algorithm for finding a minimal spanning tree for a symmetric connected relation R on a set A = {v 1, v 2, …, v n } 1.Choose a vertex of v 1 in R. Let V = {v 1 } and E = {}. 2.Choose a nearest neighbor v i of R that is adjacent to some v j of V and for which the edge (v i, v j ) does not form a cycle with members of E. Add v i to V and add (v i, v j ) to E. 3.Repeat step 2 until |E| = n – 1. Then V contains all n vertices of R, and E contains the edges of a minimal spanning tree for R.
31
§7.5 – Minimal Spanning Trees Kruskal’s Algorithm for finding a minimal spanning tree for a symmetric connected relation R on a set A = {v 1, v 2, …, v n } where S={e 1, e 2, …, e k } is the set of weighted edges of R 1.Choose an edge e 1 in S of least weight. Let E = {e 1 }. Replace S with S – {e 1 }. 2.Select an edge e i of S of least weight that will not make a cycle with members of E. Replace E with E {e i } and S with S - {e i } 3.Repeat step 2 until |E| = n – 1. Then E contains the edges of a minimal spanning tree for R.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.