using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 二叉排序树
{
class Program
{
public class Node
{
public int data;
public Node left;
public Node right;
}
public class BinarySearchTree
{
Node rootNode = null;
public void Insert(int data)
{
Node parent;
Node newNode = new Node();
newNode.data = data;
if (rootNode == null)
{
rootNode = newNode;
}
else
{
Node currentNode = rootNode;
while (true)
{
parent = currentNode;
if (newNode.data < currentNode.data)
{
currentNode = currentNode.left;
if (currentNode == null)
{
parent.left = newNode;
break;
}
}
else
{
currentNode = currentNode.right;
if (currentNode == null)
{
parent.right = newNode;
break;
}
}
}
}
}
public void search(int data)
{
Node currentNode = rootNode;
while (true)
{
if (currentNode == null)
{
break;
}
else
{
if (data < currentNode.data)
{
Console.Write(currentNode.data + ' ');
currentNode = currentNode.left;
}
if (data > currentNode.data)
{
Console.Write(currentNode.data + " ");
currentNode = currentNode.right;
}
if (data == currentNode.data)
{
Console.Write(data);
break;
}
}
}
}
}
static void Main(string[] args)
{
BinarySearchTree BST = new BinarySearchTree();
string str = Console.ReadLine();
string[] s = str.Split(' ');
List<int> LI = new List<int>();
for (int i = 0; i <s.Length; i++)
{
LI.Add(Convert.ToInt16(s[i]));
}
for (int i = 0; i < LI.Count; i++)
{
BST.Insert(LI[i]);
}
string val = Console.ReadLine();
BST.search(Convert.ToInt16(val));
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 二叉排序树
{
class Program
{
public class Node
{
public int data;
public Node left;
public Node right;
}
public class BinarySearchTree
{
Node rootNode = null;
public void Insert(int data)
{
Node parent;
Node newNode = new Node();
newNode.data = data;
if (rootNode == null)
{
rootNode = newNode;
}
else
{
Node currentNode = rootNode;
while (true)
{
parent = currentNode;
if (newNode.data < currentNode.data)
{
currentNode = currentNode.left;
if (currentNode == null)
{
parent.left = newNode;
break;
}
}
else
{
currentNode = currentNode.right;
if (currentNode == null)
{
parent.right = newNode;
break;
}
}
}
}
}
public void search(int data)
{
Node currentNode = rootNode;
while (true)
{
if (currentNode == null)
{
break;
}
else
{
if (data < currentNode.data)
{
Console.Write(currentNode.data + ' ');
currentNode = currentNode.left;
}
if (data > currentNode.data)
{
Console.Write(currentNode.data + " ");
currentNode = currentNode.right;
}
if (data == currentNode.data)
{
Console.Write(data);
break;
}
}
}
}
}
static void Main(string[] args)
{
BinarySearchTree BST = new BinarySearchTree();
string str = Console.ReadLine();
string[] s = str.Split(' ');
List<int> LI = new List<int>();
for (int i = 0; i <s.Length; i++)
{
LI.Add(Convert.ToInt16(s[i]));
}
for (int i = 0; i < LI.Count; i++)
{
BST.Insert(LI[i]);
}
string val = Console.ReadLine();
BST.search(Convert.ToInt16(val));
Console.Read();
}
}
}