private static void VisitPreorder(DsalBinaryTreeNode nodeToVisit, List<int> traversedList) { if (nodeToVisit == null) return; traversedList.Add(nodeToVisit.Data); VisitPreorder(nodeToVisit.Left, traversedList); VisitPreorder(nodeToVisit.Right, traversedList); }
public static DsalBinarySearchTree AddItem(this DsalBinarySearchTree bst, int itemToAdd) { DsalBinaryTreeNode nodeToAdd = new DsalBinaryTreeNode() { Data = itemToAdd }; if (bst.Root == null) { bst.Root = nodeToAdd; } else { AddNode(nodeToAdd, bst.Root); } return bst; }
private static BinaryTreeNodeWithParent Find(int value, DsalBinaryTreeNode searchingNode, DsalBinaryTreeNode searchingNodeParent) { if (searchingNode == null) return new BinaryTreeNodeWithParent(); if (searchingNode.Data == value) { var searchResultNode = new BinaryTreeNodeWithParent(); searchResultNode.Node = searchingNode; searchResultNode.Parent = searchingNodeParent; return searchResultNode; } else if (value < searchingNode.Data) { return Find(value, searchingNode.Left, searchingNode); } else { return Find(value, searchingNode.Right, searchingNode); } }
private static void AddNode(DsalBinaryTreeNode nodeToAdd, DsalBinaryTreeNode addToNode) { if (nodeToAdd.Data < addToNode.Data) { if (addToNode.Left == null) { addToNode.Left = nodeToAdd; } else AddNode(nodeToAdd, addToNode.Left); } else { if (addToNode.Right == null) { addToNode.Right = nodeToAdd; } else { AddNode(nodeToAdd, addToNode.Right); } } }