DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   Binary Tree - Next Highest Value (http://www.daniweb.com/forums/thread158015.html)

mapidea Nov 18th, 2008 5:56 am
Binary Tree - Next Highest Value
 
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.

dickersonka Nov 18th, 2008 9:51 am
Re: Binary Tree - Next Highest Value
 
show us your code and we can help you out

mapidea Nov 18th, 2008 9:58 am
Re: Binary Tree - Next Highest Value
 
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

dickersonka Nov 18th, 2008 10:00 am
Re: Binary Tree - Next Highest Value
 
please put the code in code tags

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

mapidea Nov 18th, 2008 10:16 am
Re: Binary Tree - Next Highest Value
 
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

dickersonka Nov 18th, 2008 10:19 am
Re: Binary Tree - Next Highest Value
 
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

mapidea Nov 18th, 2008 10:24 am
Re: Binary Tree - Next Highest Value
 
How can I start to convert the code in C# and test it?

dickersonka Nov 18th, 2008 10:29 am
Re: Binary Tree - Next Highest Value
 
did you do it in python or is that some else's?

here's a link that will give you a java version

MAKE AN EFFORT
http://simpleprogrammingtutorials.co...t-overview.php

mapidea Nov 18th, 2008 11:08 am
Re: Binary Tree - Next Highest Value
 
How will you write

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

in C#

dickersonka Nov 18th, 2008 11:14 am
Re: Binary Tree - Next Highest Value
 
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

mapidea Nov 18th, 2008 12:46 pm
Re: Binary Tree - Next Highest Value
 
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;
                }
            }
        }
    }
}

mapidea Nov 18th, 2008 1:05 pm
Re: Binary Tree - Next Highest 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;
                }
            }
        }
    }
}


All times are GMT -4. The time now is 12:07 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC