27
Introduction to Tree Fundamental data storage structures used in programming. Combines advantages of an ordered array and a linked list. Searching as fast as in ordered array. Insertion and deletion as fast as in linked list.

Discrete Mathematics Tree

Embed Size (px)

Citation preview

Page 1: Discrete Mathematics  Tree

Introduction to Tree

• Fundamental data storage structures used in programming.

• Combines advantages of an ordered array and a linked list.

• Searching as fast as in ordered array. • Insertion and deletion as fast as in linked

list.

Page 2: Discrete Mathematics  Tree

Tree (example)

node

edge

Draw a parse tree

Page 3: Discrete Mathematics  Tree

Tree characteristics

• Consists of nodes connected by edges.• Nodes often represent entities (complex

objects) such as people, car parts etc.• Edges between the nodes represent the

way the nodes are related.• Its easy for a program to get from one

node to another if there is a line connecting them.

• The only way to get from node to node is to follow a path along the edges.

Page 4: Discrete Mathematics  Tree

Tree Terminology• Path: Traversal from node to node along the edges

results in a sequence called path.• Root: Node at the top of the tree.• Parent: Any node, except root has exactly one edge

running upward to another node. The node above it is called parent.

• Child: Any node may have one or more lines running downward to other nodes. Nodes below are children.

• Leaf: A node that has no children.• Subtree: Any node can be considered to be the root of a

subtree, which consists of its children and its children’s children and so on.

Page 5: Discrete Mathematics  Tree

Tree Terminology

• Visiting: A node is visited when program control arrives at the node, usually for processing.

• Traversing: To traverse a tree means to visit all the nodes in some specified order.

• Levels: The level of a particular node refers to how many generations the node is from the root. Root is assumed to be level 0.

• Keys: Key value is used to search for the item or perform other operations on it.

Page 6: Discrete Mathematics  Tree

Leaf

• A vertex is called a leaf if it has no children.

6

Page 7: Discrete Mathematics  Tree

7

Owner Jake

Manager Brad Chef Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Leaf nodes have no children

LEAF NODES

Page 8: Discrete Mathematics  Tree

8

Owner Jake

Manager Brad Chef Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Subtree

LEFT SUBTREE OF ROOT

ROOT

Page 9: Discrete Mathematics  Tree

9

Owner Jake

Manager Brad Chef Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Another Subtree

RIGHT SUBTREE OF ROOT

ROOT

Page 10: Discrete Mathematics  Tree

m-ary trees

A rooted tree is called an m-ary tree if every internal vertex has no more than m children. The tree is called a full m-ary tree if every internal vertex has exactly m children. An m-ary tree with m=2 is called a binary tree.

Page 11: Discrete Mathematics  Tree
Page 12: Discrete Mathematics  Tree

Ordered Rooted Tree

An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered. Ordered trees are drawn so that the children of each internal vertex are shown in order from left to right.

Page 13: Discrete Mathematics  Tree

Properties of Trees

A tree with n vertices has n-1 edges.

Page 14: Discrete Mathematics  Tree

Properties of Trees

A full m-ary tree with i internal vertices contains n = mi+1 vertices.

Page 15: Discrete Mathematics  Tree

Properties of Trees

A full m-ary tree with

(i) n vertices has i = (n-1)/m internal vertices and l = [(m-1)n+1]/m leaves.

(ii) i internal vertices has n = mi + 1 vertices and l = (m-1)i + 1 leaves.

(iii) l leaves has n = (ml - 1)/(m-1) vertices and i = (l-1)/(m-1) internal vertices.

Page 16: Discrete Mathematics  Tree

Proof

• We know n = mi+1 (previous theorem) and n = l+i,

• n – no. vertices• i – no. internal vertices• l – no. leaves• For example, i = (n-1)/m

Page 17: Discrete Mathematics  Tree

Properties of Trees

The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex.

level 2

level 3

Page 18: Discrete Mathematics  Tree

Properties of Trees

The height of a rooted tree is the maximum of the levels of vertices.

Page 19: Discrete Mathematics  Tree

Properties of Trees

A rooted m-ary tree of height h is called balanced if all leaves are at levels h or h-1.

Page 20: Discrete Mathematics  Tree

Properties of Trees

There are at most mh leaves in an m-ary tree of height h.

Page 21: Discrete Mathematics  Tree

Properties of Trees

If an m-ary tree of height h has l leaves, then h lm lo g

Page 22: Discrete Mathematics  Tree

Proof

• From previous theorem:

hlhlml mmh loglog

Page 23: Discrete Mathematics  Tree

Minimum Spanning Tree• Input: A connected, undirected graph G =

(V, E) with weight function w : E R.• For simplicity, we assume that all edge

weights are distinct.

• Output: A spanning tree T, a tree that connects all vertices, of minimum weight:

Tvu

vuwTw),(

),()(

Page 24: Discrete Mathematics  Tree

Example of MST

6 12

5

14

3

8

10

15

9

7

Page 25: Discrete Mathematics  Tree

Kruskal’s AlgorithmIt is a greedy algorithm.

In Kruskal’s algorithm, the set A is a forest.

The safe edge is added to A is always a least-weight edge in the graph that connects two distinct Components.

Page 26: Discrete Mathematics  Tree

Kruskal’s Algorithm The operation FIND-SET(u) returns a representative element from the set that contains u.

Thus we can determine whether two vertices u and v belong to the same tree by testing whether

FIND-SET(u) = FIND-SET(v)

The combining of trees is accomplished by the UNION procedure

Page 27: Discrete Mathematics  Tree

Kruskal’s Algorithm1. A 2. for each vertex v V[G]3.do MAKE-SET(v)4.short the edges of E into non-decreasing order

by weight.5.for each edge (u, v) E, taken in non-decreasing order

by weight.6.do if FIND-SET(u) != FIND-SET(v)7. then, A A U {(u, v)}8. UNION(u, v)9.return A