can somebody pl.s help me out. I'm doing a binarytree project and I have all the methods I was asked of me but he wants me to create a delete method. I have two class and a main tester class.
here's my code:
this is the binarytree class
Here's the binarytreenode classCode:using System; namespace binary_tree { public class BinaryTree { private BinaryTreeNode root; public BinaryTree() { root = null; } public bool IsEmpty() { return (root == null); } private void InorderHelper(BinaryTreeNode p) { if (p != null) { InorderHelper(p.Left); System.Console.WriteLine(p.Data + ""); InorderHelper(p.Right); } } public void Inorder() { InorderHelper(root); } private void PostOrderHelper(BinaryTreeNode p) { if (p != null) { PostOrderHelper(p.Left); PostOrderHelper(p.Right); System.Console.WriteLine(p.Data + ""); } } public void Postorder() { PostOrderHelper(root); } public void Preorder() { PreOrderHelper(root); } public void PreOrderHelper(BinaryTreeNode p) { if (p != null) { System.Console.WriteLine(p.Data + ""); PreOrderHelper(p.Left); PreOrderHelper(p.Right); } } public int CountNodes() { return CountNodesHelper(root); } private int CountNodesHelper(BinaryTreeNode rootNode) { if(rootNode == null) return 0; else return 1 + (CountNodesHelper(rootNode.Left) + CountNodesHelper(rootNode.Right)); } public void Insert(int InsertItem) { BinaryTreeNode current; BinaryTreeNode parent = null; BinaryTreeNode newNode; newNode = new BinaryTreeNode(InsertItem); if(IsEmpty()) { root = newNode; } else { current = root; while(current != null) { parent = current; if(current.Data == InsertItem) { System.Console.WriteLine("Duplicates are not allowed"); return; } else { if(InsertItem < current.Data) { current = current.Left; } else current = current.Right; } } if(InsertItem < parent.Data) { parent.Left = newNode; } else { parent.Right = newNode; } } } public bool Search(int item) { BinaryTreeNode current; bool match = false; if(IsEmpty()) { System.Console.WriteLine("Tree is empty"); } else { current = root; while(current != null && !match) { if(current.Data == item) { match = true; } else { if(current.Data > item) { current = current.Left; } else { current = current.Right; } } } } return match; } public void Delete(int deleteItem) ///have no idea what to do for this method. I searched the net but only gave me delete for other programming language. Pls. help if you can.
here's the tester:Code:using System; namespace binary_tree { public class BinaryTreeNode { private int info; private BinaryTreeNode llink; private BinaryTreeNode rlink; public BinaryTreeNode() { info = 0; llink = rlink = null; } public BinaryTreeNode(int data) { info = data; llink = rlink = null; } public BinaryTreeNode Left { set {llink = value;} get {return llink;} } public BinaryTreeNode Right { set {rlink = value;} get {return rlink;} } public int Data { set {info = value;} get {return info; } } } }
I did most of the stuff but I need a delete for binarytree class.Code:using System; using s = System.Console; namespace binary_tree { public class BinaryTester { public void print(BinaryTreeNode p) { SidePrint(p,0,"R-"); } public void SidePrint(BinaryTreeNode p,int Level,string d) { int k = 0; if(p != null) { SidePrint(p.Right,Level + 1,"/"); for(;k < 3 * Level;k++) { System.Console.Write(" "); } System.Console.WriteLine(d + p.Data); SidePrint(p.Left,Level + 1,"\\"); } } public void Msg(string msg) { s.Write( msg); } [STAThread] static void Main(string[] args) { int []nodeValue = {60,65,45,46,40,35,50,61,70,66,80,42}; BinaryTester tester = new BinaryTester(); BinaryTree bt = new BinaryTree(); tester.Msg("1.1-Loading Binary Tree"); for(int i = 0; i < nodeValue.Length;i++) { bt.Insert(nodeValue[i]); } tester.Msg("\n1.2-Add Duplicate data: "); bt.Insert(60); tester.Msg("1.3-Is empty : " + bt.IsEmpty()); tester.Msg("\n1.4-Count Node: " + bt.CountNodes()); tester.Msg("\n1.5-Search for 80: " + bt.Search(0x50)); tester.Msg("\n1.6-Search for 255: " + bt.Search(0xff) ); BinaryTree bt2 = new BinaryTree(); tester.Msg("\n1.7-Search Empty Tree for 80: " ); bt2.Search(0xff); tester.Msg("1.8-Access Root: "); BinaryTreeNode root = bt.Root; tester.Msg(""+(root != null)); tester.Msg("\n1.9-Print tree: \n" ); tester.print(root); tester.Msg("\n"); tester.Msg("\n1.10-InOrder: "); bt.Inorder(); tester.Msg("\n1.11-PostOrder: "); bt.Postorder(); tester.Msg("\n1.12-PreOrder: "); bt.Preorder(); tester.Msg("\n1.13-Delete from tree value = 60: "); bt.Delete(60); tester.Msg("\n1.14-Delete from tree value = 66: \n"); bt.Delete(66); tester.Msg("1.15-Delete from tree value = 66:"); bt.Delete(66); tester.Msg("1.16-Delete from tree value = 42: \n"); bt.Delete(42); tester.print(root); tester.Msg("\n1.17-Count Node: " + bt.CountNodes()); tester.Msg("\n1.18-Is Empty: " + bt.IsEmpty()); tester.Msg("\nCongratulations Your assignment is ready to submit\nPlease don't forget to remove the BinaryTester.cs\n"); s.ReadLine(); } } }
thanks to anybody who helpsCode:public void Delete(int deleteItem) ///have no idea what to do for this method. I searched the net but only gave me delete for other programming language. Pls. help if you can.




Reply With Quote