From e0a479c4bccd1f4bd7f323c155b12cb40248538f Mon Sep 17 00:00:00 2001 From: Nikita pande <30722238+Nikitapande@users.noreply.github.com> Date: Sun, 24 Oct 2021 22:28:11 +0530 Subject: [PATCH] Create binary_tree.cpp --- Graph/binary_tree.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Graph/binary_tree.cpp diff --git a/Graph/binary_tree.cpp b/Graph/binary_tree.cpp new file mode 100644 index 00000000..abe56da9 --- /dev/null +++ b/Graph/binary_tree.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + +struct Node { + int data; + struct Node* left; + struct Node* right; + + // val is the key or the value that + // has to be added to the data part + Node(int val) + { + data = val; + + // Left and right child for node + // will be initialized to null + left = NULL; + right = NULL; + } +}; + +int main() +{ + + /*create root*/ + struct Node* root = new Node(1); + /* following is the tree after above statement + + 1 + / \ + NULL NULL + */ + + root->left = new Node(2); + root->right = new Node(3); + /* 2 and 3 become left and right children of 1 + 1 + / \ + 2 3 + / \ / \ + NULL NULL NULL NULL + */ + + root->left->left = new Node(4); + /* 4 becomes left child of 2 + 1 + / \ + 2 3 + / \ / \ + 4 NULL NULL NULL + / \ + NULL NULL + */ + + return 0; +}