using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DSA_ASSIGNMENT_3_Q6{ class node { public int value; public node left; public node right; } class tree { public node root = null; public tree() { root = null; } public node returnroot() { return root; } public void add(int e) { node n = new node(); n.value = e; if (root == null) { root = n; } else { node current = root; node parent; while (true) { parent = current; if (e < current.value) { current = current.left; if (current == null) { parent.left = n; return; } } else { current = current.right; if (current == null) { parent.right = n; return; } } } } } public node search(node root, int f) { node left, right; if (root != null) { if (f == root.value) { return root; } else { left = search(root.left, f); right = search(root.right, f); if (f == left.value) { return left; } else { return right; } } } return root; } public bool delete(node root, int target) { node x = root, p = null; while (x != null) { if (target == x.value) { break; } p = x; x = target < x.value ? x.left : x.right; } if (x == null) { Console.WriteLine("
Given value does not exist"); return false; } //Node has both child if (x.left != null && x.right != null) { node y = x.left; p = x; while (y.right != null) { p = y; y = y.right; } x.value = y.value; x = y; } //leaf and 1 child case if (p == null) { return false; } node tmp = x.left != null ? x.left : x.right; if (x == p.right) { p.right = tmp; } else { p.left = tmp; } return false; } public void inorder(node root) { if (root != null) { inorder(root.left); Console.Write(root.value + "->“); inorder(root.right); } } public void preorder(node root) { if (root != null) { Console.Write(root.value + “->”); preorder(root.left); preorder(root.right); } } public void postorder(node root) { if (root != null) { postorder(root.left); postorder(root.right); Console.Write(root.value + “->”); } } } class Program { static void Main(string args) { tree bt = new tree(); bt.add(34); bt.add(15); bt.add(65); bt.add(62); bt.add(69); bt.add(42); Console.WriteLine(“inorder Traversal: “); bt.inorder(bt.returnroot()); Console.WriteLine(”
“); Console.WriteLine(“Preorder Traversal: “); bt.preorder(bt.returnroot()); Console.WriteLine(”
“); Console.WriteLine(“Postorder Traversal: “); bt.postorder(bt.returnroot()); Console.Write(”
Enter value to be searched: “); int f = int.Parse(Console.ReadLine()); Console.WriteLine(”
Searched Value: {0}”, bt.search(bt.returnroot(), f)); Console.Write(”
Enter value to delete: “); int del = int.Parse(Console.ReadLine()); bt.delete(bt.returnroot(), del); Console.WriteLine(”
After Deletion:”); Console.WriteLine(“inorder Traversal: “); bt.inorder(bt.returnroot()); Console.WriteLine(”
“); Console.WriteLine(); Console.ReadLine(); } }}