public BST(BSTItem rootItem) { Item = rootItem; }
public static BST FromTable(List <Word> keys, Dictionary <int, List <Word> > keyDummies, int[][] table) { var stack = new Queue <Help>(); var rootHelp = new Help(0, table.Length - 1); stack.Enqueue(rootHelp); var rootIndex = table[rootHelp.Row][rootHelp.Column]; var rootItem = new BSTItem(keys[rootIndex]); var bst = new BST(rootItem); keys[rootIndex].Added = true; while (stack.Count != 0) { var current = stack.Dequeue(); if (current.Row > current.Column) { if (keyDummies[current.Row].Count > 0 && !keyDummies[current.Row][0].Added) { bst.Add(new BSTItem(keyDummies[current.Row])); keyDummies[current.Row][0].Added = true; } //if (current.Row < keyDummies.Count - 1) //{ // if (keyDummies[current.Row + 1].Count > 0 && !keyDummies[current.Row + 1][0].Added) // { // bst.Add(new BSTItem(keyDummies[current.Row + 1])); // keyDummies[current.Row + 1][0].Added = true; // } //} if (keyDummies[current.Column + 1].Count > 0 && !keyDummies[current.Column + 1][0].Added) { bst.Add(new BSTItem(keyDummies[current.Column + 1])); keyDummies[current.Column + 1][0].Added = true; } } else { var currentItemIndex = table[current.Row][current.Column]; stack.Enqueue(new Help(current.Row, currentItemIndex - 1)); stack.Enqueue(new Help(currentItemIndex + 1, current.Column)); if (keys[currentItemIndex].Added) { continue; } //var subBst = new BST(new BSTItem(keys[currentItemIndex])); bst.Add(new BSTItem(keys[currentItemIndex])); keys[currentItemIndex].Added = true; //if (current.Row == current.Column) //{ // subBst.Add(new BSTItem(keyDummies[currentItemIndex])); // subBst.Add(new BSTItem(keyDummies[currentItemIndex + 1])); //} } } return(bst); }
//public void Add(BSTItem item) //{ // if (item.Value == Item.Value) // throw new Exception("Value already in tree"); // if (item.Value < Item.Value) // { // AddLeft(item); // } // else if (item.Value > Item.Value) // { // AddRight(item); // } //} public void Add(BSTItem item) { var root = this; while (true) { if (item.Word != null && item.Word == root.Item.Word) { throw new Exception($"Value {item.Word} already in tree"); } if (item.Word == null) { if (item.Dummies[0].CompareTo(root.Item.Word) < 0) { if (root.Left != null) { root = root.Left; continue; } else { root.Left = new BST(item); return; } } else if (item.Dummies[0].CompareTo(root.Item.Word) > 0) { if (root.Right != null) { root = root.Right; continue; } else { root.Right = new BST(item); return; } } return; } else { if (item.Word.CompareTo(root.Item.Word) < 0) { if (root.Left != null) { root = root.Left; continue; } else { root.Left = new BST(item); return; } } else if (item.Word.CompareTo(root.Item.Word) > 0) { if (root.Right != null) { root = root.Right; continue; } else { root.Right = new BST(item); return; } } } } }