-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryNode.hpp
More file actions
125 lines (112 loc) · 3.26 KB
/
BinaryNode.hpp
File metadata and controls
125 lines (112 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*Name: Clare Meyer
*Class: EECS268
*professor: Gibbons
*Lab: Surya Tej Nimmakayala, Fri 9:00AM
*BinaryNode.hpp
*description:this is the implementation of the BinaryNode class
*/
#include "BinaryNode.h"
template<class ItemType>
BinaryNode<ItemType>::BinaryNode()
{
}
//precondition: member variables are not defined
//postCondition: member variables are defined
//returnType: none
template<class ItemType>
BinaryNode<ItemType>::BinaryNode(const ItemType& anItem)//constructor
{
//member variables
item=anItem;
leftChildPtr=nullptr;
rightChildPtr=nullptr;
}
//precondition: member variables are not defined
//postCondition: member variables are defined
//returnType: none
//different becuase used if have values for left and right children
template<class ItemType>
BinaryNode<ItemType>::BinaryNode(const ItemType& anItem,
BinaryNode<ItemType>* leftPtr,
BinaryNode<ItemType>* rightPtr)//constructor
{
//member variables
item=anItem;
leftChildPtr=leftPtr;
rightChildPtr=rightPtr;
}
//precondition: node may or may not have a value
//postCondition: node has a new value
//returnType: void
template<class ItemType>
void BinaryNode<ItemType>::setItem(const ItemType& anItem)
{
//set value passed in to memeber variable value
item=anItem;
}
//precondition: do not know value of node
//postcondition: know value of know
//returnType: ItemType
template<class ItemType>
ItemType BinaryNode<ItemType>::getItem() const
{
return(item);
}
//precondition: do not know if node is a leaf on the tree
//postcondition: know if the node is a leaf on the tree (leaf is node has no children)
//return type: bool
template<class ItemType>
bool BinaryNode<ItemType>::isLeaf() const
{
//if the node has no left child and no right child, it is a leaf so return true
if(leftChildPtr==nullptr&&rightChildPtr==nullptr)
{
return(true);
}
//otherwise you return false because it is not a leaf
else
{
return(false);
}
}
//precondition: do not know the left child pointer
//postcondition: know the left child pointer
//returnType: BinaryNode<ItemType>*
template<class ItemType>
BinaryNode<ItemType>* BinaryNode<ItemType>::getLeftChildPtr() const
{
return(leftChildPtr);
}
//precondition: do not know the right child pointer
//postcondition: know the right child pointer
//returnType: BinaryNode<ItemType>*
template<class ItemType>
BinaryNode<ItemType>* BinaryNode<ItemType>::getRightChildPtr() const
{
return(rightChildPtr);
}
//precondition: the left child pointer may or may not hold a value
//postCondition: the left child pointer holds a new value
//return type: void
template<class ItemType>
void BinaryNode<ItemType>::setLeftChildPtr(BinaryNode<ItemType>* leftPtr)
{
leftChildPtr=leftPtr;
}
//precondition: the right child pointer may or may not hold a value
//postCondition: the right child pointer holds a new value
//return type: void
template<class ItemType>
void BinaryNode<ItemType>::setRightChildPtr(BinaryNode<ItemType>* rightPtr)
{
rightChildPtr=rightPtr;
}
//precondition: the binary node member varibales have not been deallocated from the heap
//postcondition: the binary node member variables are no longer on the heap
//returnType: none
template<class ItemType>
BinaryNode<ItemType>::~BinaryNode()
{
//delete leftChildPtr;
//delete rightChildPtr;
}