Inorder Successor of a Node
Note:
This code was written during a crunch period and isn't perfect. There will
be some errant spacing, some files will be
using namespace std,
etc. But it's all still usable and can be a
handy guideline if you're learning Data Structures.
#include <vector>
#include <queue>
#include <stdlib.h>
#include <stdlib.h>
struct TreeNode
{
TreeNode(int v) : value(v){};
TreeNode *left = nullptr;
TreeNode *right = nullptr;
int value;
};
class BinaryTree
{
public:
TreeNode *buildPerfectTree(int starter);
void add(TreeNode *parent, int diff);
TreeNode *getRoot();
TreeNode *getInOrderSuccessor(TreeNode *parent);
private:
TreeNode *root = nullptr;
};
void BinaryTree::add(TreeNode *parent, int diff)
{
int leftVal = parent->value - diff;
int rightVal = parent->value + diff;
{
return;
}
parent->left = new TreeNode(leftVal);
parent->right = new TreeNode(rightVal);
add(parent->left, diff / 2);
add(parent->right, diff / 2);
}
TreeNode *BinaryTree::buildPerfectTree(int starter)
{
TreeNode *head = new TreeNode(starter);
root = head;
add(root, starter / 2);
return getRoot();
}
TreeNode *BinaryTree::getRoot() { return root; }
TreeNode *BinaryTree::getInOrderSuccessor(TreeNode *parent)
{
if (parent->left != nullptr)
{
return getInOrderSuccessor(parent->left);
}
else
{
return parent;
}
}
void printLevelOrder(TreeNode *node)
{
std::queue<TreeNode *> q;
q.push(node);
while (!q.empty())
{
int size = q.size();
for (int i = 0; i < size; i++)
{
TreeNode *cu = q.front();
std::cout << cu->value << " ";
q.pop();
if (cu->left != nullptr)
q.push(cu->left);
if (cu->right != nullptr)
q.push(cu->right);
}
std::cout << std::endl;
}
}
int nodeCount(TreeNode *cu)
{
if (cu == nullptr)
return 0;
return 1 + nodeCount(cu->left) + nodeCount(cu->right);
}
int getSum(TreeNode *cu)
{
if (cu == nullptr)
return 0;
int left = getSum(cu->left);
int right = getSum(cu->right);
return left + right + cu->value;
}
int main(int argc, char *argv[])
{
BinaryTree test;
test.buildPerfectTree(40);
printLevelOrder(test.getRoot());
TreeNode *successor = test.getInOrderSuccessor(test.getRoot()->right);
std::cout << "successor of " << test.getRoot()->value << ": " << successor->value << std::endl;
return 0;
}