Question: Rewrite the methods InOrder(Node theRoot) , PreOrder(Node theRoot) , and PostOrder(Node theRoot) in the program below. Each method will take as input a binary search
Rewrite the methods InOrder(Node theRoot), PreOrder(Node theRoot), and PostOrder(Node theRoot) in the program below. Each method will take as input a binary search tree node, T, and two keys k1 and k2, which are ordered so that k1 k2, and prints all elements X in the binary tree such that k1 Key(X) k2.
(2) In the Main method, create a binary search tree having 10 nodes. Then, call three methods rewritten in the previous step.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Node
{
public int Data;
public Node Left;
public Node Right;
public void DisplayNode()
{
Console.Write(Data + " ");
}
}
public class BinarySearchTree
{
public Node root;
public BinarySearchTree()
{
root = null;
}
public void Insert(int i)
{
Node newNode = new Node();
newNode.Data = i;
if (root == null)
{
root = newNode;
}
else
{
Node current = root;
Node parent;
while (true)
{
parent = current;
if (i < current.Data)
{
current = current.Left;
if (current == null)
{
parent.Left = newNode;
break;
}
}
else
{
current = current.Right;
if (current == null)
{
parent.Right = newNode;
break;
}
}
}//end of while(true)
}//end of else
}//end of public void Insert(int i)
/*
// rewrite 3 methods below: each method will take as input a binary search tree node, T,
// and two keys k1 and k2, which are ordered so that k1 < k2, and prints all elements X in
// the binary tree such that k1 < Key(X) < k2.
public void InOrder(Node theRoot)
{
if (!(theRoot == null))
{
InOrder(theRoot.Left);
theRoot.DisplayNode();
InOrder(theRoot.Right);
}
}
public void PreOrder(Node theRoot)
{
if (!(theRoot == null))
{
theRoot.DisplayNode();
PreOrder(theRoot.Left);
PreOrder(theRoot.Right);
}
}
public void PostOrder(Node theRoot)
{
if (!(theRoot == null))
{
PostOrder(theRoot.Left);
PostOrder(theRoot.Right);
theRoot.DisplayNode();
}
}
*/
static void Main()
{
/*
(1) create a binary search tree having 10 nodes
(2) call 3 methods having been rewritten
*/
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
