stree Class Implementation - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

stree Class Implementation

Description:

return reference to current object. return *this; Insertion. Example: insert z = 32 ... tree is the address of the stree object associated // with this iterator. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 26
Provided by: toshi155
Category:

less

Transcript and Presenter's Notes

Title: stree Class Implementation


1
stree Class Implementation
Extend the tnode class to include a pointer to
the parent
important for iterator and --
left
parent
nodeValue
right
template lttypename Tgt class stnode
public // stnode is used to implement the
binary search tree class // making the data
public simplifies building the class
functions T nodeValue // node
data stnodeltTgt left, right, parent //
child pointers and pointer to the node's parent
// constructor stnode (const T item,
stnodeltTgt lptr NULL, stnodeltTgt
rptr NULL, stnodeltTgt pptr
NULL) nodeValue(item), left(lptr),
right(rptr), parent(pptr)
2
Pointers
50
12
65
5
53
3
Class stree (private section)
stnodeltTgt root // pointer to tree
root int treeSize // number of elements in
the tree stnodeltTgt getSTNode(const T
item,
stnodeltTgt lptr,stnodeltTgt rptr, stnodeltTgt
pptr) // allocate a new tree node and return
a pointer to it. // if memory allocation
fails, the function throws the //
memoryAllocationError exception stnodeltTgt
copyTree(stnodeltTgt t) // recursive function
used by copy constructor and assignment //
operator to assign the current tree as a copy of
another tree void deleteTree(stnodeltTgt
t) // recursive function used by destructor
and assignment // operator to delete all the
nodes in the tree stnodeltTgt findNode(const T
item) const // search for item in the tree.
if it is in the tree, // return a pointer to
its node otherwise, return NULL. // used by
find() and erase()
4
BST Assignment
template lttypename Tgt streeltTgt
streeltTgtoperator (const streeltTgt rhs) //
can't copy a tree to itself if (this
rhs) return this // erase the existing
tree nodes from memory deleteTree(root) //
copy tree rhs into current object root
copyTree(rhs.root) // set the tree
size treeSize rhs.treeSize // return
reference to current object return this
5
Insertion
Example insert z 32
x
25
Compare 32 and 25 traverse the right subtree
20
35
40
12
insert 32 as left child
Compare 32 and 35, traverse the left subtree
y
y
x
z
6
Implementation of insert()
template lttypename Tgt pairltiterator, boolgt
insert(const T item) // t is current node in
traversal, parent the previous node stnodeltTgt t
root, parent NULL, newNode // terminate
on on empty subtree while(t ! NULL) //
update the parent pointer. then go left or
right parent t // if a match occurs,
return a pair whose iterator // component
points at item in the tree and whose // bool
component is false if (item
t-gtnodeValue) return pairltiterator, boolgt
(iterator(t, this), false) else if (item lt
t-gtnodeValue) t t-gtleft else t
t-gtright
discussed later
7
contd
// create the new leaf node newNode
getSTNode(item,NULL,NULL,parent) // if parent
is NULL, insert as root node if (parent
NULL) root newNode else if (item lt
parent-gtnodeValue) //
insert as left child parent-gtleft
newNode else // insert as right child
parent-gtright newNode // increment
size treeSize // return an pair whose
iterator component points at // the new node
and whose bool component is true return
pairltiterator, boolgt (iterator(newNode, this),
true)
8
Successor and Predecessor
10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62
x
50
The successor of y is lowest ancestor whose left
child is y or an ancestor of y
30
55
53
60
25
35
10
62
31
37
The successor of x is the leftmost node in its
right subtree
20
The predecessor of x is the rightmost node in its
left subtree
no right subtree
23
9
erase() Deletion
pNodePtr dNodePtr-gtparent
10
Deletion (A)
Case A node D (to be deleted) is a leaf
node Operation update the parent node to have
an empty subtree.
rNodePtr NULL pNodePtr-gtleft rNodePtr
D
11
Deletion (B)
Case B node D has a left child but no right
child Operation attach the left subtree of D to
the parent
rNodePtr dNodePtr-gtright if (rNodePtr !
NULL) // the parent of R is now the parent
of D rNodePtr-gtparent pNodePtr
pNodePtr-gtleft rNodePtr
25
40
15
20
45
17
30
D
17
R
12
Deletion (C)
Case C node D has a right child but no left
child Operation attach the right subtree of D
to the parent
25
rNodePtr dNodePtr-gtright if (rNodePtr !
NULL) // the parent of R is now the parent
of D rNodePtr-gtparent pNodePtr
pNodePtr-gtleft rNodePtr
40
D
15
R
45
30
13
Deletion (D)
Case D node D has two children
Operation 1. Select as the replacement (of D)
node R, the successor of D. R has
the smallest value greater than
that of D.
2. Unlink R from the tree.
3. Connect Rs right subtree to its parent.
4. Finally, connect R at the deleted node.
root
D
A
B
R
E
C
14
Deletion (D) - an Example
10, 25, 28, 30, 33, 34, 35, 40, 50, 65
40
30
65
35
25
50
10
28
33
replacement of 30
34
15
Deletion (D) Special Case
Left subtree of the right child of D is empty.
D
R
C
// right child of deleted node is the
replacement. // assign left subtree of D to left
subtree of R rNodePtr-gtleft dNodePtr-gtleft //
assign the parent of D as the parent of
R rNodePtr-gtparent pNodePtr // assign the left
child of D to have parent R dNodePtr-gtleft-gtparent
rNodePtr pNodePtr-gtright rNodePtr
16
Deletion (D) General Case
Left subtree of the right child of D is not empty.
A sequence of descents to the left children to
reach R.
pOfR
// assigning its right subtree as the left child
of the parent of R pOfRNodePtr-gtleft
rNodePtr-gtright // the parent of the right
child of R is the parent of R if (rNodePtr-gtright
! NULL) rNodePtr-gtright-gtparent pOfRNodePtr
17
Case D Finishing up
// put replacement node in place of dNodePtr //
assign children of R to be those of
D rNodePtr-gtleft dNodePtr-gtleft rNodePtr-gtright
dNodePtr-gtright // assign the parent of R to
be the parent of D rNodePtr-gtparent
pNodePtr // assign the parent pointer in the
children // of R to point at R rNodePtr-gtleft-gtpar
ent rNodePtr rNodePtr-gtright-gtparent
rNodePtr
18
erase() Link to the Parent Node
// deleting the root node. assign new root if
(pNodePtr NULL) root rNodePtr // attach
R to the correct branch of P else if
(dNodePtr-gtnodeValue lt pNodePtr-gtnodeValue) pNod
ePtr-gtleft rNodePtr else pNodePtr-gtright
rNodePtr // delete the node from memory and
decrement tree size delete dNodePtr treeSize--
19
The stree Iterator
Why needed?
Successive elements in an inorder scan can be
widely separated from each other.
50
30
55
25
53
60
35
10
62
31
37
20
20
Class streeltTgtiterator
class iterator public //
constructor iterator () // comparison
operators. just compare node pointers bool
operator (const iterator rhs) const bool
operator! (const iterator rhs) const //
dereference operator. T operator () const
// prefix increment. move to next node
inorder iterator operator() // postfix
increment. iterator operator(int) //
prefix decrement. move to previous node
inorder iterator operator() // postfix
decrement. iterator operator(int)
21
contd
private // nodePtr is the current location
in the tree. we can move // freely about the
tree using left, right, and parent. // tree is
the address of the stree object associated //
with this iterator. it is used only to access
the // root pointer, which is needed for and
-- // when the iterator value is
end() stnodeltTgt nodePtr streeltTgt
tree // used to construct an iterator return
value from // an stnode pointer iterator
(stnodeltTgt p, streeltTgt t) nodePtr(p),
tree(t)
private constructor takes two arguments 1) a
node and 2) a pointer to an stree object.
22
Dereferencing and begin()
T operator () const if (nodePtr NULL)
throw referenceError("stree iterator operator
() NULL reference") return nodePtr-gtnodeValue

template lttypename Tgt streeltTgtiterator
streeltTgtbegin() stnodeltTgt curr
root // if the tree is not empty, the first
node // inorder is the farthest node left from
root if (curr ! NULL) while (curr-gtleft
! NULL) curr curr-gtleft // build
return value using private constructor return
iterator(curr, this)
23
Prefix Operator
Two special situations
The next iteration must stop with value end().
The next node is the smallest value in the tree.
24
Operator
iterator operator () stnodeltTgt
p if (nodePtr NULL) // from
end(). get the root of the tree nodePtr
tree-gtroot // error! requested for an
empty tree if (nodePtr NULL) throw
underflowError("stree iterator operator
() tree empty") // move to the smallest
value in the tree, // which is the first node
inorder while (nodePtr-gtleft !
NULL) nodePtr nodePtr-gtleft
25
else if (nodePtr-gtright !
NULL) // successor is the furthest left
node of // right subtree nodePtr
nodePtr-gtright while (nodePtr-gtleft !
NULL) nodePtr nodePtr-gtleft else
// have already processed the left
subtree, and // there is no right subtree.
p nodePtr-gtparent while (p ! NULL
nodePtr p-gtright) nodePtr
p p p-gtparent // if we were
previously at the right-most node in // the
tree, nodePtr NULL, and the iterator
specifies // the end of the list nodePtr
p return this
Write a Comment
User Comments (0)
About PowerShow.com