Question

In: Computer Science

Write a boolean function that is given a binary tree and returns true if and only...

Write a boolean function that is given a binary tree and returns true if and only if the tree has an odd number of nodes. An empty tree is considered to have an even number of nodes. Notes: The function should have just one argument, a pointer to the root. No global variables may be used. No additional functions may be defined. You may not count the number of nodes.

Solutions

Expert Solution

BOOLEAN FUNCTION :

bool OddCheck(BinaryTreeNode<int>* root){
  
// if the binary tree is Empty ie. considered as even nodes.
if(root==NULL)
return false;
  
// if binary tree only contains one node ie.odd number f nodes
if(root->left==NULL&&root->right==NULL)
return true;
  
  
//if binary tree have only contains right subtree and left subtree is Empty.
else if(root->left==NULL&&root->right!=NULL){
bool a = OddCheck(root->right);
if(a==true)
return false;
else
return true;
  
}
  
//if binary tree have only contains left subtree and right subtree is Empty.
else if(root->left!=NULL&&root->right==NULL){
  
bool a = OddCheck(root->left);
if(a==true)
return false;
else
return true;
  
}
  
  
// If both Left Subtree and Right Subtree is not Empty.
else if(root->left!=NULL&&root->right!=NULL){
  
bool a = OddCheck(root->left);
bool b = OddCheck(root->right);
  
//if both subtrees have odd numbers of nodes. Then result will be odd.
if(a==true&&b==true)
return true;
  
//If both subtrees have even number of nodes then result will be:

// even + even + 1(for root) =ODD


else if(a==false&&b==false)
return true;
  
//for all other cases the result will be even numbers of nodes.
else
return false;
  
  
}
  
}


Related Solutions

(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is...
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is a palindrome, and boolean False if it is not. The palindromes for this problem are contained in the list below. You must use this list in your solution. Use a for loop to retrieve each palindrome and test it. Your script should test each string in the list of palindromes below. Be aware there's a second list of palindromes embedded in the palindromes list....
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
Write a function matrix power(A, n) that computes the power An using Boolean arithmetic and returns...
Write a function matrix power(A, n) that computes the power An using Boolean arithmetic and returns the result. You may assume that A is a 2D list containing only 0s and 1s, A is square (same number of rows and columns), and n is an integer ≥ 1. You should call your previously written matrix multiply boolean function. Example: Let R = [ [0, 0, 0, 1], [0, 1, 1, 0], [0, 0, 0, 1], [0, 0, 1, 0] ]...
Write a C++ method that traverses through a binary tree (only 2 children to a node)...
Write a C++ method that traverses through a binary tree (only 2 children to a node) using preorder traversal and finds the proper place to put in a new node. The method will take in an int and traverse through the tree to find the proper place but it must use PREORDER traversal to find a proper position. struct node{ int data; struct node* left, *right; Node(int Data){ this->data=data; left=right=NULL; } }; void preorderAdd(int newNum){ //Method to traverse using preorder...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT