Title: Introduction to Trees
1Introduction to Trees
2Tree example
- Consider this program structure diagram as itself
a data structure.
main
readin
print
process
sort
lookup
3Terminology
- Node - an element of a tree
- Root node - the single node at level 0
- Child node - nodes at level 1 or lower
(descending from a parent node). Note a node
can have only one parent.
4Terminology (continued)
- Parent node - a node at one level associated with
nodes at a lower level (children). - Note nodes without children are called leaves.
- Every node of a tree is the root of a subtree.
5- As you can see by the diagram of a tree, its
structure is highly recursive. Therefore, as you
would expect, the procedures and functions that
manipulate trees are also recursive in nature.
6The tree as an ADT
- Fundamental operationsCreate(Root)Empty(Root)Fi
nd(Root,Target,TargetLoc)Add(Root,Node)Delete(Ro
ot,Node)Traverse(Root)
7Binary Trees
- This (next slide) is an example of a binary tree
(ie. one in which each node has no more than two
children)
8Binary tree example
9(see previous tree)
- This tree also has the property that each node is
greater than either of its children. Another
name for it is a heap (not to be confused with
the system heap).
10Question
- What is the hierarchical ordering principal for
this tree? - (see next slide)
11What is the ordering property?
12Answer
- Each node is greater than all of the nodes in its
left subtree and less than all of the nodes in
its right subtree. - This is called its ordering property.
13Trees representing arithmetic expressions.
- For example (A-B)(C(E/F))
14Example
15- compare this tree to a tree for the expression
(A-B)(CE/F)
16- Notice how small changes in the order of
operations (like removing a set of parentheses)
make major changes in the structure of the tree.
17Reverse Polish Again!?!
- Recall that expressions are easily evaluated
using a stack if they are available in postfix
form AB-CEF/ - Trees can be traversed in such a way as to
produce the postfix form. eg. a postorder
traversal yields the postfix form of the
expression. - More on this later.
18Implementing Binary Trees
- Two methods
- 1. Linear representation - array
- 2. Linked representation - pointers
19Linear representation (Depth3)
- 1. Allocate an array of size 2(d1)-1
- 2. Store root in location 1
- 3. For node in location n, store left child in
location 2n, right child in location 2n1
20(No Transcript)
21Tradeoffs
- 1. Fast access (given a node, its children and
parent can be found very quickly) - 2. Slow updates (inserts and deletions require
physical reordering) - 3. Wasted space (partially filled trees)
22Linked Representation
- Use records with pointer fields(A-B)(C(E/F))
23(No Transcript)
24Using an array of records
- The array implementation of this ADT looks like
this (next slide) - Insertions and deletions involve only
manipulation of pointers
25Array of structures
26Insertion (A-B)(P-C(E/F))
27Deletion (A-B)(P-E/F)
- The changes to the array of records look like
this after both of the operations (insertion of
-P and deletion of C) have been performed.
28(No Transcript)
29(No Transcript)
30Tradeoffs
- 1. Faster updates
- 2. Extra memory for pointers
- 3. Wasted space for nil pointers (can be
corrected by threading) - 4. Difficult to determine a node's parent
(corrected by adding a parent pointer)
31Traversing Binary Trees
- A tree traversal visits each node of a tree
exactly once
32Modes of traversal
- When visiting a node we have three choices
- 1. Process the data
- 2. Remember location of the current node and
process nodes in the left subtree - 3. Remember location of the current node and
process nodes in the right subtreeThe choice
made dictates the order of node processing for
the entire tree
33Example tree (T)
34Steps for a Preorder traversal of a binary tree
- 1. Process root
- 2. Do a preorder traversal of the nodes in the
root's left subtree(recursively) - 3. Do a preorder traversal of the nodes in the
root's right subtree (recursively)
35- Assume the we have a procedure called Process
that simply writes out the data contained in any
given node. - Then, using process, a preorder traversal of tree
T produces ABC/EF (the prefix form of
(A-B)C(E/F))
36Steps for an Inorder traversal of a binary tree
- 1. Recursively visit left subtree
- 2. Process root
- 3. Recursively visit right sub-tree
37Steps for a Postorder traversal of a binary tree
- 1. Recursively visit left subtree
- 2. Recursively visit right subtree
- 3. Process Root
38- A postorder traversal of T produces AB- CEF/
(the postfix form) - These traversals can be done on any binary tree,
not just expression trees.
39Implementing lists using binary trees
40Implementing the List ADT
- Vehicle Advantage Disadvantage
- Array Fast search Slow inserts
O(log2N) and deletions
O(N) - Linked Fast inserts Slow search List and
deletions O(N) O(1) - Binary Fast inserts Tree must be
Tree and deletions balanced O(log2N)
The hierarchical relationship we employed
is the ordering property.
41Example tree T1
42- Inorder traversal of this tree gives
ADEFGHKMNPSTUVZSo An inorder traversal of a
binary tree with the ordering property visits the
nodes in ascending order.
43- Adding a node to a binary tree with the ordering
property - Follow the insertion rule "If less than, go
left, otherwise go right."
44Example Add the letter R to the previous tree.
- 1. Compare R to M (the root). Since R M,
add R to the right subtree - 2. Compare R to T (the root of the subtree).
Since R - 3. Compare R to P. Since R P go right
- 4. Compare R to S. Since R
- 5. Since the left subtree is NULL add R at
this pointClearly, the procedure is
recursive.
45Adding R to the tree
46Efficiency of adding to a binary tree
- Question How many comparisons did we have to
make?Answer 4 (ie. depth 1)
47Differing tree configurations
- Consider another tree T2 representing the same
list
48(No Transcript)
49Question
- How many comparisons do we have to make to add
the letter R?Answer 9 (ie. depth 1)
50Question
- How do you minimize the depth of a binary
tree?Answer Make sure it is full, or balanced.
51Definition
- A balanced binary tree is one in which every node
above level depth-1 has exactly two children.
52Depth to size comparisons
- When a binary tree of N nodes is balanced, no
branch will be longer than log(base 2)N1. Thus,
the efficiency of the adding algorithm is
O(log(base 2)N) - Depth MaxN
- 0 1 1 3 2 7 3 15 . . .
9 1023 10 1 million
53Suppose we add Q to T1
54Suppose we add Q to T1
- Now T1 is not balanced.
- There are methods for balancing trees.
55Recall the binary search of an array
56(No Transcript)
57Binary similarities
- Each probe into the array is analogous to a
recursive descent in the search of a binary tree.
(both are O(log2N))
58Deleting from a tree
59For example, remove T from tree T1.
60Deletion strategies
- Requirement The resulting tree must have the
ordering property. - One method is to find the largest item in T1's
left sub-tree and replace T with it.
61Three special cases which might occur
- 1. Node to be deleted has a left child
- 2. Node to be deleted has a right child but
no left child - 3. Node to be deleted has no children
62Case 1
- Suppose P points to the node to be deleted. There
are two important subcases - 1.a P-Left has no right child
- 1.b P-Left has a right child
63Case 1a
64Case 1b
65Case 2
66Case 3
67List comparison
- Tradeoffs Linked List Binary Tree
- adding O(1) O(1) deleting searching O(N) O(
log2N) balanced O(N) worst case - user 1 ptr/node 2 ptrs/node memory (many
NULL) - system stack minimal recursion (remedy -
threading)
68General Trees
69General Trees
- So far we have been studying binary trees,
general trees however can have more than two
children for each node.Question Does the
former restriction to binary trees mean that we
cannot apply what we have learned to general
trees?Answer No! As long as we are willing
to convert general trees to binary ones.
70(No Transcript)
71Binary version of general tree
72Preorder Traversal
- (Root, Left, Right)
- J B D N E P Q K L M C X R T
73Processing order
- 1. Process root2. Recursively traverse the
left-most subtree3. Recursively traverse the
next subtree to the right4. Repeat step 3
until there are no more subtrees
74Postorder Traversal
- (Left, Right, Root)
- Q P E N D L X R C T M K B J
75- Note that this order on the general tree can be
described by1. Start at the leaves and work
up2. Process a node after all nodes below it
have been processed