Hey I have to find the most eficient way to print a number by giving the postion. The input is like this:
8 (N-> N Numbers)
INS 100 (Add 100 to the tree)
INS 200 (Add 200 to the tree)
INS 300 (Add 300 to the tree)
REM 200 (Remove the number 200 from the tree)
PER 1 (Have to output the biggest number in the tree-> Shoud print 300)
INS 1000 (Add 1000 to the tree)
PER 1 ((Have to output the biggest number in the tree-> Shoud print 1000))
PER 2 (I have to output the second biggest number so: 300)
I have a way to print like this, but is very slow and I have to maintain a O(N * log(N)).
Here is my full code
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
// An AVL tree node
struct node
{
int key;
struct node *left;
struct node *right;
int height;
};
// A utility function to get maximum of two integers
int max(int a, int b);
// A utility function to get height of the tree
int height(struct node *N)
{
if (N == NULL)
return 0;
return N->height;
}
// A utility function to get maximum of two integers
int max(int a, int b)
{
return (a > b)? a : b;
}
/* Helper function that allocates a new node with the given key and
NULL left and right pointers. */
struct node* newNode(int key)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially added at leaf
return(node);
}
// A utility function to right rotate subtree rooted with y
// See the diagram given above.
struct node *rightRotate(struct node *y)
{
struct node *x = y->left;
struct node *T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
// Return new root
return x;
}
// A utility function to left rotate subtree rooted with x
// See the diagram given above.
struct node *leftRotate(struct node *x)
{
struct node *y = x->right;
struct node *T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
// Return new root
return y;
}
// Get Balance factor of node N
int getBalance(struct node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
struct node* insert(struct node* node, int key)
{
/* 1. Perform the normal BST rotation */
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
/* 2. Update height of this ancestor node */
node->height = max(height(node->left), height(node->right)) + 1;
/* 3. Get the balance factor of this ancestor node to check whether
this node became unbalanced */
int balance = getBalance(node);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
// Left Right Case
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
/* return the (unchanged) node pointer */
return node;
}
/* Given a non-empty binary search tree, return the node with minimum
key value found in that tree. Note that the entire tree does not
need to be searched. */
struct node * minValueNode(struct node* node)
{
struct node* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
current = current->left;
return current;
}
struct node* apagaNode(struct node* root, int key)
{
// STEP 1: PERFORM STANDARD BST DELETE
if (root == NULL)
return root;
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if ( key < root->key )
root->left = apagaNode(root->left, key);
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if( key > root->key )
root->right = apagaNode(root->right, key);
// if key is same as root's key, then This is the node
// to be deleted
else
{
// node with only one child or no child
if( (root->left == NULL) || (root->right == NULL) )
{
struct node *temp = root->left ? root->left : root->right;
// No child case
if(temp == NULL)
{
temp = root;
root = NULL;
}
else // One child case
*root = *temp; // Copy the contents of the non-empty child
free(temp);
}
else
{
// node with two children: Get the inorder successor (smallest
// in the right subtree)
struct node* temp = minValueNode(root->right);
// Copy the inorder successor's data to this node
root->key = temp->key;
// Delete the inorder successor
root->right = apagaNode(root->right, temp->key);
}
}
// If the tree had only one node then return
if (root == NULL)
return root;
// STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
root->height = max(height(root->left), height(root->right)) + 1;
// STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether
// this node became unbalanced)
int balance = getBalance(root);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);
// Left Right Case
if (balance > 1 && getBalance(root->left) < 0)
{
root->left = leftRotate(root->left);
return rightRotate(root);
}
// Right Right Case
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
// Right Left Case
if (balance < -1 && getBalance(root->right) > 0)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
int imprime(struct node *root,int targetPos,int curPos)
{
if(root != NULL)
{
int newPos = imprime(root->left, targetPos, curPos);
newPos++;
if (newPos == targetPos)
{
printf("%d\n", root->key);
}
return imprime(root->right, targetPos, newPos);
}
else
{
return curPos;
}
}
int main()
{
struct node *root = NULL;
int total=0;
int n,b;
string a;
cin >> n;
for (int i=0; i<n; i++)
{
cin >> a >> b;
if(a=="INS")
{root = insert(root, b);total=total+1;}
else
if(a=="REM")
{root = apagaNode(root, b);total=total-1;}
else
imprime(root, total-b+1, 0);
}
return 0;
}
The way I found to print the values:
int imprime(struct node *root,int targetPos,int curPos)
{
if(root != NULL)
{
int newPos = imprime(root->left, targetPos, curPos);
newPos++;
if (newPos == targetPos)
{
printf("%d\n", root->key);
}
return imprime(root->right, targetPos, newPos);
}
else
{
return curPos;
}
}
Problem is that this function is very slow, and I can't use it. How is the best way to print by a given postion like this? (A heard about, counting n_nodes, and during the rotations I have to incremennt, decrement, i realy did not understand. Help me please! Give some tips, and advices) (PS: I'm not an expert with this kind of algorithms)
The advice you heard is correct: you should add a node counter to your node structure:
struct node
{
int key;
struct node *left;
struct node *right;
int height;
int n_nodes;
};
It should hold the number of nodes in the tree. Assuming it's correct, you can improve the algorithm for finding a node with a target position: it will know exactly in which branch of the tree to look (left or right), which will make the search faster (current imprime implementation is O(n)).
So, how to make it so the n_nodes field holds the right value? Fortunately, you already have an example: height. Look where your existing code changes it; these are roughly the places where you have to update n_nodes, too. Most of them are trivial (just add 1 to it); the more interesting ones are the rotation functions:
struct node *rightRotate(struct node *y)
{
struct node *x = y->left;
struct node *T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
// Update numbers of nodes
x->n_nodes = ...;
y->n_nodes = ...;
T2->n_nodes = ...;
// Return new root
return x;
}
So it transforms the tree like this:
y x
/ \ / \
x D A y
/ \ ==> / \
A T2 T2 D
/ \ / \
B C B C
Here A, B, C and D are trees whose sizes your program knows; let's denote their sizes as a, b, c and d. So the transformation changes these sizes like this:
size of x: from a+b+c+2 to a+b+c+d+3
size of y: from a+b+c+d+3 to b+c+d+2
size of T2: unchanged
So just transform this to code.
Related
I'm trying to solve a self-balancing tree problem on Hackerrank (https://www.hackerrank.com/challenges/self-balancing-tree/problem) and I keep getting a Segmentation fault in the last 3 test cases. I saw that this same problem had been posted before and tried to apply the same approach (check for nullptr) to my case, but no success so far. The following code is written in C++ and I would appreciate a lot if you guys could help me out. Thanks!
/* Node is defined as :
typedef struct node
{
int val;
struct node* left;
struct node* right;
int ht;
} node; */
node * newNode(int val)
{
node * newNode = new node;
newNode->val = val;
newNode->left = nullptr;
newNode->right = nullptr;
newNode->ht = 0;
return newNode;
}
int getHeight(node* root)
{
if(root == nullptr) {
return -1;
} else {
return root->ht;
}
}
node* rightRotate(node* root)
{
node* temp = root->left;
root->left = temp->right;
temp->right = root;
root->ht = 1 + max(getHeight(root->left), getHeight(root->right));
temp->ht = 1 + max(getHeight(temp->left), getHeight(temp->right));
return temp;
}
node* leftRotate(node* root)
{
node* temp = root->right;
root->right = temp->left;
temp->left = root;
root->ht = 1 + max(getHeight(root->left), getHeight(root->right));
temp->ht = 1 + max(getHeight(temp->left), getHeight(temp->right));
return temp;
}
node * insert(node * root,int val)
{
if(root == nullptr) {
root = newNode(val);
return root;
}
if(val > root->val) {
root->right = insert(root->right, val);
}
else if(val < root->val) {
root->left = insert(root->left, val);
}
else{ return 0; }
root->ht = 1 + max(getHeight(root->left),getHeight(root->right));
int balance = getHeight(root->left) - getHeight(root->right);
if(root->left != nullptr && balance > 1) {//Left subtree disbalanced
if(val < root->left->val) {
//Left-Left case: perform a right rotation on the disb. node
return rightRotate(root);
}
else {
//Left-Right case: perfom a left rotation on the disb. node left subtree
//and a right rotation on the disb. node
root->left = leftRotate(root->left);
return rightRotate(root);
}
}
if(root->right != nullptr && balance < -1) {//Right subtree disbalanced
if(val > root->right->val) {
//Right-Right case: perform a left rotation on the disb. node
return leftRotate(root);
}
else {
//Right-Left case: perfom a right rotation on the disb. node right subtree
//and a left rotation on the disb. node
root->right = rightRotate(root->left);
return leftRotate(root);
}
}
return root;
}
Edit:
For debugging purposes I've used an online gdb and added to the code above the following traversal method and main function:
void inOrder(node* root) {
if(root == NULL) {
return;
} else {
inOrder(root->left);
cout << root->val << " ";
inOrder(root->right);
}
}
int main()
{
node* root=NULL;
root=insert(root,2);
root=insert(root,4);
root=insert(root,3);
inOrder(root);
return 0;
}
After trying to insert the values 2, 4 and 3, in this order, we would have a disbalanced tree since the right subtree would have a height of 1 while the left subtree would have a height of -1 (leaf node is nullptr) and the balance factor would be less than -1. Further analysis shows that we have a RIGHT-LEFT case since the node causing the disbalance is the left child of the right child of the disbalanced node (root 2). We would then have to perform a right rotation on the disbalanced node right child followed by a left rotation on the disbalanced node itself, and the tree should end up looking like the following:
3
/ \
2 4
Thanks to everyone who tried helping me out on this one, it turns out there is no limit to how dumb I can be. I believe I've figured out what I had done wrong.
The problem in this question lies on the section that checks if the right subtree is disbalanced and, if so, performs the necessary rotations. On the code piece below, it should be
root->right = rightRotate(root->right);
instead of
root->right = rightRotate(root->left);
Here's a code snippet of a solution that calculates height of each node in a binary tree and stores the height in each node. The code traverse the tree recursively, and below is the Node constructor.
class Node {
public:
int height; // to be set by computeHeight()
Node *left, *right;
Node() { height = -1; left = right = nullptr; }
~Node() {
delete left;
left = nullptr;
delete right;
right = nullptr;
}
};
Below is the function which computes and stores height at each Node. Where I am confused is how do leftHeight and rightHeight get updated by n->left->height and n->right->height if at construction, height is set to -1?
void computeHeight(Node *n) {
if (n == nullptr) {
return;
}
computeHeight(n->left);
computeHeight(n->right);
int leftHeight = -1;
int rightHeight = -1;
if (n->left != nullptr) {
leftHeight = n->left->height;
}
if (n->right != nullptr) {
rightHeight = n->right->height;
}
n->height = std::max(leftHeight, rightHeight) + 1;
}
Here is the main file that runs the function computeHeight
int main() {
Node *n = new Node();
n->left = new Node();
n->right = new Node();
n->right->left = new Node();
n->right->right = new Node();
n->right->right->right = new Node();
n->right->right->right->left = new Node();
computeHeight(n);
delete n;
n = nullptr;
return 0;
}
Imagine a leaf node (left and right are nullptr). Then n->left != nullptr and n->right != nullptr are false so the calculation effectively becomes
int leftHeight = -1;
int rightHeight = -1;
n->height = std::max(leftHeight, rightHeight) + 1;
which is effectively
n->height = 0;
Now because of the way the recursion is done, each node gets it height calculated after it's children have had their heights calculated. So imagine a node with two children, each of which is a leaf node. We've already seen that leaf nodes get a height of zero. So the calculation for such a node is effectively
int leftHeight = -1;
int rightHeight = -1;
if (n->left != nullptr) {
leftHeight = 0; // because n->left is a leaf node
}
if (n->right != nullptr) {
rightHeight = 0; // because n->right is a leaf node
}
n->height = std::max(leftHeight, rightHeight) + 1;
which means that you end up with n->height = 1 for that node.
And so on. These calculations perculate up the tree, starting at the leaves, until finally the root gets it's height set.
We can solve this problem with a basic case of induction. Basically, let's start with the base case, and then assuming any case n works, we have to check if case n+1 works. In the case of calculating the heights of the nodes in a Binary Tree, the base case is when the root node is a leaf node, and for the n+1 case the left/right side nodes are the n cases. You can think of it as n is the height of the current node, and n=0 is the leaf node base case.
When the root node is a leaf, both the left and right side nodes are nullptr the method essentially turns into
void computeHeight(Node *n) {
int leftHeight = -1;
int rightHeight = -1;
n->height = std::max(leftHeight, rightHeight) + 1;
}
in which case n->height becomes 0, which is correct for a leaf node. Now, when the node is a non-leaf node, the statements
computeHeight(n->left);
computeHeight(n->right);
are already called before the calculation. This essentially makes it so that we assume that both the left and right side nodes are already taken care of, and their heights are correct. Then, we can use the left and right nodes' heights to calculate the root node's height, which is calculated through
int leftHeight = -1;
int rightHeight = -1;
if (n->left != nullptr) {
leftHeight = n->left->height;
}
if (n->right != nullptr) {
rightHeight = n->right->height;
}
n->height = std::max(leftHeight, rightHeight) + 1;
The trick here is that we already have called computeHeight() on the left and right side nodes so that when we do the calculations on the current node, we can safely assume that the child nodes have been totally taken care of. Also, the child nodes are calculated before the root node, so the program will first trickle all the way down to the leaves before coming back up and calculating the non-leaf nodes.
Okay so I seem to have gotten a bit lost. I am trying to insert data into a tree and when checking for balance and whether or not to rotate, I default to checking through the root. When I check examples online, I see that we can also rotate along other nodes in the tree as well. How do we figure out which node to use to balance the tree and how do we reach said node? I also saw that instead of having void functions to implement the insert and rotation functions, people use a node pointer return type instead. What is the purpose of that? I know the answers may be super obvious I am just lost.
struct TNode{
int data;
TNode* left;
TNode* right;
};
class Tree{
public:
TNode* root;
public:
Tree()
{root = nullptr;}
int height(TNode* node);
int balanceFactor(TNode* node);
bool isBalance(TNode* node)
{return balanceFactor(node)<-1 && balanceFactor(node)>1?true:false;}
void avlInsert(int key);
void LLRotation(TNode* node);
};
int Tree::height(TNode* node){
int l = 0;
int r = 0;
if(!node->left && !node->right)
return 0;
if(node->left)
l = height(node->left) + 1;
if(node->right)
r = height(node->right) + 1;
return l > r ? l : r;
}
int Tree::balanceFactor(TNode *node){
if(node->left && node->right)
return height(node->left) - height(node->right);
else if(node->left && !node->right)
return height(node);
else
return -1 * height(node);
}
void Tree::LLRotation(TNode *node){
TNode* nl = node->left;
node->left = nl->right;
nl->right = node;
if(root == node)
root = nl;
}
void Tree::avlInsert(int key){
TNode* trav;
TNode* follow;
TNode* node = new TNode;
node->data = key;
node->left = nullptr;
node->right = nullptr;
if (!root)
root = node;
else{
trav = root;
while (trav){
if (key < trav->data){
follow = trav;
trav = trav->left;
}
else{
follow = trav;
trav = trav->right;
}
}
if(key < follow->data)
follow->left = node;
else
follow->right = node;
}
if (balanceFactor(root) == 2 && balanceFactor(root->left) == 1)
LLRotation(root);
}
It really depends on your implementation some people they want to return a node, using a void function does not change the result much. For insert function you need to check only after you insert the node so you can go ahead and insert that node than rotate it. Please note that this function will only work if you have an AVL Tree to start with. Also your code is not correct, below:
if (balanceFactor(root) == 2 && balanceFactor(root->left) == 1)
LLRotation(root);
This is not how you check it for insertion.... That's for deletion buddy, I think you are getting mixed up. Usually for insertion you check if the balanceFactor is between 1 and -1, so if you are getting 2, then it is a left heavy tree, in your case, because it is left-right, which means you should do Right rotation........ Buddy......
int l = 0;
int r = 0;
if(!node->left && !node->right)
return 0;
if(node->left)
l = height(node->left) + 1;
if(node->right)
r = height(node->right) + 1;
return l > r ? l : r;
Height function, extremely inefficient... Instead define height as a variable for node and then modify it after each insertion, deletion.
I am having some trouble with a Tree comparison program I am writing in C++. I am trying to traverse a tree to compare elements in another tree from a comparator class, and I am getting an error that says that I cannot access root in the AVL tree class. The error I am getting is:'class avlTree' has no member named 'root.' Below is my code for the method in question, and after that is the AVL Tree class. I am beginning to wonder if I should pass in the second tree to be compared in the method call to a method within the AVL Tree class, and just traverse the tree, and call to the second tree from within the Tree class. I am very much a beginner at C++, so I apologize in advance for any confusion and/or poor style.
Method
void findCommon(){
if(tree.root == NULL){
return;
}
findCommon(tree.root->left);
if (tree2.find(tree.root) == true){
cout<<tree.root->data<<" ";
}
findCommon(tree.root->right);
}
AVL Tree Class
#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<string>
#define pow2(n) (1 << (n))
using namespace std;
/*
* Node Declaration
*/
struct avl_node
{
string data;
struct avl_node *left;
struct avl_node *right;
}*root;
/*
* Class Declaration
*/
class avlTree
{
public:
avlTree()
{
root = NULL;
}
/*
* Height of AVL Tree
*/
int height(avl_node *temp)
{
int h = 0;
if (temp != NULL)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int max_height = max (l_height, r_height);
h = max_height + 1;
}
return h;
}
/*
* Height Difference
*/
int diff(avl_node *temp)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int b_factor= l_height - r_height;
return b_factor;
}
/*
* Right- Right Rotation
*/
avl_node* rr_rotation(avl_node *parent)
{
avl_node* temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
/*
* Left- Left Rotation
*/
avl_node* ll_rotation(avl_node *parent)
{
avl_node* temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
/*
* Left - Right Rotation
*/
avl_node* lr_rotation(avl_node *parent)
{
return parent;
}
/*
* Right- Left Rotation
*/
avl_node* rl_rotation(avl_node *parent)
{
return parent;
}
/*
* Balancing AVL Tree
*/
avl_node* balance(avl_node *temp)
{
int bal_factor = diff (temp);
if (bal_factor > 1)
{
if (diff (temp->left) > 0)
temp = ll_rotation (temp);
else
temp = lr_rotation (temp);
}
else if (bal_factor < -1)
{
if (diff (temp->right) > 0)
temp = rl_rotation (temp);
else
temp = rr_rotation (temp);
}
return temp;
}
/*
* Insert Element into the tree
*/
avl_node* insert(avl_node *root, string value)
{
if (root == NULL)
{
root = new avl_node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
}
else if (value < root->data)
{
root->left = insert(root->left, value);
root = balance (root);
}
else if (value >= root->data)
{
root->right = insert(root->right, value);
root = balance (root);
}
return root;
}
avl_node * minValueNode(avl_node* node)
{
avl_node* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
current = current->left;
return current;
}
bool find(avl_node* root, string data)
{
// STEP 1: PERFORM STANDARD BST DELETE
if (root == NULL)
return false;
// If the data to be deleted is smaller than the
// root's data, then it lies in left subtree
if ( data < root->data ){
return find(root->left, data);
}
// If the data to be deleted is greater than the
// root's data, then it lies in right subtree
else if( data > root->data ){
return find(root->right, data);
}
else
{
return true;
}
}
// Recursive function to delete a node with given data
// from subtree with given root. It returns root of
// the modified subtree.
avl_node* pick(avl_node* root, string data)
{
// STEP 1: PERFORM STANDARD BST DELETE
if (root == NULL)
return root;
// If the data to be deleted is smaller than the
// root's data, then it lies in left subtree
if ( data < root->data )
root->left = pick(root->left, data);
// If the data to be deleted is greater than the
// root's data, then it lies in right subtree
else if( data > root->data )
root->right = pick(root->right, data);
// if data is same as root's data, then This is
// the node to be deleted
else
{
// node with only one child or no child
if( (root->left == NULL) || (root->right == NULL) )
{
avl_node *temp = root->left ? root->left :
root->right;
// No child case
if (temp == NULL)
{
temp = root;
root = NULL;
}
else // One child case
*root = *temp; // Copy the contents of
// the non-empty child
free(temp);
}
else
{
// node with two children: Get the inorder
// successor (smallest in the right subtree)
avl_node* temp = minValueNode(root->right);
// Copy the inorder successor's data to this node
root->data = temp->data;
// Delete the inorder successor
root->right = pick(root->right, temp->data);
}
}
// If the tree had only one node then return
if (root == NULL)
return root;
// STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
//root->height = 1 + max(height(root->left),
// height(root->right));
// STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to
// check whether this node became unbalanced)
balance(root);
return root;
}
void inOrder(avl_node *root){
if(root == NULL){
return;
}
inOrder(root->left);
cout<<root->data<<" ";
inOrder(root->right);
}
};
Given a Binary Tree, find the deepest leaf node that is left child of its parent. For example, consider the following tree. The deepest left leaf node is the node with value 9.
1
/ \
2 3
/ / \
4 5 6
\ \
7 8
/ \
9 10
The answer is 9.
I developed the following code for this:
int maxlevel = 0;
Node *newNode(int data)
{
Node *temp = new Node;
temp->val = data;
temp->left = temp->right = NULL;
return temp;
}
Node * root;
Node * maxi = NULL;
int getlevel (Node * treeroot,int level, Node * foo)
{
if (treeroot == NULL)
return -1;
else if (treeroot->val == foo->val)
return level+1;
else
{
int downlevel = getlevel(treeroot->left,level+1,foo);
if (downlevel != -1)
return downlevel;
else
downlevel = getlevel(treeroot->right,level+1,foo);
return downlevel;
}
}
void foo(Node * temp)
{
// Base case
if (temp == NULL)
return;
Node * prev;
if (temp->left != NULL)
{
prev = temp;
foo(temp->left);
}
if (prev->left != NULL)
{
if (temp->left == NULL && temp->right == NULL && prev->left == temp)
{
int ind = getlevel(root,0,temp);
if (ind > maxlevel)
{
maxlevel = ind;
maxi = temp;
}
}
}
foo(temp->right);
return;
}
Here, foo is the actual function which determines the deepest left leaf in a tree. getlevel is a function which gets the level of a node in a tree. newNode is a function which allocates a new node.
When I try giving this input tree, it says the leaf is not present. Is there something wrong with my logic?
Thanks!
I think you could simplify this a little by passing the current level and a flag indicating whether the current node is a left or right child into each call...
Something like this:
int maxLevel = 0;
Node* maxNode = null;
void findDeepestLeftNode(Node* node, int level, bool isLeftChild) {
bool isLeaf = true;
if (node->left != null) {
isLeaf = false;
findDeepestLeftNode(node->left, level + 1, true);
}
if (node->right != null) {
isLeaf = false;
findDeepestLeftNode(node->right, level + 1, false);
}
if (isLeaf && isLeftChild && level > maxLevel) {
maxLevel = level;
maxNode = node;
}
}
Then just call it with:
findDeepestLeftNode(root, 0, false);
Assumes the root is not null.