/// <summary> /// generic method reading an array representation of a binary tree and bui /// node = array[n]; node.left = array[2n]; node.right = array[2n+1] /// root of the input binary tree is array[0] /// </summary> /// <typeparam name="T">can be data type or reference type</typeparam> /// <param name="array">an array representation of a binary tree</param> /// <return name="Node"> return a reference of the root node</return> internal static BinaryTree <T> BuildBinaryTreeFromList <T>(List <object> list) { if (list == null) { return(null); } else { BinaryTree <T> bt = new BinaryTree <T>(); foreach (object i in list) { Node <T> temNode; if (i == null) { temNode = null; } else { temNode = new Node <T>(i); } bt.Insert(temNode); } return(bt); } /* * Parent(r) =⌊(r−1)/ 2⌋ if r≠0. * Left child(r) = 2r + 1 if 2r + 1 < n. * Right child(r) = 2r + 2 if 2r + 2 < n. * Left sibling(r) = r−1 if r is even and r≠0. * Right sibling(r) = r + 1 if r is odd and r + 1 < n. */ }
static void Main() { var Tree = new BinaryTree <int>(); foreach (int x in new List <int> { 100, 2, 123, 442, 15, 200, 117, 5000, 343 }) { Tree.Insert(x); } Console.WriteLine(Tree.OrderedOut()); }
private void Insert_btn_Click(object sender, RoutedEventArgs e) { var elem = this.insert_txt.Text; int res; var try_res = Int32.TryParse(elem, out res); if (elem != "" && try_res) { tree.Insert(res); this.output.Text = tree.ToString(); this.insert_txt.Text = ""; } }
// Действия с узлами private void Add_Btn_Click(object sender, EventArgs e) { int value; try { value = int.Parse(edit_add.Text); } catch (System.FormatException) { MessageBox.Show("Введите число."); return; } if (Tree.Insert(value) == true) { Log_TxBx.AppendText($"Значение {value} добавлено.\r\n"); } else { Log_TxBx.AppendText($"Значение {value} уже существует.\r\n"); } edit_add.Text = ""; }