LECTURE ABOUT TREE

Preview:

Citation preview

1

FOA

Lecture 10Trees

2

TreesImposes a Hierarchical structure, on a collection of items.

Applications can be: Organization charts. Organization of information in database systems. Representation of syntactic structure of source

programs in compilers.

3

TreesPakistan BOI Organizational chart

4

TreesRoyal Genealogies

Roman Emperors: Julian-Claudian House (27BC-68AD)Roman Emperors: Julian-Claudian House (27BC-68AD) France

CHRONOLOGY The Merovingian Kings (481-752) Pepin and Charlemagne (640-987)

Monarchs of France (987-1883) Capet to Valois (987-1328)

House of Valois (1328-1589) the French Bourbons 1589-1883)

                                 

5

Trees

Organization of information in database systems.

CUSTOMERCUSTOMER NUMBERCUSTOMER NAMECUSTOMER CITYCUSTOMER POSTCUSTOMER STCUSTOMER ADDRCUSTOMER PHONECUSTOMER FAX

ORDERORDER NUMBERORDER DATESTATUS

ORDER ITEM BACKORDEREDQUANTITY

ITEMITEM NUMBERQUANTITYDESCRIPTION

ORDER ITEM SHIPPEDQUANTITYSHIP DATE

6

Trees: Basic TerminologyA Tree is a collection of elements called nodes.

One of the node is distinguished as a root, along with a relation (“parenthood”) that places a hierarchical structure on the nodes.

A node can be of any type. e.g.; A Letter, a String, or a Number.

7

TreesA Tree can be defined recursively.

A single node by itself is a tree. This node is also the root of the tree.

Suppose n is a node and T1,T2,…Tk are trees with roots n1,n2,…nk, respectively.

Construct a new tree by making n the parent of nodes n1,n2,…nk.

8

Trees

T1,T2,…Tk the subtrees.

n1,n2,…nk the children of node n.

Null Tree: A Tree with no nodes is called a “Null Tree”.

9

Table of Contents: An Example of Trees

BookC1

s1.1s1.2

C2s2.1

s2.1.1s2.1.2

s2.2s2.3

C3

Book

C1 C2 C3

s1.1 s1.2 s2.1 s2.2 s2.3

s2.1.1 s2.1.2

10

The root node, called “Book”, has three subtrees.

The root of each subtree corresponds to a chapter, C1, C2, C3

The downward line represents: “Book” as the parent of C1, C2, C3 and these are the children of “Book”

Table of Contents: An Example of Trees

11

The subtree, with root C3 is a tree of single node.

The subtree with root C2 has three subtrees, corresponding to the subsections s2.1, s2.2 and s2.3.

Table of Contents: An Example of Trees

12

Trees: Some Definitions Path: If n1,n2,…nk is a sequence of nodes in a tree

such that ni is a parent of ni+1 for i <= I < k, then this sequence is called a Path from n1 node to nk.

Length of Path: One less than the number of nodes in the path.

There is a path of length zero from every node to itself.

13

Zero length pathBook

C1 C2 C3

s1.1 s1.2 s2.1 s2.2 s2.3

s2.1.1 s2.1.2

Trees: Some Definitions

Path from C2 to s2.1.1Path length is 2

14

Trees: Some Definitions If there is a path from node a to node b, then a is an

ancestor of b and b is a descendant of a.

Any node is both an ancestor and descendant of itself.

An ancestor or descendant of a node, other than the node itself, is called a proper ancestor or proper descendant of the node.

15

Book

C1 C2 C3

s1.1 s1.2 s2.1 s2.2 s2.3

s2.1.1 s2.1.2

Trees: Some Definitions

C2 ancestor of s2.1s2.1 ancestor of s2.1.1

16

Trees: Some Definitions The root is the only node with no proper ancestor.

A node with no proper descendant is called a leaf.

A subtree of a node is a node, together with all its descendants.

The height of a node in a tree is the length of a longest path from that node to the leaf.

17

Book

C1 C2 C3

s1.1 s1.2 s2.1 s2.2 s2.3

s2.1.1 s2.1.2

Trees: Some DefinitionsNo proper ancestor

No proper descendent

Sub tree

Height

18

Trees: Some Definitions The height of a tree is the height of the

root.

The depth of a node is the length of the path from the root to that node.

19

Book

C1 C2 C3

s1.1 s1.2 s2.1 s2.2 s2.3

s2.1.1 s2.1.2

Trees: Height of a Tree

20

The Order of NodesThe children of a node are usually ordered from left-to-right.

b c

a

c b

a

Two distinct (ordered) trees

21

The Order of NodesComparison on bases of ordering:

“If a and b are siblings, and a is to the left of b, then all the descendants of a are to the left of all the descendent of b.”

22

The Order of Nodes

1

2 3 4

5 6 7

8 9 10

Node 8 is to the right of node 2.

Node 8 is the left of nodes 9,6,10,4 and 7.

Neither left nor right of its ancestor 1,3 and 5.

23

The Order of NodesThree most important ordering are: Preorder

Postorder

InorderThese ordering are defined recursively.

24

The Order of Nodes If a tree T is null, then the empty list is

the preorder, inorder and postorder listing of T.

If T consists a single node, then that node by itself is the preorder, inorder and postorder listing of T.

25

The Order of NodesLet T be a tree with root n and subtrees

T1,T2,…Tk

n

T1 T2 Tk…

26

The Order of NodesThe preorder listing of the nodes of T is the root n of T followed by the nodes of T1 in preorder, then the nodes of T2 in preorder, and so on, up to the nodes of Tk in preorder.

n

T1 T2 Tk…

27

The Order of NodesThe inorder listing of the nodes of T is the nodes of T1 in inorder, followed by the node n, followed by the nodes of T2,…, Tk, each group of nodes in inorder.

n

T1 T2 Tk…

28

The Order of NodesThe postorder listing of the nodes of T is the nodes of T1 in postorder, then the nodes of T2 in postorder, and so on, up to Tk, all followed by node n.

n

T1 T2 Tk…

29

1

2 3 4

5 6 7

8 9 10

The Order of NodesWalk around the outside of the tree, start at the root, move counterclockwise and stay as close to the tree as is possible.

30

The Order of Nodes For preorder, list the nodes the first time

you pass it. For postorder, list a nodes the last time

you pass it, as you move up to the parent.

For inorder, list a leaf the first time you pass it, but list an interior node the second time you pass it.

31

The Order of Nodes

The ordering of the interior nodes and their relationship to the leaves vary among the three orderings.

32

Labeled and Expressions TreesA label is a value stored at the node.

n4 n5

n2

n6 n7

n3

n1*

+ +

a ab c

A labeled tree representing expression (a + b) * ( a + c)

LabelName ofThe node

33

Labeled and Expressions Trees1. Every leaf is labeled by an operand and

consists of that operand alone.2. Every interior node n is labeled by an

operator. If n represents the binary operator Θ, the left child represent the expression E1 and the right child represent the Expression E2, then n represents (E1) Θ (E2).

34

Labeled and Expressions Trees The Preorder listing of the Expression tree produces

a prefix expression. In a prefix expression the operator precedes its left

and right operand. The prefix operation for a single operand a is a itself. The prefix expression for (E1) Θ (E2), is Θ P1 P2,

where P1 and P2 are the prefix expressions for E1 and E2.

No parentheses are necessary.

35

Labeled and Expressions Trees

n4 n5

n2

n6 n7

n3

n1

+ +

a ab c

The preorder listing of the labels is *+ab+ac *

The prefix expression for n2 is +ab, is the shortest legal prefix of *+ab+ac.

36

Labeled and Expressions Trees The Postorder listing of the Expression tree

produces a postfix (or Polish)expression. In a postfix expression the operator follows

its left and right operand. The postfix expression for (E1) Θ (E2), is P1

P2 Θ, where P1 and P2 are the postfix expressions for E1 and E2.

No parentheses are necessary.

37

Labeled and Expressions Trees

n4 n5

n2

n6 n7

n3

n1

+ +

a ab c

The postorder listing of the labels is ab+ac+* *

The postfix expression for n2 is ab+, is the shortest legal postfix of ab+ac+*.

38

Labeled and Expressions Trees

n4 n5

n2

n6 n7

n3

n1

+ +

a ab c

The inorder traversal gives the infix expression, but without any parentheses.

a+b*a+c