Title: Trees
1Trees
2Example Tree
3Formal Definition (from Googles definetree)
- In graph theory, a directed acyclic graph (DAG)
in which the root node has no parent and every
other node has exactly one parent.www.mindspring.
com/scarlson/tc/glossary.htm - In computer science, a tree is a widely-used
computer data structure that emulates a tree
structure with a set of linked nodes. Each node
has zero or more child nodes, which are below it
in the tree (in computer science, unlike in
nature, trees grow down, not up). A node that has
a child is called the child's parent node. A
child has at most one parent a node without a
parent is called the root node (or root). Nodes
with no children are called leaf nodes.
en.wikipedia.org/wiki/Tree_(computing)
4Tree Terminology
Root A
Leaf D, E, G, J, K, L
Non-leaf A, B, C, F, H
Subtree ( of F ) F, G, H, J, K, L
Natural Order A, B, D, E, C, F, G, H, J , K, L
Level (of F) 2
5Uses of Trees
- Paragraph Numbering in a Document
- Family (aSexual reproduction!)
- File system e.g. Unix, Dos
- Classification e.g. Linaus, Dewey
- Hierarchical Database e.g. IBMs MIS, LDAP
- Web site structure
- Hierarchical Menu
- Organisational Hierarchy
- Class Hierarchy in Java
- Assembly / Part hierarchy
- Document Object Model
- XML
- B-tree for efficient indexing
6Legal Numberingin a document
In natural order with A as root 1 B 1.1 D 1.2 E 2. C 2.1 F 2.1.1 G 2.1.2 H 2.1.2.1 J 2.1.2.2 K 2.1.2.3 L
7Family TerminologyFor object F
Parent (at most 1) C
Children G, H
Siblings (none)
Descendents G, H, J, K, L
Ancestors C, A
Node and Descendents F, G, H, J, K, L
8File system
- File system in Unix or Windows
- C\Program Files\eXist\webapp\admin\admin.xql
- Actually this is a Forest of trees, each drive
a separate Tree
9Folder Hierarchy
10Navigation in File System(from F)
Root /
Current directory (context) .
Child G G (or ./G)
Parent ..
Sibling (none)
Cousin ../../B/E
Absolute Path /A/C/F
Leaf name File
Node name Directory or Folder
11Taxonomy
- Animal Kingdom
- Home Kingdom Animalia Phylum Arthropoda
Class Insecta Order Lepidoptera Suborder
Macrolepidoptera Family Nymphalidae Species
Vanessa atalanta - Knowledge
- Dewey Decimal Classification
- Finding Butterflies
12Type (A Kind of) Hierarchyrelative to F
supertype C
subtype G, H
G and H would inherit all the attributes and
methods of F and define additional attributes and
methods of their own. A type hierarchy allows
sharing of definitions. see Normalisation Easi
ly confused with an Object Hierarchy or
Whole/Part hierarchy.
13Object Hierarchy
- An object hierarchy supports one or more of a
number of kinds of relationship. - Containment
- One object is contained within, is a part of,
a parent object. A containment relationship is
used to move, copy or delete a whole subtree. - Scoping
- Names of objects must be unique within some
scope. Often these scopes are hierarchical. The
name of a child is unique within the scope of the
parent. - Delegation
- A child object delegates responsibility for a
property to its parent, and so on up the
hierarchy until an object defines the property. - Aggregation
- A parent object has properties determined by the
properties of its children. For example, the
value of the cost in a non-leaf would be
defined as the sum of cost values over all
children, with actual cost values defined at the
leaves. Min, max, average are other aggregate
relationships. - Distribution
- A parent property is distributed over the
children for example a budget value defined in
the parent is distributed over the children.
14HTML Document (Object Tree)
lthtmlgt ltheadgt lttitlegtSample
Documentlt/titlegt lt/headgt ltbodygt
lth1gtAn HTML Documentlt/h1gt ltpgtThis is
a ltigtsimplelt/igt document. lt/pgt lt/bodygt
lt/htmlgt
From Flanagan,D. (2002) JavaScript The Definitive
Guide , OReilly on-line
15DOM Class Tree
From Flanagan,D. (2002) JavaScript The Definitive
Guide , OReilly on-line
16 XPATH DOM
Declarative Procedural (navigational)
Root / document
Context Node Current Directory (self) .
Local Name n
Child 1 last() n.childNodes n.firstChild n.lastChild
Parent .. f.parentNode
Sibling ../g ../ f.nextSibling f.previousSibling
Attribute f/_at_att f.getAttribute(att)
Locate node by ID //_at_id'f1' documents.getElementByID('f1')
Locate nodes by Tag //img document.getElementsByTagName('img')
Number of nodes in sequence s.length s.length
Select by predicate _at_size 10 and . 'fred'
17Exercise
18Exercises(based on hoax.xml)
- Write XPath expressions to locate the following
- The recipe title
- All recipe ingredients
- The amount of brown sugar
- The last instruction
- Write DOM code to access the same data