Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Binary search tree - Pre-order Traversal
//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) { // Pre-order Traversal - the root node is visited first, then the left subtree and finally the right subtree. //Your code goes here Console.WriteLine("Hello, world!"); Tree bsTree = new Tree(); bsTree.Insert(2); bsTree.Insert(4); bsTree.Insert(6); bsTree.Insert(8); bsTree.Insert(10); bsTree.Insert(12); bsTree.Insert(1); bsTree.Insert(3); bsTree.Insert(5); bsTree.Insert(7); bsTree.Insert(9); bsTree.Insert(11); Console.WriteLine("Preorder Traversal : "); bsTree.Preorder(bsTree.ReturnRoot()); Console.WriteLine(" "); Console.WriteLine(); } } class Node { public int item; public Node left; public Node right; public void display() { Console.Write(" "); Console.Write(item); Console.Write(" "); } } class Tree { public Node root; public Tree() { root = null; } public Node ReturnRoot() { return root; } public void Insert(int id) { Node newNode = new Node(); newNode.item = id; if (root == null) root = newNode; else { Node tempNode = root; while (true) { if (id < tempNode.item) { if (tempNode.left == null) { tempNode.left = newNode; return; } tempNode = tempNode.left; } else { if (tempNode.right == null) { tempNode.right = newNode; return; } tempNode = tempNode.right; } } } } public void Preorder(Node Root) { if (Root != null) { Root.display(); Preorder(Root.left); Preorder(Root.right); } } } }
run
|
edit
|
history
|
help
0
Hello-Heath-World
Reflekcja assembly, instancja i metoda
gass
compra de restaurante
Joro Football Player
Arreglo
asdfghjujytgrf
Operator Overloading Example (+, -, *, /, ==, !=)
12
Simple Selection Sort