Download presentation
Presentation is loading. Please wait.
Published bySiska Lesmono Modified over 6 years ago
1
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
2
Nature Lover’s View Of A Tree
leaves branches root
3
Computer Scientist’s View
root leaves branches nodes Branches meet at nodes.
4
Trees Another Abstract Data Type (ADT)
Simulates a hierarchical tree structure Has no cycles Two vertices are connected by exactly one path Recursively defined as a collection of nodes, each node has a value and a list of references to the children No reference is duplicated None points to root [ [
5
Tree [
6
Not Tree Top left: Undirected cycle Top right: Cycle
Bottom left: Cycle Bottom right: two non-connected components [
7
Linear Lists And Trees Linear lists are useful for serially ordered data. (e0, e1, e2, …, en-1) Days of week. Months in a year. Students in this class. Trees are useful for hierarchically ordered data. Employees of a corporation. President, vice presidents, managers, and so on. Continent, country, state, city North America, USA, FL, Gainesville
8
Hierarchical Data And Trees
The element at the top of the hierarchy is the root. Elements next in the hierarchy are the children of the root. Elements next in the hierarchy are the grandchildren of the root, and so on. (descendants) Elements that have no children are leaves. (external nodes) Internal node: node with at least one child
9
Example Tree root President VP1 VP2 VP3 Manager1 Manager2 Manager
Worker Bee children of root grand children of root great grand child of root
10
Definition A tree t is a finite nonempty set of elements.
One of these elements is called the root. The remaining elements, if any, are partitioned into trees, which are called the subtrees of t.
11
Subtrees root President VP1 VP2 VP3 Manager1 Manager2 Manager
Worker Bee
12
Leaves President VP3 VP1 VP2 Manager Manager1 Manager2 Manager
Worker Bee
13
Parent, Grandparent, Siblings, Ancestors, Descendants
President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee
14
Levels Level 1 President VP1 VP2 VP3 Manager1 Manager2 Manager
Worker Bee Level 2 Level 3 Level 4
15
Caution Some texts start level numbers at 0 rather than at 1.
Root is at level 0. Its children are at level 1. The grand children of the root are at level 2. And so on. We shall number levels with the root at level 1.
16
height = depth = number of levels
President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee Level 1
17
Node Degree = Number Of Children
President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee 3 2 1 1 1
18
Tree Degree = Max Node Degree
President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee 3 2 1 Note that it is possible to have a tree whose degree is (say) 3 and whose root has a degree that is < 3. Degree of tree = 3.
19
Binary Tree Finite (possibly empty) collection of elements.
A nonempty binary tree has a root element. The remaining elements (if any) are partitioned into two binary trees. These are called the left and right subtrees of the binary tree.
20
Differences Between A Tree & A Binary Tree
No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. A binary tree may be empty; a tree cannot be empty.
21
Differences Between A Tree & A Binary Tree
The subtrees of a binary tree are ordered; those of a tree are not ordered. a b Are different when viewed as binary trees. Are the same when viewed as trees.
22
Arithmetic Expressions
(a + b) * (c + d) + e – f/g*h Expressions comprise three kinds of entities. Operators (+, -, /, *). Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d), etc.). Delimiters ((, )).
23
Operator Degree Number of operands that the operator requires.
Binary operator requires two operands. a + b c / d e - f Unary operator requires one operand. + g - h
24
Infix Form Normal way to write an expression.
Binary operators come in between their left and right operands. a * b a + b * c a * b / c (a + b) * (c + d) + e – f/g*h
25
Operator Priorities How do you figure out the operands of an operator?
a + b * c a * b + c / d This is done by assigning operator priorities. priority(*) = priority(/) > priority(+) = priority(-) When an operand lies between two operators, the operand associates with the operator that has higher priority.
26
Tie Breaker When an operand lies between two operators that have the same priority, the operand associates with the operator on the left. a + b - c a * b / c / d
27
Delimiters Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. (a + b) * (c – d) / (e – f)
28
Infix Expression Is Hard To Parse
Need operator priorities, tie breaker, and delimiters. This makes computer evaluation more difficult than is necessary. Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters. So it is easier for a computer to evaluate expressions that are in these forms.
29
Postfix Form The postfix form of a variable or constant is the same as its infix form. a, b, 3.25 The relative order of operands is the same in infix and postfix forms. Operators come immediately after the postfix form of their operands. Infix = a + b Postfix = ab+
30
Postfix Examples Infix = a + b * c Infix = a * b + c
Infix = (a + b) * (c – d) / (e + f) Postfix = a b + c d - * e f + /
31
Unary Operators Replace with new symbols. + a => a @
+ a + b => b + - a => a ? - a-b => a ? b - @ and ? are new symbols that are used to denote the unary + and – operators, respectively.
32
Postfix Evaluation Scan postfix expression from left to right pushing operands on to a stack. When an operator is encountered, pop as many operands as this operator needs; evaluate the operator; push the result on to the stack. This works because, in postfix, operators come immediately after their operands.
33
Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + /
stack a b + c d - * e f + / a b + c d - * e f + / b a
34
Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + /
stack a b + c d - * e f + / d a b + c d - * e f + / c a b + c d - * e f + / (a + b) a b + c d - * e f + / a b + c d - * e f + /
35
Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + /
stack a b + c d - * e f + / (c – d) (a + b)
36
Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + /
stack
37
Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + /
stack
38
Prefix Form The prefix form of a variable or constant is the same as its infix form. a, b, 3.25 The relative order of operands is the same in infix and prefix forms. Operators come immediately before the prefix form of their operands. Infix = a + b Postfix = ab+ Prefix = +ab
39
Binary Tree Form + a b a + b - a - a
Each leaf represents a variable or constant. Each nonleaf represents an operator. The left operand (if any) of this operator is represented by the left subtree, the right subtree represents the right operand of the operator. Binary Expression Tree [
40
Binary Tree Form (a + b) * (c – d) / (e + f) / + a b - c d e f *
41
Merits Of Binary Tree Form
Left and right operands are easy to visualize. Code optimization algorithms work with the binary tree form of an expression. Simple recursive evaluation of expression. + a b - c d e f * / Evaluate an expression– if there is no root, return null; if the root is a leaf, return the value of variable or constant in the leaf; otherwise, evaluate the left and right operands recursively; compute the root operator; return the result.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.