trees krw

16
Trees Kasun Ranga Wijeweera (Email: [email protected])

Upload: kasun-ranga-wijeweera

Post on 13-Jun-2015

57 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Trees krw

Trees

Kasun Ranga Wijeweera

(Email: [email protected])

Page 2: Trees krw

What is a Tree?• A two dimensional linked structure• Examples:– Family tree– Organization of sports tournament– Organizational chart of large cooperation

Page 3: Trees krw

A Tree• A tree is a nonempty collection of vertices and edges that

satisfies certain requirements• A vertex is a simple object (also referred to as a node) that can

have a name and carry other associated information• An edge is a connection between two vertices• A path in a tree is a list of distinct vertices in which successive

vertices are connected by edges in the tree• One node in the tree is designated as the root• The defining property of a tree is that there is exactly one path

between the root and each of the other nodes in the tree

Page 4: Trees krw

A Sample Tree: Figure 1

Page 5: Trees krw

Details• Each node (except the root) has exactly one node above it,

which is called its parent• The nodes directly below a node are called its children• In Figure 1, P is the grandchild of R and has three siblings• Nodes with no children are sometimes called leaves, or

terminal nodes• Nodes with at least one child are called non terminal nodes• Sometimes we refer to non terminal nodes as internal nodes

and terminal nodes as external nodes

Page 6: Trees krw

A Sub Tree• Any node is the root of a sub tree consisting of it and the

nodes below it• In Figure 1, there are – seven one-node sub trees– one three-node sub tree– one five-node sub tree– one six-node sub tree

Page 7: Trees krw

A Forest• A set of trees is called a forest• For example, if we remove the root and the edges connecting

it from the tree in Figure 1, we are left with a forest consisting of three trees rooted at A, R, and E

Page 8: Trees krw

An Ordered Tree• An ordered tree is one in which the order of the children at

every node is specified• There are many different ways to draw trees that are not

ordered

Page 9: Trees krw

Levels of a Tree• The level of a node is the number of nodes on the path from the

node to the root (not including itself)• For example, in Figure 1, R is on level 1 and S is on level 2• The height of a tree is the maximum level among all nodes in the

tree (or maximum distance to the root from any node)• The path length of a tree is the sum of the levels of all the nodes

in the tree (or the sum of the lengths of the paths from each node to the root)

• The tree in Figure 1 is of height 3 and path length 21• If internal nodes are distinguished from external nodes, we speak

of internal path length and external path length

Page 10: Trees krw

A Multi Way Tree• If each node must have a specific number of children

appearing in a specific order, then we have a multi way tree• In such a tree, it is appropriate to define special external nodes

which have no children (and usually no name or other associated information)

• Then external nodes act as “dummy” nodes for reference by nodes that do not have the specified number of children

Page 11: Trees krw

A Binary Tree• A binary tree is an ordered tree consisting of two types of

nodes: external nodes with no children and internal nodes with exactly two children

• Since the two children of each internal node are ordered, we refer to the left child and the right child of internal nodes

• Every internal node must have both a left and a right child, though one or both of them might be an external node

Page 12: Trees krw

A Sample Binary Tree: Figure 2

Page 13: Trees krw

Binary Tree Details• The purpose of the binary tree is to structure the internal

nodes; the external nodes serve only as placeholders• A binary tree could be “empty”, consisting of no internal

nodes and one external node

Page 14: Trees krw

Reference

Page 15: Trees krw

Any Questions?

Page 16: Trees krw

Thank You!