मैं BinarySearchTree में _left और _right क्यों नहीं कर सकते हैं?

वोट
2

मैं निम्नलिखित कोड का टुकड़ा के साथ समस्या हो रही है:

using System;
using System.Collections.Generic;
using System.Text;

namespace trees_by_firas
{
    class Program
    {
        static void Main(string[] args)
        {
            BinarySearchTree t = new BinarySearchTree();

            t.insert(ref t.root, 10);
            t.insert(ref t.root, 5);
            t.insert(ref t.root, 6);
            t.insert(ref t.root, 17);
            t.insert(ref t.root, 2);
            t.insert(ref t.root, 3);

            BinarySearchTree.print(t.root);
            Console.WriteLine(--------------------);
            Console.WriteLine(t.FindMax());
            Console.WriteLine(t.FindMin());
            Console.WriteLine(--------------------);
            Console.WriteLine(t.CountLeaves());
            Console.WriteLine(t.CountNodes());



        }

        public class TreeNode
        {
            public int n;
            public TreeNode _left;
            public TreeNode _right;


            public TreeNode(int n, TreeNode _left, TreeNode _right)
            {
                this.n = n;
                this._left = _left;
                this._right = _right;
            }


            public void DisplayNode()
            {
                Console.Write(n);
            }


        }


        public class BinarySearchTree
        {
            public TreeNode root;


            public BinarySearchTree()
            {
                root = null;
            }


            public void insert(ref TreeNode root, int x)
            {
                if (root == null)
                {
                    root = new TreeNode(x, null, null);
                }
                else
                    if (x < root.n)
                        insert(ref root._left, x);
                    else
                        insert(ref root._right, x);
            }

            public int FindMin()
            {
                TreeNode current = root;

                while (current._left != null)
                    current = current._left;

                return current.n;
            }

            public int FindMax()
            {
                TreeNode current = root;

                while (current._right != null)
                    current = current._right;

                return current.n;
            }



            public TreeNode Find(int key)
            {
                TreeNode current = root;

                while (current.n != key)
                {
                    if (key < current.n)
                        current = current._left;
                    else
                        current = current._right;
                    if (current == null)
                        return null;
                }
                return current;
            }



            public void InOrder(ref TreeNode root)
            {
                if (root != null)
                {
                    InOrder(ref root._left);
                    root.DisplayNode();
                    InOrder(ref root._right);
                }
            }

            public int CountNodes()
            {
                int count = 1; // me!        
                if (root._left != null)
                    count += _left.CountNodes();
                if (root._right != null)
                    count += _right.CountNodes();
                return count;
            }

            public int CountLeaves()
            {
                int count = (root._left == null && root._right == null) ? 1 : 0;
                if (root._left != null)
                    count += _left.CountLeaves();
                if (root._right != null)
                    count += _right.CountLeaves();
                return count;
            }

            public static void print(TreeNode root)
            {
                if (root != null)
                {
                    print(root._left);
                    Console.WriteLine(root.n.ToString());
                    print(root._right);
                }

            }



        }

    }
}

मैं निम्नलिखित त्रुटियाँ मिलती है:

Error 1 The name '_left' does not exist in the current context 

// on the countnodes & countleaves

Error 2 The name '_right' does not exist in the current context 
// on the countnodes & countleaves

मैं इन त्रुटियों को ठीक करके उसे पर कोई विचार?

02/01/2009 को 14:31
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


2 जवाब

वोट
2

_left और _right TreeNode में क्षेत्र हैं। आप उन्हें उपयोग करने के लिए रूप में यदि वे BinarySearchTree का हिस्सा हैं कोशिश कर रहे हैं। मेरा मानना है कि आप बस के साथ उन्हें उपसर्ग कर सकते हैं root.:

public int CountNodes()
{
    int count = 1; // me!        
    if (root._left != null)
        count += root._left.CountNodes();
    if (root._right != null)
        count += root._right.CountNodes();
    return count;
}

public int CountLeaves()
{
    int count = (root._left == null && root._right == null) ? 1 : 0;
    if (root._left != null)
        count += root._left.CountLeaves();
    if (root._right != null)
        count += root._right.CountLeaves();
    return count;
}
02/01/2009 को 14:36
का स्रोत उपयोगकर्ता

वोट
1

मैं अपने कोड से काफी विपरीत परिणाम मिलता है - 6 नोड्स और 3 पत्ते। 6 नोड्स आइटम आप पेड़ में डाला है की संख्या है, इसलिए इस समझ में आता है। पत्तियों की संख्या पेड़ नहीं संतानों की नोड्स की संख्या होनी चाहिए। अपने पेड़ के रूप में वर्तमान में इस तरह दिखता है ...

     10
    /  \
   5   17
  / \
 2   6
  \
   3

... आप छह नोड्स और तीन पत्तियों (17,6 और 3) है।

03/01/2009 को 15:41
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more