31
CSCI 115 Chapter 7 Trees

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

Embed Size (px)

Citation preview

Page 1: 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

CSCI 115

Chapter 7

Trees

Page 2: 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

CSCI 115

§7.1

Trees

Page 3: 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

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

Page 4: 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

§7.1 – Trees

• Root• Rooted tree (Notation: (T, v0))• Vertex• Parent• Offspring• Siblings• Descendant• Levels• Height• Leaf

Page 5: 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

§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

Page 6: 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

§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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

CSCI 115

§7.2

Labeled Trees

Page 10: 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

§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

Page 11: 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

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

Page 12: 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

CSCI 115

§7.3

Tree Searching

Page 13: 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

§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

Page 14: 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

§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)

Page 15: 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

§7.3 – Tree Searching

• Binary tree search algorithm 1 – Preorder Search1. Visit v0

2. If vL exists, apply algorithm to T(vL)

3. If vR exists, apply algorithm to T(vR)

• Evaluation of polish (prefix) notation1. 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 Fxy3. Repeat until 1 number or an algebraic expression remains

Page 16: 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

§7.3 – Tree Searching

• Binary tree search algorithm 2 – Inorder Search1. If vL exists, apply algorithm to T(vL)

2. Visit v0

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

Page 17: 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

§7.3 – Tree Searching

• Binary tree search algorithm 3 – Postorder Search1. If vL exists, apply algorithm to T(vL)

2. If vR exists, apply algorithm to T(vR)

3. Visit v0

• Evaluation of reverse polish (postfix) notation1. 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 xyF3. Repeat until 1 number or an algebraic expression remains

Page 18: 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

§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

Page 19: 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

§7.3 – Tree Searching

• Searching general trees (i.e. non binary trees)

1. Find binary representation

2. Apply desired search algorithm

Page 20: 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

CSCI 115

§7.4

Undirected Trees

Page 21: 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

§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

Page 22: 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

§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

Page 23: 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

§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

Page 24: 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

§7.4 – Undirected Trees

• Theorem 7.4.3– A tree with n vertices has n – 1 edges

Page 25: 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

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

Page 26: 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

§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

Page 27: 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

§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}

1. Choose a vertex of v1 in R and find MR s.t. the 1st row corresponds to v1.

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

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.

Page 28: 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

CSCI 115

§7.5

Minimal Spanning Trees

Page 29: 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

§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

Page 30: 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

§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}

1. Choose a vertex of v1 in R. Let V = {v1} and E = {}.

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

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.

Page 31: 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

§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

1. Choose an edge e1 in S of least weight. Let E = {e1}. Replace S with S – {e1}.

2. 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}

3. Repeat step 2 until |E| = n – 1. Then E contains the edges of a minimal spanning tree for R.