/// <summary> /// This method inserts a new cell containing that NameInformation in sorted order by name in the given list. /// It returns a reference to the first cell in the list. /// </summary> /// <param name="nameInfo">name information that is needed to be insert</param> /// <param name="firstCell">a reference to the first cell in a linked list</param> /// <returns></returns> private LinkedListCell <NameInformation> Insert(NameInformation nameInfo, LinkedListCell <NameInformation> firstCell) { LinkedListCell <NameInformation> cell = new LinkedListCell <NameInformation>(); cell.Data = nameInfo; if (firstCell == null) { cell.Data = nameInfo; return(cell); } else if (nameInfo.Name.CompareTo(firstCell.Data.Name) < 0) { cell.Next = firstCell; firstCell = cell; return(firstCell); } LinkedListCell <NameInformation> tempCell = firstCell; while (tempCell.Next != null) { if (nameInfo.Name.CompareTo(tempCell.Next.Data.Name) > 0) { tempCell = tempCell.Next; } else { cell.Next = tempCell.Next; tempCell.Next = cell; return(firstCell); } } tempCell.Next = cell; return(firstCell); }
/// <summary> /// Gets binary tree information. /// </summary> /// <param name="name"></param> /// <param name="t"></param> /// <returns></returns> private NameInformation GetInformation(string name, BinaryTreeNode <NameInformation> t) { NameInformation result = new NameInformation(); if (t == null) { return(result); } else { int temp = t.Data.Name.CompareTo(name); if (temp == 0) { return(t.Data); } else if (temp < 0) { return(GetInformation(name, t.RightChild)); } else { return(GetInformation(name, t.LeftChild)); } } }
/// <summary> /// Builds the binary search tree that results from removing the NameInformation containing the given name from the given tree. /// </summary> /// <param name="name">The name to remove.</param> /// <param name="t">The binary search tree from which to remove the name.</param> /// <param name="removed">Indicates whether the name was found.</param> /// <returns>The resulting binary search tree.</returns> private BinaryTreeNode <NameInformation> Remove(string name, BinaryTreeNode <NameInformation> t, out bool removed) { if (t == null) { removed = false; return(t); } else { if (t.Data.Name == name) { removed = true; if (t.LeftChild == null && t.RightChild == null) { return(null); } else if (t.RightChild == null) { return(t.LeftChild); } else if (t.LeftChild == null) { return(t.RightChild); } else //if (t.LeftChild != null && t.RightChild != null) { removed = true; NameInformation newTemp; BinaryTreeNode <NameInformation> newRight = RemoveMin(t.RightChild, out newTemp); BinaryTreeNode <NameInformation> newLeft = t.LeftChild; return(new BinaryTreeNode <NameInformation>(newTemp, newLeft, newRight)); } } else if (t.Data.Name.CompareTo(name) > 0) { BinaryTreeNode <NameInformation> newLeft = Remove(name, t.LeftChild, out removed); BinaryTreeNode <NameInformation> newRight = t.RightChild; NameInformation newData = t.Data; return(new BinaryTreeNode <NameInformation>(newData, newLeft, newRight)); } else //if (t.Data.Name.CompareTo(name) < 0) { BinaryTreeNode <NameInformation> newRight = Remove(name, t.RightChild, out removed); BinaryTreeNode <NameInformation> newLeft = t.LeftChild; NameInformation newData = t.Data; return(new BinaryTreeNode <NameInformation>(newData, newLeft, newRight)); } } }
/// <summary> /// Handles a Click event on the Get Statistics button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxLookup_Click(object sender, EventArgs e) { NameInformation temp = _names.Find(new NameInformation(uxName.Text.Trim().ToUpper(), 0, 0)); if (temp.Name == null) { MessageBox.Show("Name not found."); uxRank.Text = ""; uxFrequency.Text = ""; } else { uxFrequency.Text = temp.Frequency.ToString(); uxRank.Text = temp.Rank.ToString(); return; } }
/// <summary> /// Handles a Click event on the Get Statistics button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxLookup_Click(object sender, EventArgs e) { string name = uxName.Text.Trim().ToUpper(); NameInformation info = GetInformation(name, _names); if (info.Name == null) { MessageBox.Show("Name not found."); uxFrequency.Text = ""; uxRank.Text = ""; } else { uxFrequency.Text = info.Frequency.ToString(); uxRank.Text = info.Rank.ToString(); } }
/// <summary> /// Reads the given file into a lined list. /// </summary> /// <param name="fn">The name of the file to read.</param> /// <returns>A linked list containing the information from the file.</returns> private List <NameInformation> ReadFile(string fn) { List <NameInformation> list = new List <NameInformation>(); using (StreamReader input = new StreamReader(fn)) { while (!input.EndOfStream) { string name = input.ReadLine().Trim(); float freq = Convert.ToSingle(input.ReadLine()); int rank = Convert.ToInt32(input.ReadLine()); NameInformation temp = new NameInformation(name, freq, rank); list.Add(temp); } return(list); } }
/// <summary> /// Reads in the File /// </summary> /// <param name="FileName"></param> /// <returns></returns> private LinkedListCell <NameInformation> ReadIn(String FileName) { _info = null; LinkedListCell <NameInformation> info = new LinkedListCell <NameInformation>(); using (StreamReader input = new StreamReader(FileName)) { while (!input.EndOfStream) { LinkedListCell <NameInformation> temp = new LinkedListCell <NameInformation>(); string Name = input.ReadLine().Trim(); float Frequency = Convert.ToSingle(input.ReadLine()); int Rank = Convert.ToInt32(input.ReadLine()); NameInformation x = new NameInformation(Name, Frequency, Rank); temp.Data = x; temp.Next = info; info = temp; } } return(info); }
/// <summary> /// Adds binary tree information. /// </summary> /// <param name="info"></param> /// <param name="t"></param> /// <returns></returns> private BinaryTreeNode <NameInformation> Add(NameInformation info, BinaryTreeNode <NameInformation> t) { if (t == null) { return(new BinaryTreeNode <NameInformation>(info, null, null)); } int temp = info.Name.CompareTo(t.Data.Name); if (temp < 0) { return(new BinaryTreeNode <NameInformation>(t.Data, Add(info, t.LeftChild), t.RightChild)); } else if (temp == 0) { throw new ArgumentException(); } else { return(new BinaryTreeNode <NameInformation>(t.Data, t.LeftChild, Add(info, t.RightChild))); } }
/// <summary> /// Handles a Click event on the Get Statistics button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxLookup_Click(object sender, EventArgs e) { string name = uxName.Text.Trim().ToUpper(); NameInformation info = new NameInformation(name, 0, 0); NameInformation t = _names.Find(info); if (t.Name == null) { MessageBox.Show("Name not found."); uxRank.Text = ""; uxFrequency.Text = ""; } else { uxFrequency.Text = t.Frequency.ToString(); uxRank.Text = t.Rank.ToString(); return; } }
/// <summary> /// Removes a node from the binary search tree. /// </summary> /// <param name="name"></param> /// <param name="t"></param> /// <param name="removed"></param> /// <returns></returns> private BinaryTreeNode <NameInformation> Remove(string name, BinaryTreeNode <NameInformation> t, out bool removed) { if (t == null) { removed = false; return(t); } else { int temp = name.CompareTo(t.Data.Name); if (temp == 0) { removed = true; if (t.LeftChild == null) { return(t.RightChild); } else if (t.RightChild == null) { return(t.LeftChild); } else { NameInformation info = new NameInformation(); BinaryTreeNode <NameInformation> x = RemoveMininumName(t.RightChild, out info); return(new BinaryTreeNode <NameInformation>(info, t.LeftChild, x)); } } else if (temp < 0) { return(new BinaryTreeNode <NameInformation>(t.Data, Remove(name, t.LeftChild, out removed), t.RightChild)); } else { return(new BinaryTreeNode <NameInformation>(t.Data, t.LeftChild, Remove(name, t.RightChild, out removed))); } } }
/// <summary> /// Builds the binary search tree that results from adding the given NameInformation to the given tree. /// </summary> /// <param name="info">The data to add to the tree.</param> /// <param name="t">The binary search tree to which the data will be added.</param> /// <returns>The resulting tree.</returns> private BinaryTreeNode <NameInformation> Add(NameInformation nameInformation, BinaryTreeNode <NameInformation> t) { //throw new NotImplementedException(); if (t == null) { return(new BinaryTreeNode <NameInformation>(nameInformation, null, null)); } else if (t.Data.Name == nameInformation.Name) { throw new Exception(); } else if (nameInformation.Name.CompareTo(t.Data.Name) < 0) { BinaryTreeNode <NameInformation> newLeft = Add(nameInformation, t.LeftChild); return(new BinaryTreeNode <NameInformation>(t.Data, newLeft, t.RightChild)); } else //if (nameInformation.Name.CompareTo(t.Data.Name) > 0) { BinaryTreeNode <NameInformation> newRight = Add(nameInformation, t.RightChild); return(new BinaryTreeNode <NameInformation>(t.Data, t.LeftChild, newRight)); } }
private LinkedListCell <NameInformation> parseFileToList(string filename) { LinkedListCell <NameInformation> front = null; using (StreamReader sr = new StreamReader(filename)) { while (!sr.EndOfStream) { LinkedListCell <NameInformation> temp = new LinkedListCell <NameInformation>(); string name = sr.ReadLine(); name = name.Trim(); float frequency = Convert.ToSingle(sr.ReadLine()); int rank = Convert.ToInt32(sr.ReadLine()); NameInformation nametemp = new NameInformation(name, frequency, rank); temp.Data = nametemp; temp.Next = front; front = temp; } } return(front); }
/// <summary> /// Removes the smallest element from the given binary search tree, which must not be null. /// </summary> /// <param name="t">The binary search tree from which to remove the minimum.</param> /// <param name="min">The data removed.</param> /// <returns>The resulting binary search tree.</returns> private BinaryTreeNode <NameInformation> RemoveMin(BinaryTreeNode <NameInformation> t, out NameInformation min) { if (t.LeftChild == null) { min = t.Data; return(t.RightChild); } else { return(BinaryTreeNode <NameInformation> .GetAvlTree(t.Data, RemoveMin(t.LeftChild, out min), t.RightChild)); } }
/// <summary> /// Method to handle remove buttons clicks /// </summary> /// <param name="t"></param> /// <param name="min"></param> /// <returns></returns> private BinaryTreeNode <NameInformation> RemoveMininumName(BinaryTreeNode <NameInformation> t, out NameInformation min) { //if the left child of t is nonexistent if (t.LeftChild == null) { min = t.Data; return(t.RightChild); } //Else, return element with minimum key in leftchild else { return(new BinaryTreeNode <NameInformation> (t.Data, RemoveMininumName(t.LeftChild, out min), t.RightChild)); } }