How can we find the next highest value in the binary tree?

We have a binary tree and an number is being input. We need to find the next highest number.

What will be the C# version of the below code.

int findSuccessor(self):
succ = None
if self.rightChild:
succ = self.rightChild.findMin()
else:
if self.parent.leftChild == self:
succ = self.parent
else:
self.parent.rightChild = None
succ = self.parent.findSuccessor()
self.parent.rightChild = self
return succ

int findMin(self):
n = self
while n.leftChild:
n = n.leftChild
print 'found min, key = ', n.key
return n

I have not done this in C#.

int findSuccessor(self):
succ = None
if self.rightChild:
succ = self.rightChild.findMin()
else:
if self.parent.leftChild == self:
succ = self.parent
else:
self.parent.rightChild = None
succ = self.parent.findSuccessor()
self.parent.rightChild = self
return succ

int findMin(self):
n = self
while n.leftChild:
n = n.leftChild
print 'found min, key = ', n.key
return n

then make an effort, and we will make an effort

if you are having trouble with a specific piece, rather than just though whole thing then let us know, we won't do the work for you

How will you write

if self.rightChild:
        succ = self.rightChild.findMin()

in C#

in c# you have classes, think of it this way

you have a node, and a node on the left and right

each node is connected to another node (rightChild) and (leftChild)

you can't just translate this code to c#, you need to think about what structure you need first

I have a binary tree

50

30 70

10 40 60 80

The below code is not giving the correct result for test cases 80, 83, 60, 11

public partial class Default2 : System.Web.UI.Page
{
   
    public class FindNextHighestNuber
    {
        public int search(int InputNumber)
        {
            int NextHighestNuber = "";
            int NodeValue = this.RootNode.value;

            ActionOnNode(NodeValue, InputNumber);

        }

        public int ActionOnNode(int NodeValue, int InputNumber)
        {
            if (NodeValue <= InputNumber)
            {
                if (this.RightChild.value != "")
                {
                    NodeValue = this.RightChild.value;
                    ActionOnNode(NodeValue, InputNumber);
                }
                else
                {
                    return this.Parent.value;
                }
            }
            else
            {
                if (this.LeftChild.value != "")
                {
                    NodeValue = this.LeftChild.value;
                    ActionOnNode(NodeValue, InputNumber);
                }
                else
                {
                    return this.LeftChild.value;
                }
            }
        }
    }
}

We have a binary tree

50
30 70

10 40 60 80


The following code doesn't work for 80, 83 , 60, 40

public partial class Default2 : System.Web.UI.Page
{
   
    public class FindNextHighestNuber
    {
        public int search(int InputNumber)
        {
            int NextHighestNuber = "";
            int NodeValue = this.RootNode.value;

            ActionOnNode(NodeValue, InputNumber);

        }

        public int ActionOnNode(int NodeValue, int InputNumber)
        {
            if (NodeValue <= InputNumber)
            {
                if (this.RightChild.value != "")
                {
                    NodeValue = this.RightChild.value;
                    ActionOnNode(NodeValue, InputNumber);
                }
                else
                {
                    return this.Parent.value;
                }
            }
            else
            {
                if (this.LeftChild.value != "")
                {
                    NodeValue = this.LeftChild.value;
                    ActionOnNode(NodeValue, InputNumber);
                }
                else
                {
                    return this.LeftChild.value;
                }
            }
        }
    }
}
This article has been dead for over six months. Start a new discussion instead.