diff options
author | Haidong Ji | 2019-01-15 22:25:39 -0600 |
---|---|---|
committer | Haidong Ji | 2019-01-15 22:25:39 -0600 |
commit | 4f9bd7ae24acef1824134df9a62521185e3d1aa3 (patch) | |
tree | c67bb63edc7e31f15f32b60869ac024c4aa2261b /Sources | |
parent | 8fb3943122786e9ab13bf06cae6ad9ab70860fc4 (diff) |
Tree height done in C++!!!
Wow, this is so frustrating at times but in the end so much fun!!! My
real issue was creating and using a member function called getChildren,
which returns a node's children. It turned out if I accessed the
children member variable directly (since it's public), problem solved!
Another somewhat interesting observation is that using "vector<Node>" in
the main getHeight function seems slightly more efficient (memory wise)
than "vector<Node*>". I also used Visual Studio Community C++ 2017,
which was cool as well!
Diffstat (limited to 'Sources')
-rw-r--r-- | Sources/DataStructure.cpp | 15 |
1 files changed, 3 insertions, 12 deletions
diff --git a/Sources/DataStructure.cpp b/Sources/DataStructure.cpp index b82f908..91fdf86 100644 --- a/Sources/DataStructure.cpp +++ b/Sources/DataStructure.cpp @@ -23,24 +23,16 @@ public: parent = theParent; parent->children.push_back(this); } - - vector<Node *> getChildren() { - return children; - } - - void setKey(int theKey) { - key = theKey; - } }; int treeLevelCounter(vector<Node> &nodes, int rootIndex) { int levelCounter = 1; queue<Node *> q; int popCount = 0; - int childrenCount = nodes[rootIndex].getChildren().size(); + int childrenCount = nodes[rootIndex].children.size(); int grandChildrenCount = 0; for (int i = 0; i < childrenCount; i++) { - q.push(nodes[rootIndex].getChildren()[i]); + q.push(nodes[rootIndex].children[i]); } while (!q.empty()) { if (popCount < childrenCount) { @@ -67,9 +59,8 @@ int getHeight(vector<int> &a) { vector<Node> nodes; nodes.resize(a.size()); int rootIndex = 0; - for (int i = 0; i < a.size(); i++) { + for (size_t i = 0; i < a.size(); i++) { int parentIndex = a[i]; - nodes[i].setKey(i); if (parentIndex == -1) { rootIndex = i; } else { |