Presentation is loading. Please wait.

Presentation is loading. Please wait.

Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.

Similar presentations


Presentation on theme: "Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees."— Presentation transcript:

1 Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees

2 Introduction. The Trees Abstract Date Type Basic Algorithms on Trees Outline

3 Trees Productivity experts say that breakthroughs come by thinking “nonlinearly”. Trees as a nonlinear data structure gives ways of accessing and finding elements of better performance than linear structures such as sequences. Introduction

4 Trees The main terminology for tree data structures comes from family trees, with the term “parent”, “child”, “ancestor”, and “descendent”. Introduction

5 Trees Example Prophets tree [1] اللهم صلي على سيدنا محمد [1] http://img246.imageshack.us/img246/3582/8618qz1.png

6 The Trees Abstract Data Type A tree is an abstract data type that stores elements hierarchally. With the exception of the top element, each element in a tree has a parent element and zero or more children. A tree is usually visualized by placing elements inside ovals and rectangles, and by drawing the connections between parents and children with straight lines. Definition root

7 The Trees Abstract Data Type The tree T is a set of nodes storing elements in a parent-child relationship with the following properties: T has a distinguished node r, called the root of T, that has no parent. Each node v of T distinct from r has a parent node u. For every node v of T, the root r is ancestor of v. If a node u is the parent of node v, then we say that v is a child of u. Two nodes that are children of the same parent are siblings. A node is external or leave if it has no children. A node is internal if it has one or more children. Terminology and Basic properties

8 The Trees Abstract Data Type Terminology and Basic properties rootParent of I, J, K children of F

9 The Trees Abstract Data Type Terminology and Basic properties Descendant of B, A Descendants of F, B, and A

10 The Trees Abstract Data Type Terminology and Basic properties Ancestor of all other nodes Ancestor of I, J, K

11 The Trees Abstract Data Type A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first, second, third, and so on. Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order. Terminology and Basic Properties

12 The Trees Abstract Data Type Terminology and Basic Properties Book Preface P1P2 Part A CH. 1Ch.2 Part B Ch3Ch4Ch5 References P1

13 The Trees Abstract Data Type A binary tree is a tree with the following properties: Each internal node has at most two children (exactly two for proper binary trees) The children of a node are an ordered pair. We call the children of an internal node left child and right child Alternative recursive definition: A binary tree is either a tree consisting of a single node, or a tree whose root has an ordered pair of children, each of which is a binary tree Terminology and Basic Properties

14 The Trees Abstract Data Type Example of a binary tree. Terminology and Basic Properties

15 The Trees Abstract Data Type Binary trees applications: Decision trees: Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining Terminology and Basic Properties

16 The Trees Abstract Data Type Binary trees applications: Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 × (a − 1) + (3 × b)) Terminology and Basic Properties

17 The Trees Abstract Data Type Generic methods: int size() boolean isEmpty() Enumeration elements() Enumeration positions() Accessor methods: Position root() Position parent(Position p) Enumeration children(Position p) Query methods: boolean isInternal(Position p) boolean isExternal(Position p) boolean isRoot(Position p) Update method: Object replace (Position p, Object o) void swap(Position v, Position w) Trees ADT methods

18 The Trees Abstract Data Type A Tree Interface in Java PositionalContainer > SimpleTree > int size() boolean isEmpty() Enumeration elements() Enumeration positions() Object replace (Position p, Object o) void swap(Position v, Position w) Position root() Position parent(Position p) Enumeration children(Position p) boolean isInternal(Position p) boolean isExternal(Position p) boolean isRoot(Position p)

19 Basic Algorithms on Trees Depth and Height The depth of a node v is the number of ancestors of v, excluding v itself. If v is the root, then the depth of v is 0. Otherwise, the depth of v is one plus the depth of the parent v.

20 Basic Algorithms on Trees Depth and Height public int depth(SimpleTree T,Position v){ if(T.isRoot(v)) return(0); else return(1+depth(T.parent(v))); }

21 Basic Algorithms on Trees Depth and Height The height of an entire tree T is defined to be the height of the root of T. Another way to view the height of a tree T is the maximum depth of a node of T, which is achieved at one or more external nodes.

22 Basic Algorithms on Trees Depth and Height public int height1(SimpleTree T){ int h; Enumberation nodes_of_T=T.positions(); while(nodes_of_T.hasMoreElements()) Position v=(Position)nodes_of_T.nextElement(); if(T.isExternal(v)) h=Math.max(h,depth(T,v)); } return h; }

23 The Trees Abstract Data Type Example Height Algorithm 1

24 Basic Algorithms on Trees Depth and Height public int height2(SimpleTree T; Position v){ if(T.isExternal(v)) return 0; } else { int h=0; Enumeration children_of_v= T.children(v); while(children_of_v.hasMoreElements()){ Position w=(Position)children_of_v.nextElement(); h=Math.max(h,T.height2(w)); } return 1+h; }

25 Preorder Traversal Definition A traversal of a tree T is a systematic way of accessing, or “visiting” all the nodes of T. In a preorder traversal of a tree T, the root of T is visited first and then the subtree rooted at its children are traversed recursively.

26 Preorder Traversal Algorithm Algorithm preorder(T,v) Visit(v) for each child w of v do preorder(T,w)

27 Preorder Traversal Preorder Print public void preorderPrint(SimpleTree T,Position v){ System.out.println(T.element(v)); Enumeration children_of_v=T.children(v); while(children_of_v.hasMoreElements()){ Position w=(Position)children_of_v.nextElement(); preorderPrint(T,w); }

28 Postorder Traversal Definition Recursively traverses the subtrees rooted at the children of the root first, and then visits the root.

29 Preorder Traversal Algorithm Algorithm postorder(T,v) for each child w of v do preorder(T,w) Visit(v)


Download ppt "Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees."

Similar presentations


Ads by Google