Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Binary Search Tree
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { var tree = new SearchTree(); var isBinarySearchTree = tree.IsBinarySearchTree(); Console.Out.WriteLine(isBinarySearchTree); } } public class SearchTree { public Node Root {get; set;} public SearchTree() { /* 5 2 8 1 6 */ var node1 = new Node (1, null, null); var node6 = new Node (6, null, null); var node2 = new Node (2, node6, node1); var node8 = new Node (8, null, null); var root = new Node (5, node8, node2); this.Root = root; } public bool IsBinarySearchTree() { List<int> items = new List<int>(); TraverseInOrder(this.Root, items); foreach( var i in items) Console.Out.WriteLine(i); var orderedItems = items.OrderBy(i => i); return items.SequenceEqual(orderedItems); } public void TraverseInOrder(Node root, IList<int> items ) { if (root == null) return; TraverseInOrder(root.Left, items); items.Add(root.Value); TraverseInOrder(root.Right, items); } } public class Node { public int Value {get; set;} public Node Right {get; set;} public Node Left {get; set;} public Node(int val, Node right, Node left) { this.Value = val; this.Right = right; this.Left = left; } } }
run
|
edit
|
history
|
help
0
Finding a Substring in a Main String
Fibonacci of array elements C#
UINT32toHEX
Ccc
AdventCode
Strings: Padding Integers and Handling Decimal Float Numbers
Delegate, Method, List, Classes, Conditions
Numbers in Interval Dividable by Given Number
LevenshteinDistance
ProWIN2