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 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 6:05 pm.

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