Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 102925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:02:33+00:00 2026-05-11T01:02:33+00:00

HI i did this code with help of Marc Gravell in Why can't I

  • 0

HI i did this code with help of Marc Gravell in

Why can't I find _left and _right in BinarySearchTree?
&
How do I correct an implicit conversion error in BST C# Code?

, its Binary search tree , but now I’m having logical error the results coming are wrong the output of my code is the following:

2 3 5 6 10 17 ------------------------------------------------ 17 2 ------------------------------------------------ 3                  6 Press any key to continue . . . 

the last two number must give the total of inserted elements 6 but its showing 9

and but the way How can i get the height of the tree ?!


using System; using System.Collections.Generic; using System.Text;  namespace errors {     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(t.root));             Console.WriteLine(t.CountNodes(t.root));          }          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(TreeNode root)             {                 int count=1;                 if (root._left != null)                     count += CountNodes(root._left);                 if (root._right != null)                     count += CountNodes(root._right);                 return count;             }              public int CountLeaves(TreeNode root)             {                 int count = (root._left == null && root._right == null) ? 1 : 0;                 if (root._left != null)                     count += CountLeaves(root._left);                 if (root._right != null)                     count += CountLeaves(root._right);                 return count;             }               public static void print(TreeNode root)             {                 if (root != null)                 {                     print(root._left);                     Console.WriteLine(root.n.ToString());                     print(root._right);                 }              }            }      } } 

Thanx in advance and special thanx to Marc Gravell .

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:02:34+00:00Added an answer on May 11, 2026 at 1:02 am

    If what you mean in CountNodes is to count all non-leaf nodes you must change this line:

    int count=1; 

    to read this:

    int count = (root._left == null && root._right == null) ? 0 : 1; 

    (the opposite of what is in CountLeaves).

    And this will get you the height of the tree:

    public int Height(TreeNode root) {     int height = 1;     if (root._left != null)         height = Math.Max(height, Height(root._left));     if (root._right != null)         height = Math.Max(height, Height(root._right));     return height;    } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did this but it didn't work. Can anyone help me? Thanks. $('.current_element').mouseover(function() {
Couldn't find anything relevant in forums So ,Please help me with this code .I'm
I cannot figure out why this code will not work. Can anyone help? I
according to this code i dont understand about NSClassFromString how did i know viewControllerName
Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;
This is the code i have. The NSDictionary did used to be something, but
I just recently found myself writing this line of code, which i did not
I want v0.1.27 of nodejs code base. This is what I did. git clone
I did this code in my presentation layer but i don't know how to
First of all I did not write this code. I found it on somebody

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.