Download presentation
Presentation is loading. Please wait.
1
University of Palestine
Trees
2
Trees Outline Introduction. The Trees Abstract Date Type
Basic Algorithms on Trees
3
Trees Introduction 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.
4
Trees Introduction The main terminology for tree data structures comes from family trees, with the term “parent”, “child”, “ancestor”, and “descendent”.
5
اللهم صلي على سيدنا محمد
Trees Example Prophets tree [1] اللهم صلي على سيدنا محمد [1]
6
The Trees Abstract Data Type
Definition 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. root
7
The Trees Abstract Data Type
Terminology and Basic properties 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.
8
The Trees Abstract Data Type
Terminology and Basic properties root Parent 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
Terminology and Basic Properties 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.
12
The Trees Abstract Data Type
Terminology and Basic Properties
13
The Trees Abstract Data Type
Terminology and Basic Properties 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
14
The Trees Abstract Data Type
Terminology and Basic Properties Example of a binary tree.
15
The Trees Abstract Data Type
Terminology and Basic Properties Binary trees applications: Decision trees: Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining
16
The Trees Abstract Data Type
Terminology and Basic Properties 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))
17
The Trees Abstract Data Type
Trees ADT methods 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)
18
The Trees Abstract Data Type
A Tree Interface in Java int size() boolean isEmpty() Enumeration elements() Enumeration positions() Object replace (Position p, Object o) void swap(Position v, Position w) PositionalContainer <<interface>> SimpleTree <<interface>> 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
Height Algorithm 1 Example
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
Postorder Traversal Algorithm Algorithm postorder(T,v)
for each child w of v do preorder(T,w) Visit(v)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.