Nonrecursive Trees - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Nonrecursive Trees

Description:

Bubbling up: idea: add an empty node, called the vacancy. ... Bubbling down: ... Bubbling down: If the vacancy does not have a right child it is either: ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 32
Provided by: fduu8
Category:

less

Transcript and Presenter's Notes

Title: Nonrecursive Trees


1
Non-recursive Trees
2
Non-recursive Trees
  • There are times when the recursive definition of
    a tree puts us at a disadvantage.
  • We can, if we want, define a tree in a
    non-recursive manner.
  • For example, if we want to build a tree
    level-by-level then a non-recursive definition is
    easier to work with.

3
Full and Complete Trees
  • A tree that is built level-by-level from left to
    right is called a full tree. If the bottom-most
    level is completely filled it is called a
    complete tree.

1
2
3
5
4
6
7
8
4
Non-recursive Trees
  • A full tree is built level-by-level and it is
    often necessary to go up the tree.
  • Because of this, such trees are not viewed
    recursively.
  • A non-recursive tree can be implemented in
    several ways
  • with extra pointer going back to parent,
  • with a "thread" running through all the nodes at
    each level starting at the root and going
    left-to-right through each level
  • using an array.

5
Non-recursive Trees
  • Let's describe a non-recursive tree definition.

6
Nonrecursive Trees
typedef unsigned int Position template ltclass
Itemgt class NonrecursiveTree public Nonr
ecursiveTree() NonrecursiveTree() bool
is_empty () const bool left_child_exists
(Position node) const bool right_child_exists
(Position node) const bool has_children
(Position node) const bool has_parent
(Position node) const
7
Nonrecursive Trees (notes)
We use the type definition typedef unsigned
int Position to specify a node by its position
in the natural order shown a moment ago.
8
Nonrecursive Trees (notes)
Note how we now talk about children rather than
subtrees. We now think of the tree
non-recursively. bool left_child_exists
(Position node) const bool right_child_exists
(Position node) const bool has_children
(Position node) const Note how we now can think
about going "up" the tree as well as
down. bool has_parent (Position node) const
9
Nonrecursive Trees
Item operator (Position node)
const Position left_child (Position node)
const Position right_child (Position node)
const Position parent (Position node)
const Position root_node () const Position
last_node () const
10
Nonrecursive Trees (notes)
We can talk about the value at ANY node since we
are viewing a tree as a collection of
nodes. Item operator (Position node)
const Position left_child (Position node)
const Position right_child (Position node)
const Position parent (Position node)
const Position root_node () const We can even
look at the last node entered into the tree which
is useful when working with full
trees. Position last_node () const
11
Nonrecursive Trees
int height () const int node_count ()
const bool add_new_node () bool
delete_last_node () void destroy_tree ()
private
12
Nonrecursive Trees (notes)
I've left these in so we can see non-recursive
definitions int height () const int
node_count () const Now we add and delete node
by node. To keep our trees complete, we only
allow adding after the last node and deleting the
last node. bool add_new_node () bool
delete_last_node () void destroy_tree ()
private
13
Vector implementation
0 1 2 3 4 5
root
positions
left child of root
right child of root
left child of position 1 node
left child of node at position n is at position 2n
right child of position 1 node
right child of node at position n is at position
2n1
14
Nonrecursive Trees
private VectorltItemgt tree
15
Nonrecursive Trees
template ltclass Itemgt NonrecursiveTreeltItemgtNon
recursiveTree() Item dummy
tree.append(dummy) // empty node at index
0 template ltclass Itemgt NonrecursiveTreeltItemgt
NonrecursiveTree() template ltclass
Itemgt bool NonrecursiveTreeltItemgtis_empty ()
const return tree.size() 1
16
Nonrecursive Trees
template ltclass Itemgt bool NonrecursiveTreeltIt
emgtleft_child_exists (Position
node) const return 2node lt tree.size()
template ltclass Itemgt bool
NonrecursiveTreeltItemgtright_child_exists
(Position node) const return 2node
1 lt tree.size()
17
Nonrecursive Trees
template ltclass Itemgt bool NonrecursiveTreeltItemgt
has_children (Position node)
const return left_child_exists() templat
e ltclass Itemgt bool NonrecursiveTreeltItemgthas_p
arent (Position node)
const return node gt 1
18
Nonrecursive Trees
template ltclass Itemgt Item NonrecursiveTreeltItem
gtoperator (Position node)
const return treenode template ltclass
Itemgt Position NonrecursiveTreeltItemgtleft_child
(Position node) const return 2
node template ltclass Itemgt Position
NonrecursiveTreeltItemgtright_child
(Position node) const return 2
node 1
19
Nonrecursive Trees
template ltclass Itemgt Position
NonrecursiveTreeltItemgtparent (Positio
n node) const return node / 2 template
ltclass Itemgt Position NonrecursiveTreeltItemgtroo
t_node () const return 1 template
ltclass Itemgt Position NonrecursiveTreeltItemgtlas
t_node () const return tree.size() - 1
20
Nonrecursive Trees
template ltclass Itemgt int NonrecursiveTreeltIt
emgtheight () const int levels, nodes
for (levels 0, nodes 1 nodes lt
tree.size() nodes 2,
levels) return levels template
ltclass Itemgt int NonrecursiveTreeltItemgtnode_cou
nt () const return tree.size() - 1
21
Nonrecursive Trees
template ltclass Itemgt bool NonrecursiveTreeltItemgt
add_new_node() Item dummy return
tree.append(dummy) template ltclass
Itemgt bool NonrecursiveTreeltItemgtdelete_last_no
de() Item dummy return
tree.detach(dummy) template ltclass
Itemgt void NonrecursiveTreeltItemgtdestroy_tree()

22
Heapsort
  • Heapsort is based on trees in which the value at
    the root of any tree is smaller (larger) than all
    the value in any of its descendant nodes.
  • Like the Binary Insertion Tree sort, it consists
    of two phases
  • building the tree "bubbling up"
  • decomposing the tree "bubbling down"

23
Heapsort
  • Bubbling up
  • idea
  • add an empty node, called the vacancy.
  • the vacancy will eventually hold the value to be
    inserted.
  • the vacancy starts as the last leaf of the tree.
  • if the value at the parent of the vacancy is less
    than the new value, put the new value into the
    vacancy
  • if it is greater, put the parent's value in the
    vacancy and "bubble up" the vacancy to the parent
    node.
  • stop when location is found or we reach the root.

24
Heapsort
  • Bubbling down
  • more complicated than bubbling up.
  • smallest value is at the root, so process it.
  • the root now becomes vacant.
  • bubble the vacancy down until it becomes the last
    node of the tree. Delete that node.

25
Heapsort
  • Bubbling down
  • What makes this more complicated is that in
    trying to find the smallest remaining value in
    the tree, it might not be either the left or
    right child of the vacancy!
  • Consider the case on the next slide

26
Heapsort
vacancy
5
10
15
30
35
20
25
vacancy
27
Heapsort
  • Bubbling down
  • If the vacancy does not have a right child it is
    either
  • the parent of the last node of the tree
  • a leaf.
  • If it is a leaf the vacancy can not bubble down
    any more, but we know that the value in the last
    node will fit quite nicely there.

28
Heapsort
void bubble_up (CompleteTreeltintgt t, int
value) void bubble_down (CompleteTreeltintgt
t) int main() CompleteTreeltintgt tree int
value for (int i 0 i lt 100
i) value rand() 101 bubble_up
(tree, value) while (!tree.is_empty()) c
out ltlt treetree.root_node() ltlt
endl bubble_down (tree) return
EXIT_SUCCESS
29
Heapsort
void bubble_up (CompleteTreeltintgt t, int
value) Position vacancy t.add_new_node() v
acancy t.last_node() while (t.has_parent(vacan
cy) value lt t.value(t.parent(vacancy)))
tvacancy tt.parent(vacancy) vacancy
t.parent(vacancy) tvacancy value
30
Heapsort
static Position smallest (const
NonrecursiveTreeltintgt t, Position
vacancy) Position new_vacancy
t.last_node() int last_value
tt.last_node() if (t.has_children(vacancy))
if (!t.right_child_exists(vacancy)
tt.left_child(vacancy) lt
last_value) new_vacancy t.left_child(vacancy)
else if (t.right_child_exists(vacancy))
if (tt.left_child(vacancy) lt
last_value) new_vacancy t.left_child(vacancy
) if (tt.right_child(vacancy) lt
tnew_vacancy) new_vacancy
t.right_child(vacancy) return
new_vacancy
31
Heapsort
static void bubble_down (NonrecursiveTreeltintgt
t) Position vacancy int last_value
tt.last_node() vacancy t.root_node() whil
e (vacancy ! t.last_node()) int
position_of_smallest smallest(t,
vacancy) tvacancy tposition_of_smallest
vacancy position_of_smallest t.delete_la
st_node()
Write a Comment
User Comments (0)
About PowerShow.com