private void Delete(Node node, Int32 value) { var nodeToDelete = Find(node, value); var leftMostChild = GetLeftMostChild(nodeToDelete.Right); nodeToDelete.Value = leftMostChild.Value; leftMostChild = null; }
public void Insert(Int32 value) { if (root == null) root = new Node(value); else Insert(root, value); }
private Boolean Exists(Node node, Int32 value) { if (node == null) return false; if (node.Value == value) return true; return Exists(node.Left, value) || Exists(node.Right, value); }
static void Main() { Console.WriteLine("Please paste the input:"); var N = int.Parse(Console.ReadLine()); var nodes = new Node<int>[N]; for (int i = 0; i < N; i++) { nodes[i] = new Node<int>(i); } for (int i = 1; i <= N - 1; i++) { string line = Console.ReadLine(); var lineParts = line.Split(' '); int parentValue = int.Parse(lineParts[0]); int childValue = int.Parse(lineParts[1]); nodes[parentValue].Children.Add(nodes[childValue]); nodes[childValue].HasParent = true; } // Root var root = FindRoot(nodes); Console.WriteLine("The rootis: {0}", root.Value); // Leafs var leafs = GetLeafs(nodes); Console.Write("Leafs: "); foreach (var leaf in leafs) { Console.Write("{0}, ", leaf.Value); } Console.WriteLine(); // Middle nodes var middleNodes = GetMiddleNodes(nodes); Console.Write("Middle nodes: "); foreach (var node in middleNodes) { Console.Write("{0}, ", node.Value); } Console.WriteLine(); // Longest path var longestPath = GetLongestPath(FindRoot(nodes)); Console.WriteLine("Number of levels: {0}", longestPath + 1); }
private static int GetLongestPath(Node<int> root) { if (root.Children.Count == 0) { return 0; } int maxPath = 0; foreach (var node in root.Children) { maxPath = Math.Max(maxPath, GetLongestPath(node)); } return maxPath + 1; }
private Node Find(Node node, Int32 value) { var currentNode = node; while (currentNode != null) { if (currentNode.Value == value) return currentNode; else if (currentNode.Value > value) return Find(node.Left, value); else return Find(node.Right, value); } return null; }
public void LevelOrder(Node start) { var queue = new Queue<Node>(); queue.Enqueue(start); while (queue.Count > 0) { var node = queue.Dequeue(); Console.Write(node.value + ", "); var child = node.firstChild; while (child != null) { queue.Enqueue(child); child = child.nextSibling; } } }
public void InOrder(Node start) { var child = start.firstChild; if (child != null) { while (child.nextSibling != null) { InOrder(child); child = child.nextSibling; } } Console.Write(start.value + ", "); if (child != null) { InOrder(child); } }
public Node InsertChild(Node parent, string value) { var newNode = new Node { value = value, parent = parent }; if (parent.firstChild == null) { parent.firstChild = newNode; } else { var child = parent.firstChild; while (child.nextSibling != null) { child = child.nextSibling; } child.nextSibling = newNode; } return newNode; }
static Node<int> FindRoot(Node<int>[] nodes) { var hasParent = new bool[nodes.Length]; foreach (var node in nodes) { foreach (var child in node.Children) { hasParent[child.Value] = true; } } for (int i = 0; i < hasParent.Length; i++) { if (!hasParent[i]) { return nodes[i]; } } throw new ArgumentException("No root in the tree!"); }
public int MinNode(Node node) { while (node.left != null) node = node.left; return node.data; }
public int MaxNode(Node node) { while (node.right != null) node = node.right; return node.data; }
public void levelorder(Node node) { Node cur = node; Queue<Node> queue = new Queue<Node>(); int nextLevel = 1; int curLevel; queue.Enqueue(cur); while (queue.Count > 0) { curLevel = nextLevel; nextLevel = 0; for (int i = 0; i < curLevel; i++) { cur = queue.Dequeue(); Console.Write(" {0} ", cur.data); if (cur.left != null) { queue.Enqueue(cur.left); nextLevel++; } if (cur.right != null) { queue.Enqueue(cur.right); nextLevel++; } } Console.WriteLine(); } }
public Node InPreToBinaryTree(int[] pre, int[] inorder, int instart, int inend, ref int preIndex) { if (instart > inend) return null; Node node = new Node(pre[preIndex++]); if (instart == inend) return node; int inIndex; for (inIndex = 0; inIndex < inorder.Length; inIndex++) { if (inorder[inIndex] == node.data) break; } node.left = InPreToBinaryTree(pre, inorder, instart, inIndex - 1, ref preIndex); node.right = InPreToBinaryTree(pre, inorder, inIndex + 1, inend, ref preIndex); return node; }
private void buildTree() { int[] vals = { 6, 2, 8, 1, 3, 7, 10, 5, 4, 9 }; foreach (int data in vals) { root = insert(root, data); } }
public int preorderSuccBST(Node node) { Node cur = root; if (node.left != null) return node.left.data; if (node.right != null) return node.right.data; Node succ = null; while (cur != null) { if (cur.data > node.data) { if (cur.right != null) succ = cur.right; cur = cur.left; } else if (cur.data < node.data) cur = cur.right; else break; } return (succ == null) ? -1 : succ.data; }
public int preorderPredBST(Node node) { Node cur = root; while (cur != null && cur != node) { if (cur.right == node && cur.left != null) return MaxNode(cur.left); if (cur.left == node || cur.right == node) return cur.data; if (cur.data > node.data) cur = cur.left; if (cur.data < node.data) cur = cur.right; } return -1; }
public void preorderIter(Node node) { Node cur = node; Stack<Node> stk = new Stack<Node>(); stk.Push(cur); Console.WriteLine("\nPreorder traversal iterative (using stack): "); while (stk.Count > 0) { cur = stk.Pop(); Console.Write(" {0} ", cur.data); if (cur.right != null) stk.Push(cur.right); if (cur.left != null) stk.Push(cur.left); } }
//using 2 stacks (this can be done using 1 stack also) public void postorderIter(Node node) { Node cur = node; Console.WriteLine("\n Postorder iterative using two stack: "); Stack<Node> stk = new Stack<Node>(); Stack<Node> rem = new Stack<Node>(); stk.Push(cur); while (stk.Count > 0) { cur = stk.Pop(); rem.Push(cur); if (cur.left != null) stk.Push(cur.left); if (cur.right != null) stk.Push(cur.right); } while (rem.Count > 0) { Console.Write(" {0} ", rem.Pop().data); } }
private static List<Node<int>> GetLeafs(Node<int>[] nodes) { List<Node<int>> leafs = new List<Node<int>>(); foreach (var node in nodes) { if (node.Children.Count == 0) { leafs.Add(node); } } return leafs; }
public void postorderIterOneStack(Node node) { Console.WriteLine("\nPostorder iterative using only one stack: "); Node cur = node; Stack<Node> stk = new Stack<Node>(); do { while (cur != null) { if (cur.right != null) stk.Push(cur.right); stk.Push(cur); cur = cur.left; } cur = stk.Pop(); if (cur.right != null && stk.Count > 0 && cur.right == stk.Peek()) { stk.Pop(); stk.Push(cur); cur = cur.right; } else { Console.Write(" {0} ", cur.data); cur = null; } } while (stk.Count > 0); }
//using stack, time: O(n) and space: O(1) (no extra space) public void inorderIter(Node node) { Stack<Node> stk = new Stack<Node>(); Node cur = node; bool done = false; Console.WriteLine("\nIn order traversal using stack (iterative): "); while (!done) { if (cur != null) { stk.Push(cur); cur = cur.left; } else { if (stk.Count > 0) { cur = stk.Pop(); Console.Write(" {0} ", cur.data); cur = cur.right; } else done = true; } } }
public void preorderMorris(Node node) { Node cur = node; Console.WriteLine("\nPreorder traversal using Morris traversal (threaded binary tree): "); while (cur != null) { if (cur.left == null) { Console.Write(" {0} ", cur.data); cur = cur.right; } else { Node succ = cur.left; while (succ.right != null && succ.right != cur) succ = succ.right; if (succ.right == null) { succ.right = cur; Console.Write(" {0} ", cur.data); cur = cur.left; } else if (succ.right == cur) { succ.right = null; cur = cur.right; } } } }
public void inorderMorris(Node node) { Node cur = node; Console.WriteLine("\nIn order traversal using Morris traversal (threaded binary tree): "); while (cur != null) { if (cur.left == null) { Console.Write(" {0} ", cur.data); cur = cur.right; } else { Node succ = cur.left; while (succ.right != null && succ.right != cur) succ = succ.right; if (succ.right == null) { succ.right = cur; //form a thread between leaf nodes right to its successor cur = cur.left; } else if (succ.right == cur) { succ.right = null; //remove thread formed previously Console.Write(" {0} ", cur.data); cur = cur.right; } } } }
public void preorderRecur(Node node) { if (node == null) return; Console.Write(" {0} ", node.data); preorderRecur(node.left); preorderRecur(node.right); }
public Node(int data, Node left, Node right) { this.data = data; this.left = left; this.right = right; }
public Node SortedArrToBinaryTree(int[] arr, int start, int end) { if (start > end) return null; int mid = (start + end) / 2; Node node = new Node(arr[mid]); node.left = SortedArrToBinaryTree(arr, start, mid - 1); node.right = SortedArrToBinaryTree(arr, mid + 1, end); return node; }
public int inorderPredBST(Node node) { Node cur = root; if (node.left != null) return MaxNode(node.left); Node pred = null; while (cur != null) { if (cur.data < node.data) { pred = cur; cur = cur.right; } else if (cur.data > node.data) cur = cur.left; else break; } return (pred == null) ? -1 : pred.data; }
private Node insert(Node node, int data) { if (node == null) return new Node(data); else { if (node.data >= data) node.left = insert(node.left, data); else node.right = insert(node.right, data); return node; } }
public int inorderSuccBST(Node node) { Node cur = root; if (node.right != null) return MinNode(node.right); Node succ = null; while (cur != null) { if (cur.data > node.data) { succ = cur; cur = cur.left; } else if (cur.data < node.data) { cur = cur.right; } else break; } return (succ == null) ? -1 : succ.data; }