Note that there are some explanatory texts on larger screens.

plurals
  1. POLogical error BST ... wrong results
    text
    copied!<p>HI i did this code with help of Marc Gravell in </p> <p><a href="https://stackoverflow.com/questions/406791/">Why can&#39;t I find _left and _right in BinarySearchTree?</a><br> &amp;<br> <a href="https://stackoverflow.com/questions/406402/bst-c-code-with-errors">How do I correct an implicit conversion error in BST C# Code?</a></p> <p>, its Binary search tree , but now I'm having logical error the results coming are wrong the output of my code is the following:</p> <pre><code>2 3 5 6 10 17 ------------------------------------------------ 17 2 ------------------------------------------------ 3 6 Press any key to continue . . . </code></pre> <p>the last two number must give the total of inserted elements 6 but its showing 9</p> <p>and but the way How can i get the height of the tree ?!</p> <hr> <pre><code>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 &lt; 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 &lt; 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 &amp;&amp; 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); } } } } } </code></pre> <p>Thanx in advance and special thanx to Marc Gravell .</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload