Skip to content

Instantly share code, notes, and snippets.

@bexp
Created October 17, 2017 04:14
Show Gist options
  • Save bexp/7cf1f27daf3d1049a19a33ff764827d7 to your computer and use it in GitHub Desktop.
Save bexp/7cf1f27daf3d1049a19a33ff764827d7 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
typedef struct node {
int data;
node() : data(0), left(NULL), right(NULL) {}
node(int d) {
data = d;
left = NULL;
right = NULL;
}
struct node* left;
struct node* right;
} Node;
Node* insert(Node* root, int data) {
if (root == NULL)
return new Node(data);
else {
if (data <= root->data) {
cout << "left insert\n";
root->left = insert(root->left, data);
} else {
cout << "right insert\n";
root->right = insert(root->right, data);
}
return root;
}
}
int minValue(Node* node) {
Node* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL) {
current = current->left;
}
return(current->data);
}
int maxDepth(Node* node) {
if (node == NULL)
return 0;
int leftDepth = maxDepth(node->left);
int rightDepth = maxDepth(node->right);
int maxDepth = max(leftDepth, rightDepth);
return maxDepth + 1;
}
// A utility function to do inorder traversal of BST
void inorder(Node *root)
{
if (root != NULL)
{
inorder(root->left);
cout << root->data;
inorder(root->right);
}
}
// To execute C++, please define "int main()"
int main() {
Node *root = new Node(10);
root = insert(root, 4);
root = insert(root, 2);
root = insert(root, 1);
root = insert(root, 3);
root = insert(root, 6);
root = insert(root, 5);
inorder(root);
return 0;
}
/*
Your previous Plain Text content is preserved below:
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment