310 likes | 476 Views
CSCI 115. Chapter 7 Trees. CSCI 115. §7 .1 Trees. §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 . §7 .1 – Trees. Root
E N D
CSCI 115 Chapter 7 Trees
CSCI 115 §7.1 Trees
§7.1 – Trees • TREE • Let T be a relation on a set A. T is a tree if there exists a vertex v0 in A s.t. there is a unique path from v0 to every other vertex in A, but no path from v0 to v0.
§7.1 – Trees • Root • Rooted tree (Notation: (T, v0)) • Vertex • Parent • Offspring • Siblings • Descendant • Levels • Height • Leaf
§7.1 – Trees • Theorem 7.1.1 • Let (T, v0) be a rooted tree. Then:i) There are no cycles in Tii) v0 is the only root of Tiii) Each vertex in T other than v0 has in-degree 1 and v0 has in-degree 0
§7.1 – Trees • Theorem 7.1.2 • Let (T, v0) be a rooted tree on a set A. Then:i) T is irreflexiveii) T is asymmetriciii) If (a,b) T and (b,c) T, then (a, c) T a, b, c T
§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.
§7.1 – Trees • Let (T, v0) be a rooted tree on A, with vT. Let B be the set of v and all of its descendents (BA). Then T(v) is the restriction of T to B. • Theorem 7.1.3 • If (T, v0) 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.
CSCI 115 §7.2 Labeled Trees
§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
§7.2 – Labeled Trees • Procedure to find visual representation of computer representation • Construct doubly linked list representation. • Create 4 arrays, each with one more element than there are vertices in T. Call the arrays Index, Left, Data, and Right. • Fill in Index and Data arrays according to the linked list representation constructed in 1. • Fill in the last 2 elements in each row, doing one row at the time.
CSCI 115 §7.3 Tree Searching
§7.3 – Tree Searching • Tree searching (walking, traversing) • Visiting each vertex in some specified order • Visit is defined accordingly • Terminology for binary tree • VL is the left offspring of a vertex, VR is the right offspring • If VL exists, T(VL) is the left subtree of v • If VR exists, T(VR) is the right subtree of v
§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)
§7.3 – Tree Searching • Binary tree search algorithm 1 – Preorder Search • Visit v0 • If vL exists, apply algorithm to T(vL) • If vR exists, apply algorithm to T(vR) • Evaluation of polish (prefix) notation • 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 • Evaluate xFy and substitute the answer for Fxy • Repeat until 1 number or an algebraic expression remains
§7.3 – Tree Searching • Binary tree search algorithm 2 – Inorder Search • If vL exists, apply algorithm to T(vL) • Visit v0 • If vR exists, apply algorithm to T(vR) • Evaluation of infix notation • Since the inorder search results in an ambiguous expression, there is no algorithm to evaluate it
§7.3 – Tree Searching • Binary tree search algorithm 3 – Postorder Search • If vL exists, apply algorithm to T(vL) • If vR exists, apply algorithm to T(vR) • Visit v0 • Evaluation of reverse polish (postfix) notation • 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 • Evaluate xFy and substitute the answer for xyF • Repeat until 1 number or an algebraic expression remains
§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: • vL (the left offspring) of v in B(T) is the 1st offspring of v in T if it exists • vR (the right offspring) of v in B(T) is the next sibling of v in T if it exists
§7.3 – Tree Searching • Searching general trees (i.e. non binary trees) • Find binary representation • Apply desired search algorithm
CSCI 115 §7.4 Undirected Trees
§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
§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 treeii) R is connected and acyclic
§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 acyclicii) R is connected, and if any undirected edge is removed from R, the new relation is not connected
§7.4 – Undirected Trees • Theorem 7.4.3 • A tree with n vertices has n – 1 edges
§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.
§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
§7.4 – Undirected Trees • Prim’s Algorithm for finding a spanning tree for a symmetric connected relation R on a set A = {v1, v2, …, vn} • Choose a vertex of v1 in R and find MR s.t. the 1st row corresponds to v1. • Choose a vertex v2 of R s.t. (v1, v2) R. Merge v1 & v2 into a new vertex v1‘ representing {v1, v2} and replace v1 by v1‘. Compute the matrix of the resulting relation R’. The vertex v1‘ is called a merged vertex. • 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. • 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.
CSCI 115 §7.5 Minimal Spanning Trees
§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
§7.5 – Minimal Spanning Trees • Prim’s Algorithm for finding a minimal spanning tree for a symmetric connected relation R on a set A = {v1, v2, …, vn} • Choose a vertex of v1 in R. Let V = {v1} and E = {}. • Choose a nearest neighbor vi of R that is adjacent to some vj of V and for which the edge (vi, vj) does not form a cycle with members of E. Add vi to V and add (vi, vj) to E. • 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.
§7.5 – Minimal Spanning Trees • Kruskal’s Algorithm for finding a minimal spanning tree for a symmetric connected relation R on a set A = {v1, v2, …, vn} where S={e1, e2, …, ek} is the set of weighted edges of R • Choose an edge e1 in S of least weight. Let E = {e1}. Replace S with S – {e1}. • Select an edge ei of S of least weight that will not make a cycle with members of E. Replace E with E {ei} and S with S - {ei} • Repeat step 2 until |E| = n – 1. Then E contains the edges of a minimal spanning tree for R.