From 41145b01e4e597a1b8573fb39be8db904c9a91d8 Mon Sep 17 00:00:00 2001 From: Anurupa-05 <115679435+Anurupa-05@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:53:00 +0530 Subject: [PATCH] Create Anurupa_hactober Checking if a binary tree is a full binary tree in C++ --- Anurupa_hactober | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Anurupa_hactober diff --git a/Anurupa_hactober b/Anurupa_hactober new file mode 100644 index 0000000..4a65508 --- /dev/null +++ b/Anurupa_hactober @@ -0,0 +1,96 @@ + +#include +using namespace std; + +struct Node { + int key; + struct Node *left, *right; +}; + +// New node creation +struct Node *newNode(char k) { + struct Node *node = (struct Node *)malloc(sizeof(struct Node)); + node->key = k; + node->right = node->left = NULL; + return node; +} + +bool isFullBinaryTree(struct Node *root) { + + // Checking for emptiness + if (root == NULL) + return true; + + // Checking for the presence of children + if (root->left == NULL && root->right == NULL) + return true; + + if ((root->left) && (root->right)) + return (isFullBinaryTree(root->left) && isFullBinaryTree(root->right)); + + return false; +} + +int main() { + struct Node *root = NULL; + root = newNode(1); + root->left = newNode(2); + root->right = newNode(3); + root->left->left = newNode(4); + root->left->right = newNode(5); + root->left->right->left = newNode(6); + root->left->right->right = newNode(7); + + if (isFullBinaryTree(root)) + cout << "The tree is a full binary tree\n"; + else + cout << "The tree is not a full binary tree\n"; +} + +#include +using namespace std; + +struct Node { + int key; + struct Node *left, *right; +}; + +// New node creation +struct Node *newNode(char k) { + struct Node *node = (struct Node *)malloc(sizeof(struct Node)); + node->key = k; + node->right = node->left = NULL; + return node; +} + +bool isFullBinaryTree(struct Node *root) { + + // Checking for emptiness + if (root == NULL) + return true; + + // Checking for the presence of children + if (root->left == NULL && root->right == NULL) + return true; + + if ((root->left) && (root->right)) + return (isFullBinaryTree(root->left) && isFullBinaryTree(root->right)); + + return false; +} + +int main() { + struct Node *root = NULL; + root = newNode(1); + root->left = newNode(2); + root->right = newNode(3); + root->left->left = newNode(4); + root->left->right = newNode(5); + root->left->right->left = newNode(6); + root->left->right->right = newNode(7); + + if (isFullBinaryTree(root)) + cout << "The tree is a full binary tree\n"; + else + cout << "The tree is not a full binary tree\n"; +}