using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MvcTreeView{ public class BinaryTree { static void Main(string args) { BinaryTreeSubClass BT = new BinaryTreeSubClass(); BT.insert_Node(1); BT.insert_Node(6); BT.insert_Node(2); BT.insert_Node(4); BT.insert_Node(5); BT.insert_Node(3); BT.display(); BT.search(3); Console.ReadLine(); } } public class BinaryTreeSubClass { private BinaryTree_Node root; private int count; public BinaryTreeSubClass() { root = null; count = 0; } public bool isEmpty() { return root == null; } public void insert_Node(int d) { if (isEmpty()) { root = new BinaryTree_Node(d); } else { root.insertData(ref root, d); } count++; } public bool search(int s) { return root.search(root, s); } public bool isLeaf() { if (!isEmpty()) return root.isLeaf(ref root); return true; } public void display() { if (!isEmpty()) root.display(root); } public int Count() { return count; } public void DeleteNode(int x) { BinaryTree_Node deleteNode = new BinaryTree_Node(x); deleteNode.DeleteNode(root, deleteNode); } } private class BinaryTree_Node { private int number; public BinaryTree_Node rightLeaf; public BinaryTree_Node leftLeaf; public BinaryTree_Node(int value) { number = value; rightLeaf = null; leftLeaf = null; } public bool isLeaf(ref BinaryTree_Node node) { return (node.rightLeaf == null && node.leftLeaf == null); } public void insertData(ref BinaryTree_Node node, int data) { if (node == null) { node = new BinaryTree_Node(data); } else if (node.number < data) { insertData(ref node.rightLeaf, data); } else if (node.number > data) { insertData(ref node.leftLeaf, data); } } public bool search(BinaryTree_Node node, int s) { if (node == null) return false; if (node.number == s) { return true; } else if (node.number < s) { return search(node.rightLeaf, s); } else if (node.number > s) { return search(node.leftLeaf, s); } return false; } public void display(BinaryTree_Node n) { if (n == null) return; display(n.leftLeaf); Console.Write(” ” + n.number); display(n.rightLeaf); } public BinaryTree_Node DeleteNode(BinaryTree_Node root, BinaryTree_Node deleteNode) { if (deleteNode.number < root.number) { root.leftLeaf = DeleteNode(root.leftLeaf, deleteNode); } if (deleteNode.number > root.number) { root.rightLeaf = DeleteNode(root.rightLeaf, deleteNode); } if (deleteNode.number == root.number) { if (root.leftLeaf == null && root.rightLeaf == null) { root = null; return root; } else if (root.leftLeaf == null) { BinaryTree_Node temporary = root; root = root.rightLeaf; temporary = null; } else if (root.rightLeaf == null) { BinaryTree_Node temporary = root; root = root.leftLeaf; temporary = null; } } return root; }}}