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.

show us your code and we can help you out

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

please put the code in code tags

do you have any of this done yourself in c# yet?

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 can I start to convert the code in C# and test it?

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;
                }
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.