How does recursion works here? - recursion

This is the main file:
public static void main(String[] args) {
BTFunction bt=new BTFunction();
bt.insert(5);
bt.insert(15);
bt.insert(10);
bt.insert(7);
}
This is the class BTFunction:
public class BTFunction {
BTNode root=null;
void insert(int data){
root=BTinsertion(data,root);
}
BTNode BTinsertion(int data,BTNode n){
if(n==null){
n=new BTNode(data);
}
else{
if(n.right==null)
n.right=BTinsertion(data,n.right);
else
n.left=BTinsertion(data,n.left);
}
return n;
}
I understand the first 3 insertions(i.e. root, right and left) but when a new value(i.e. 7) is inserted, how does the function works.
According to me when insert(7) is implemented, it should just search for root.next and root.right which both are not null now. So, it should not do anything.
Can you explain the recursive process, specially when more values are added.

Here's what happens when bt.insert(7); is called:
n is not null, so we move to the else block. n.right is not null, so we move to the next else block. Here we call BTinsertion with n.left as the root.
Running BTinsertion a level down, data is still 7, and n is our node with value 10.
n is not null, but n.right is null, so the 7 node will be added to the right of node 10.
return n is called, so our node with value 10 is now a node with a root value and a value for n.right (7). Hope that makes sense.

Related

LinkedList not printing the first element in a special case

I am new to recursion concept and while practicing I came across a problem for which I am not able to get a logical reasoning.
For the below code snippet, first element of the link is not getting printed (Assume list has more than one elements).
public void foo(ListNode head) {
foo1(head, head.next);
}
private void foo1(ListNode curr, ListNode nextNode) {
if (nextNode == null) {
return;
}
curr = nextNode;
nextNode = curr.next;
foo1(curr, nextNode);
System.out.println(curr);
}
Now for example if list has 3 elements as 1 -> 2 -> 3 -> null, only 3 and 2 are getting printed. foo method made a call with head element which is one so shouldn't it print 1 aslo in the output.
Please help me understand what I am doing wrong here.
curr = nextNode;
...
System.out.println(curr);
The problem is you set the curr to its successor before you print it.
private void foo1(ListNode curr) {
if (curr == null)
return;
System.out.println(curr);
foo1(curr.next);
}
Reason
The reason is, before printing the first element, your code changes the value from the first element to the second element. One more issue also present in your code, when we pass one element, it won't print that one element also. Because consider this you have a node 5 and the next node is null. When you pass this node to foo1() method, immediately it reaches the condition nextNode is null and it returns, here there is no chance to print the first node 5.
I modified your same code to run perfectly as expected, look below.
public void foo(ListNode head) {
foo1(head, head.next);
}
private void foo1(ListNode curr, ListNode nextNode) {
if(curr != null)
System.out.println(curr);
if (nextNode == null) {
return;
}
curr = nextNode;
nextNode = curr.next;
foo1(curr, nextNode);
}

How can i get this tree to work properly?

I cannot get this tree to act correctly. i keep getting exit error codes. What is going on with the tree and how would i use the search function in main? it seems the methods are coded correctly but i am not using it correctly in main. i keep geting exit errors that are not 0 and none of the methods i try to use in the main function work. now i am just typing to fill in space because apparently my post is mostly code and not enough text!
//Binary Tree Practice
#include <iostream>
struct node{
int data;
node* right;
node* left;
};
class bTree{
public:
bTree(){
root=NULL;
}
~bTree(){
destroyTree();
}
void addNode(int key);
node *search(int key);
void destroyTree();
private:
node* root;
void addNode(int key,node*nod);
node *search(int key, node *leaf);
void destroyTree(node*&node);
};
node *bTree::search(int key)
{
return search(key, root);
}
void bTree::destroyTree()
{
destroyTree(root);
}
void bTree::addNode(int key)
{
if(root!=NULL)
addNode(key, root);
else
{
root=new node;
root->data=key;
root->left=NULL;
root->right=NULL;
}
}
void bTree::addNode(int key, node* nod) {//ADD a node in correct position.
if (key < nod->left->data) {
if (nod->left != NULL)
addNode(key, nod->left);//RECURSION traverse tree to the left until
find a NULL node
else {//When NULL node is found
nod->left = new node;
nod->left->data = key;
nod->left->left = NULL;
nod->right = NULL;
std::cout<<"node added"<<std::endl;
}
} else if (key > nod->right->data) {
if (nod->right != NULL)
addNode(key, nod->right);//RECURSIONTraverse right till find a null
node
else {//NULL node found
nod->right = new node;//Create new node
nod->right->data = key;//set NODE data to KEY
nod->right->right = NULL;
nod->left = NULL;
}
}
}
node *bTree::search(int key, node *leaf)
{
if(leaf!=NULL)
{
if(key==leaf->data)
return leaf;
if(key<leaf->data)
return search(key, leaf);
else
return search(key, leaf->right);
}
else return NULL;
}
void bTree:: destroyTree(node*&node){
if(node==NULL){
destroyTree(node->left);
destroyTree(node->right);
delete node;
}
}
int main() {
bTree *trees=new bTree();
trees->addNode(10);
trees->addNode(6);
trees->addNode(14);
node *check;
}
The first thing your addNode function with signature void bTree::addNode(int key, node* nod) does is this:
if (key < nod->left->data) {
The problem with your code is that nod->left will lead to a crash, since the left node has not been initialized and leads to an unauthorized memory access, or what is called a segmentation fault. Let's go through the main loop.
addNode(10) - The addNode function with signature void bTree::addNode(int key) is called, root is null, so root is created with left and right nodes set to NULL.
addNode(6) - The addNode function with signature void bTree::addNode(int key) is called, root is NOT null, so addNode with signature void bTree::addNode(int key, node* nod) is called. Then nod->left, and crash.
This is a common problem in low level programming, and my advice to you is to put debug prints inside the functions to see which parameters entered, and where exactly the code crashed. If you can pinpoint the exact line that leads to the crash (in thise case the line with nod->left) you can solve these kinds of problems more easily in the future.
In order to fix your issue, simply make sure to initialize the left and right nodes before you access them.

BinaryTree count leaves infinite loof

I wrote countLeaf method in my binary tree class to count every leaves from root.
However, it gave me stack overflow error, but I couldn't figure what I did wrong.
this is the countLeaf class from my binaryTree
public int countLeaf(Node node){
if(root == null){return 0;} // this part work when I create null Tree
else if(root.left == null && root.right == null){
return 1; //this work when I create tree without left and right
}
else {
System.out.print(root.data); // check infinite loop
return countLeaf(root.left) + countLeaf(root.right);
}
}
And this is my main
public static void main (String[] args){
BinaryTree a = new BinaryTree("A?",
new BinaryTree("B?",
new BinaryTree("D"),
new BinaryTree("E")),
new BinaryTree("C?",
new BinaryTree("E"),
new BinaryTree("F")));
System.out.print(a);
int n = a.countLeaf(a.root);
}
and when I run it, it gave me
A?A?A?A?A?A?A?A?A?A?A?A?A? ... and stackoverflow error
why it keep repeating original root instead of follow left or right??
Replace public int countLeaf(Node node) with public int countLeaf(Node root).
I believe it will help.
Anyways, variable node is never used.

Rebuild BST from preorder, error in logic

Trying to wrap my head around how to correct my code. I have the idea up, but I get stuck during the implementation.
when I step through the code below, I can reconstruct part of the BST from a pre-order traversal. But at some point, I will have function call like:
recon(preOrd,2,2)
which results in a leaf not being assigned. I do yet know how to correct this.
I have seen other threads on this topic, but want to iron out my issue so I can really learn this concept of rebuilding the BST.
public static Node recon(int[] preOrd,int start,int end){
if (start==end){
return null;
}
Node root = new Node (preOrd[start]);
int div=start;
for (i=start+1;i<=end && preOrd[i]<preOrd[start];i++){
div=i;
}
Node left= reconstruct(preOrd,start+1,div);
Node right= reconstruct(preOrd,div+1,end);
root.setLeft= left;
root.setRight=right;
return root;
}
Turns out this is pretty straightforward. Just needed to correct my thinking on the updating of leaf nodes..
public static Node recon(int[] preOrd,int start,int end){
Node root = new Node (preOrd[start]);//declare the new node
if (start>end){ //this is illegal, so return null
return null;
}
if (start==end){
return root;
}
int div=start;
for (int i=start+1;i<=end preOrd[i]<preOrd[start];i++){
div=i;
}
Node left= reconstruct(preOrd,start+1,div);
Node right= reconstruct(preOrd,div+1,end);
root.setLeft= left;
root.setRight=right;
return root;
}

Binary Tree and Return root node

..I'm building a binary tree where the root is given and the children are either root-3, root-2 or root-1 (that is, they hold those number of pennies). So 5 would have nodes of 2,3,4, and so on, until the leaves are 0. Here's my method for making such a tree. I don't understand why the method doesn't return the original node, in this case, the value should be 3.
Any guidance would be awesome.
public GameNode buildTree1(GameNode root){
int penn = root.getPennies();
if (penn < 0)
{
return null;
}
else {
root.print();
root.setLeft(buildTree1(new GameNode(penn-1)));
root.setMiddle(buildTree1(new GameNode(penn-2)));
root.setRight(buildTree1(new GameNode(penn-3)));
return root;
}
Get/Set Methods
public void setLeft(GameNode newNode) {
// TODO Auto-generated method stub
left = newNode;
}
Same for setMiddle and setRight;

Resources