Docsity

Trees - Algorithms and Applications in Java - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Trees - Algorithms and Applications in Java - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Trees

Nature Lover’s View Of A Tree

root

branches

leaves

Linear Lists And Trees

• Linear lists are useful for serially ordered data.

 (e

0

, e

1

, e

2

, …, e

n-

 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.

 Java’s classes.

  • Object is at the top of the hierarchy.
  • Subclasses of Object are next, and so on.

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.

• Elements that have no children are leaves.

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.

Subtrees

Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException root

Parent, Grandparent, Siblings, Ancestors, Descendants Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException

Level 4 Level 3 Level 2

Levels

Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException Level 1

height = depth = number of levels

Level 3 Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException Level 4 Level 2 Level 1

Node Degree = Number Of Children

Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException

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.

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.

Arithmetic Expressions

• (a + b) * (c + d) + e – f/g*h + 3.

• Expressions comprise three kinds of entities.

 Operators (+, -, /, *).

 Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d),

etc.).

 Delimiters ((, )).

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