Download Trees and Tree Traversals

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Quadtree wikipedia , lookup

Lattice model (finance) wikipedia , lookup

B-tree wikipedia , lookup

Red–black tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Tree Traversals, TreeSort
20 February 2003
Expression Tree
Leaves are operands
Interior nodes are operators
A binary tree to represent (A - B) + C * (E - F)
+
–
A
2
*
B
–
C
E
F
Formal Definition of Tree



3
An empty structure is an empty tree.
If t1, t2, …, tk are disjoint trees, then the
structure whose root has as its children the
roots of t1, t2, …, tk, is also a tree.
Only structures generated by rules 1 and 2
are trees.
Observations


4
The recursive fashion in which a tree is defined
provides a strong hint that most tree
processing algorithms will also be recursive.
Most operations on the tree data structure are
closely linked to the hierarchical relationship
among nodes for that particular tree.
Tree Properties






5
The minimal tree is an empty tree and has no nodes.
If not empty, a tree consists of nodes and branches.
Each node must be reachable from the root through a
unique sequence of branches, called a path.
The number of branches in a path is the length of the
path.
The level of a node is the length of the path from the
root to the node plus one, which is the number of
nodes in the path.
The height of a nonempty tree is the maximum level of
a node in the tree. [The empty tree has height 0 (by definition),
and a single node is a tree of height 1. This is the only case in
which a node is both the root and a leaf.]
Linked Implementation of a Binary Tree
Since each node in a binary tree has at most two
children, a node in a linked implementation has
two pointer members, one for each child, and
one or more members for storing data.
+
–
A
6
*
B
–
C
E
F
Tree Traversals



7
Move through all the nodes in a tree visiting
them one at a time.
For lists we traverse nodes by visiting
successors.
We have more freedom in traversing trees.
Preorder Binary Tree Traversal
In a preorder traversal, the nodes will be visited
in the following order:
First, process the root node.
2. Then, recursively visit all nodes in the left subtree.
3. Finally, recursively visit all nodes in the right
subtree.
The traversal method works recursively, that is, once the
root of the tree is processed, we go to the root of the left
subtree, and then to the root of the left subtree of the left
subtree, and so on until we can go no farther.
1.
8
+ – A B * C – E F (prefix form)
Inorder Binary Tree Traversal
The inorder traversal of a binary tree visits nodes
in the following order.
1.
2.
3.
First, recursively visit all nodes in the left subtree.
Then, process the root node.
Finally, recursively visit all nodes in the right
subtree.
A–B+C*E–F
(infix form)
9
Postorder Binary Tree Traversal
The postorder traversal of a binary tree visits the
nodes in the following order:
1.
2.
3.
First, recursively visit all nodes in the left subtree
of the root node.
Then, recursively visit all nodes in the right
subtree of the root node.
Finally, process the root.
AB–CEF–*+
(postfix form)
10
General Trees




So far we have considered binary trees.
There are many applications with hierarchical
relationships in which a parent may have an
unrestricted number of children.
A tree that represents such structures is called
a general tree.
For example, consider the general tree of the
next slide.
–
11
In this structure, A1 is the first child of A, with A2, A3,
and A4 as A1’s siblings. Similarly, A11 is the first child
of A1, with A12 and A13 as A1’s siblings
A General Tree
A
A1
A11
A12
A2
A13
A3
A31
A4
A32
A41
A411
12
A412
A413
Linked Representation



13
We can convert any general tree (or forest) into
a binary tree.
We can use the linked representation of the
converted binary tree to represent this general
tree.
Since only two pointers are associated with
each node of the binary tree, we do this by
defining one of the pointers to be a pointer to
the leftmost child of a node in the general tree,
while the other pointer is used to identify the
next sibling of that node in the general tree.
Converting any Forest to a Tree



14
Connect the roots of the trees via right links
As we descend the levels of the trees in the
forest, make the left pointer point to the first
child of a node and the right pointer of the first
child begins a linked list of that child’s siblings.
Rotate the entire structure by 45º
Example
1
2
15
3
5
4
6
7
8
9
10
11
Example (first child/sibling links)
1
5
X X
2
3
X
4
6
7
X
9
16
X
10
8
X
11
Example
1
2
17
3
5
4
6
7
8
9
10
11
Example (rotate 45º)
1
2
5
6
3
7
4
8
9
10
11
18
Tree Sort

Algorithm:
–
–
–
–
19
create empty BST
insert (ordered) each element into BST
Inorder traverse BST
(destroy BST)
Tree Sort

Advantages:
–
–

Disadvantages:
–
–
20
n elements, lg(n) insert  O(nlg(n)) sort
don’t need to have a fixed set of data, nodes can be
inserted and deleted dynamically
additional overhead of entire Tree
if data arrives in order or reverse order degenerate
to O(n2) behavior just like QuickSort