Title: CSECE 268 DATA STRUCTURE
1CS/ECE 268 DATA STRUCTURE
- SOUNDARARAJAN EZEKIEL
- DEPARTMENT OF COMPUTERSCIENCE
- OHIO NORTHERN UNIVERSITY
2Trees
- Productivity experts says that breakthrough come
by thinking nonlinearity - one of the most important nonlinear data
structure in computing -- tree - It is a breakthrough in data organization
- algorithms are much faster than when using linear
data structure such as list, vector, and sequences
3- Tree also provide a natural organization for data
- file system
- graphical user interface
- database
- Web sites
- other computer systems
- we say trees are non-linear, we are referring to
an organizational relationship that is richer
than the simple before and after
relationship between the objects in sequences
4- The relationship in a tree are hierarchical---
with some objects being above and some below
others - Actually the main terminology for tree data
structure comes from family trees, with the terms
parents child, ancestor, and descendant
being the most common words used to describe
relationship - We show an example of a family tree from Holy
Bible
5Example Genesis Chapter 25-36
Eldaah Abida Hanoch Epher Ephah Dedan Sheba
Shuah Ishbak Midian Medan Jokshan Zimran Ishama
el Isaac
Eliphaz Revel Jeusuh Jalam Korah
Nebaioth Kedar Adbeel Mibsam Mishma Dumah Massa Ha
dad tema Jetur Naphish Jedemah
Benjamin Joseph Dinah Zebulun Issachar Asher Gad N
aphtali Dan Judah levi Simeon Reuben
Abraham
Esau Jacob(Israel)
6The tree ADT(abstract data type)
- A tree is an ADT that stores elements
hierarchically - With exception of the top element, each element
in a tree has a parent element and zero or more
children elements - tree is visualized by placing elements inside
ovals or rectangles, and drawing the connections
between parents and children with straight lines - We typically call the top element the root of the
tree, but is drawn as the highest element, with
the other element being connected below
7Example
ONU RUS
Manu
Sales
purchase
RD
TV
CD
computer
Internat
Domestic
Overseas
Canada
S.Ameri
Africa
Europe
Asia
Australia
8Terminology and Basic Properties
- A tree T is a set of nodes storing elements in a
parent-child relationship with the following
properties - T has a special node r, called the root of T
- Each node v of T different from r has a parent
node u
9Caution
- Note that according to the above definition, a
tree cannot be empty, since it must have at least
one node, the root - Some people allow the definition to include the
empty tree, but we adopt the convention that a
tree always has a root so as to keep our
presentation simple and to avoid having to always
deal with the special case of an empty tree in
our algorithm
10More Terminology
- if 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 sibling - a node is external if it has no children and it
is internal if it has one or more children - external nodes are also known as leaves
- the subtree of T rooted at a node v is the tree
consisting of all the descendents of v in
T(including v itself)
11- An ancestor of a node is either the node itself
or an ancestor of the parent of the node - Conversely we say that a node v is a descendent
of a node u if u is an ancestor of v - Example Sales is an ancestor of Europe is a
descendent of Sales
12Example 1
- The inheritance relation between classes in a
Java program forms tree. The class.java.lang.Objec
t is an ancestor of all other classes
13Example 2
- In most operating systems, files are organized
hierarchically into nested directories( also
called folders), which are presented to the user
in the form of a tree - More specifically, the internal nodes of the tree
are associated with directories and the external
nodes are associated with regular files - In the UNIX OS the root of the tree is
appropriately called the root directory and is
responded by the symbol /. It is the ancestor
of all directories and files in a UNIX file system
14/user/rt/courses/
CS 268
CS 365
Prog
HW
Projects
grades
grades
demos
papers
pr3
hw1
pr2
pr1
hw2
hw3
perfor
pipe
data
15Terminology
- A tree is ordered if there is a linear ordering
defined for the children of each node that is,
we can identify children of a node as being the
first, second, third, and so on.
16Example 3
- A structural document, such as book, is
hierarchically organized as a tree whose internal
nodes are chapters, sections, and subsections,
and external nodes are paragraph, tables,
figures, the bibliography and so on.. - We expand the tree further to show paragraph
consists of sentences, sentences consists of
words, and words consists of characters - In any case, such a tree is an example of an
ordered tree, because there is well-defined
ordering among the children of each node
17Binary tree
- a binary tree is an ordered tree in which every
node has at most two children - A binary tree is proper if each node has either
zero or two children - Thus, in a proper binary tree, every internal
node has exactly two children - For each internal node in a binary tree, we label
each child as either being a left child or a
right child. - These children are ordered so that a left child
comes before the a right child
18- The subtree rooted as a left or right child of an
internal node v is called a left subtree or right
subtree respectively of v - Binary trees have a number of useful applications
- we will discuss two common uses in the example
below
19Example Investment Advise
Are you nervous?
Yes
NO
Saving account
Will you need to access most of the money within
next 5 years?
NO
Yes
Are you willing to accept risk in exchange for
higher expected return?
Money market fund
NO
Yes
Diversified portfolio with stocks, bonds and
short tern instruements
Stock portfolio
20Example Arithmetic Expression
- An arithmetic expression can be represented by a
tree whose external nodes are associated with
variables or constants, and whose internal nodes
are associated with one of the operators, , -,
x, and / - Each node in such a tree has a value associated
with it - If node is external, then its value is that of
its variable or constant
21- if a node is internal, then its value is defined
by applying its operation to the values of its
children - such an arithmetic expression tree is a proper
binary tree, since each of the operators , -, x,
and / take exactly two operands. - Of course, if we were to allow for unary
operators like (-) negation, as in (-x) then we
could have an improper binary tree
22 ((((31)x3)/((9-5)2))-((3x(7-4))6))
-
/
6
x
x
-
3
3
-
2
1
3
9
5
7
4
23Tree methods
- The tree ADT stores elements at positions, which,
as with positions in a list, are defined relative
to neighboring positions. - The positions in a tree are its nodes, and
neighboring positions satisfy the parent-child
relationships that define a valid tree. - we use the terms position and node
interchangeably for trees
24- element() Return the object at this position
Input None, Output Object - the real power of node positions in a tree,
however, comes from the accessor methods of the
tree ADT that return and accept positions, such
as the follwoing
25- root() Return the root of the tree Input
None, Output Position - parent(v) Return the parent of node v, an error
occurs if v is not root, Input Position
Output Position - children(v) Return an iterator of the children
of node v. Input Position, Output Iterator of
positions
26Iterator
- A typical computation on a vector, list, or
sequence is to march through its elements in
order, one at a time, for example, to look for a
specific element - An iterator is asoftware design pattern that
abstracts the process of scanning though a
collection of elements one element at a time. An
iterator consists of a sequence S, a current
position in S, and a way of stepping to the next
position in S and making it the current position
27(No Transcript)