private void Insert(int data, MyTreeNode <int> node) { if (data < node.Data) { if (node.LeftChild == null) { node.LeftChild = new MyTreeNode <int>(data); } else { Insert(data, node.LeftChild); } } else { if (node.RightChild == null) { node.RightChild = new MyTreeNode <int>(data); } else { Insert(data, node.RightChild); } } }
private void InsertNodeValInMatrix(List <List <char> > matrix, MyTreeNode <T> node, int matrixLvl, int treeLvl, int pos) { if (node == null) { return; } int maxPositionsForLvl = (int)Math.Pow(2, treeLvl); if (matrixLvl < (matrix.Count - 2)) { maxPositionsForLvl++; //need for spacing } int spacingPerPosition = matrix[treeLvl].Capacity / maxPositionsForLvl; int placeIdx = (pos * spacingPerPosition) - 1; matrix[matrixLvl][placeIdx] = node.Data.ToString()[0]; if (node.LeftChild != null) { matrix[matrixLvl + 1][placeIdx - 1] = '/'; InsertNodeValInMatrix(matrix, node.LeftChild, matrixLvl + 2, treeLvl + 1, (pos * 2) - 1); } if (node.RightChild != null) { matrix[matrixLvl + 1][placeIdx + 1] = '\\'; InsertNodeValInMatrix(matrix, node.RightChild, matrixLvl + 2, treeLvl + 1, pos * 2); } //if(pos > 8) //{ // Console.WriteLine($"Pos {pos} (treeLvl {treeLvl}) use spacing of {spacingPerPosition} per pos. Curr plxIdx is {placeIdx}. Total cap is {matrix[treeLvl].Capacity}"); // Console.WriteLine($"maxPositionsForLvl {maxPositionsForLvl}"); //} }
public MyBst(IList <int> unsortedArr) { Root = new MyTreeNode <int>(unsortedArr[0]); for (int i = 1; i < unsortedArr.Count(); i++) { Insert(unsortedArr[i]); } }
public static int GetTreeHeight(MyTreeNode <T> node) { if (node == null) { return(0); } return(1 + Math.Max(GetTreeHeight(node.LeftChild), GetTreeHeight(node.RightChild))); }
public static void PostOrderTraversalAction(MyTreeNode <T> treeNode, Action <T> action) { if (treeNode == null) { return; } PostOrderTraversalAction(treeNode.LeftChild, action); PostOrderTraversalAction(treeNode.RightChild, action); action(treeNode.Data); }
public MyTreeNode(T value) { Data = value; LeftChild = null; RightChild = null; }