-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.cpp
82 lines (72 loc) · 1.67 KB
/
Node.cpp
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
#include <iostream>
#include <algorithm>
#include "Node.h"
/*
* Returns the data that is stored in this node
*
* @return the data that is stored in this node.
*/
int Node::getData() const
{
return data;
}
/*
* Returns the left child of this node or null if it doesn't have one.
*
* @return the left child of this node or null if it doesn't have one.
*/
NodeInterface * Node::getLeftChild() const
{
return leftChild;
}
/*
* Returns the right child of this node or null if it doesn't have one.
*
* @return the right child of this node or null if it doesn't have one.
*/
NodeInterface * Node::getRightChild() const
{
return rightChild;
}
/*
* Returns the height of this node. The height is the number of nodes
* along the longest path from this node to a leaf. While a conventional
* interface only gives information on the functionality of a class and does
* not comment on how a class should be implemented, this function has been
* provided to point you in the right direction for your solution. For an
* example on height, see page 448 of the text book.
*
* @return the height of this tree with this node as the local root.
*/
int Node::getHeight()
{
int leftHeight = 0;
int rightHeight = 0;
if (leftChild != NULL)
{
leftHeight = leftChild->getHeight();
}
if (rightChild != NULL)
{
rightHeight = rightChild->getHeight();
}
return max(leftHeight, rightHeight) + 1;
}
int Node::getBalance()
{
int right;
int left;
if (rightChild != NULL)
{
right = rightChild->getHeight();
}
else
right = 0;
if (leftChild != NULL)
{
left = leftChild->getHeight();
}
else
left = 0;
return right - left;
}