Notes
October 31,2006
Happy
Halloween from Kimberly Hart
TREES
Binary tree
Generally implemented
using linked lists
Can be implemented as
an array –
Implementation of a tree element (What would a node look like in
this case?)
--Node and two pointers
--left pointer and a right pointer
Type link is new
access node;
Type node is record
value : integer;
left : link;
right : link;
What might you want to do with the tree? i.e. what operations
will have to be implemented?
--add values to the tree
--delete values from the tree
--search for a value in the tree
--Order the tree – generally done as you construct it!!!
--get parent, - not possible with our node
structure
--add parent – not possible with our
node structure
--Make empty
--Print the values in the tree starting at the
root. – called TRAVERSING the tree.
Parts of the tree
root-—has no
PARENT
every other
node has at most two children—0,1,or two children.
a node
with no children is a LEAF.
a node other than the root with 1 or 2 children is an
internal node
TRAVERSING THE TREE
Pre-order
Visit a node (i.e. do something with its value –
e.g. print it)
Traverse its left sub-tree
Traverse its right sub-tree
In-order
Traverse a node’s
left sub-tree
Visit the node (i.e. do something with its value –
e.g. print it)
Traverse the
node’s right sub-tree
Post-order
Traverse the
node’s left sub-tree
Traverse the
node’s right sub-tree
Visit the node (i.e. do
something with its value – e.g. print it)
IF we stored the tree in an array, here’s what the array
structure would look like
Array position |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
Etc |
|
Root |
Left child of root |
Right child of root |
Left child of left child of root |
Right child of left child of root |
Left child of right child of root |
Right child of right child of root |
|
Note that if a node is stored in position n, its children are stored in positions 2n and 2n+1
Other terms
--Binary tree means each node has at most two children
--Height –number of levels in a tree, start with 0
--Ancestor—someone further up the tree
--Descendant-below a given node in a tree
PERFECT BINARY TREE—a binary tree which all of the leaves are on
the same level and every non leaf has two children.
HEAP—binary tree implemented as an array
binary tree definition – (our
text – [age 630] )
A tree in which each node is capable of
having two child nodes, a left child node and a right child node.
Each subtree is a binary tree
Binary tree versus—binary search tree --- Search tree is
ordered!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
6 |
|
17 |
|
|
|
|
|
|
|
|
|
A binary search tree is a tree in which all of the values in the
left sub-tree of the root are smaller than the value in the root and all of the
values in the right sub-tree of the root are greater than the value in the root
and one where each of the sub-trees of the root is a binary search tree.
Start of
building a binary search tree
--check for empty tree
-- if empty—pointer points to new node
which becomes root of tree (or subtree)
--else
--if the value in the node n greater than root.value go right
--else go left
At each point follow the same pattern
Pre-Order DEPTH FIRST SEARCH—go down
Check root
If not empty—print value then check left st.
if not empty print value until hit empty subtree then
back up… (to be discussed further in class)
If you look at the binary tree in figure 10.3 on page 629, here are the three
traversals:
Pre-order would print the values in the following order:
A B D G
H E C
F I J
In-order would print the values in the following order:
G D H
B E A
I F J C
Post-order would print the values in the following order:
G H D
E F I
J F C A