public void InorderTraversal(BinaryTreeNode root) { if (root != null) { InorderTraversal(root.Left); Console.WriteLine(root.Key); InorderTraversal(root.Right); } }
internal bool Search(BinaryTreeNode root,int data) { if (root == null) return false; if (root.data == data) return true; else if (root.data < data) return Search(root.right, data); else return Search(root.left, data); }
internal BinaryTreeNode Insert(ref BinaryTreeNode root,BinaryTreeNode newNode) { if (root == null) { root = newNode; } else { if (root.data > newNode.data) Insert(ref root.left, newNode); else Insert(ref root.right, newNode); } return root; }
public void BinaryTreeInsert(int k) { BinaryTreeNode node = new BinaryTreeNode(k); if (Root == null) { Root = node; } else if (Root.Left == null) Root.Left = node; else if (Root.Right == null) Root.Right = node; else { Queue<BinaryTreeNode> q = new Queue<BinaryTreeNode>(); q.Enqueue(Root); while (q.Any()) { BinaryTreeNode temp = q.Dequeue(); if (temp.Left == null) { temp.Left = node; break; } else if (temp.Right == null) { temp.Right = node; break; } else { q.Enqueue(temp.Left); q.Enqueue(temp.Right); } } //Free Queue and delete } }
void preOrderValues(BinaryTreeNode current) { if (current != null) { preOrder.Add(current.data); preOrderValues(current.left); preOrderValues(current.right); } }
private BinaryTreeNode SearchIterative(BinaryTreeNode root, int key) { if (root == null) { return null; } else { BinaryTreeNode p = root; while (p != null) { if (p.Key == key) { return p; } else if (key < p.Key) { p = p.Left; } else { p = p.Right; } } return null; } }
private BinaryTreeNode SearchRecursiveBinaryTree(BinaryTreeNode root, int k) { if (root == null) return null; if(root.Key ==k) { return root; } BinaryTreeNode leftSearch = SearchRecursiveBinaryTree(root.Left, k); if(leftSearch!=null) { return leftSearch; } else { return SearchRecursiveBinaryTree(root.Right, k); } }
private bool isSymmetric(BinaryTreeNode leftRoot, BinaryTreeNode rightRoot) { if (leftRoot == null && rightRoot == null) { return true; } else if (leftRoot != null && rightRoot != null) { return (leftRoot.Key == rightRoot.Key && isSymmetric(leftRoot.Left, rightRoot.Right) && isSymmetric(leftRoot.Right, rightRoot.Left)); } else { return false; } }
/// <summary> /// Here it is assumed that nodes are present in the binary tree /// </summary> /// <param name="root"></param> /// <param name="p"></param> /// <param name="q"></param> /// <returns></returns> private BinaryTreeNode LCA_BinaryTree(BinaryTreeNode root, BinaryTreeNode p, BinaryTreeNode q) { if(root == null) { return null; } if(root==p|| root== q) { return root; } BinaryTreeNode leftLca = LCA_BinaryTree(root.Left, p, q); BinaryTreeNode rightLca = LCA_BinaryTree(root.Right, p, q); if(leftLca != null && rightLca != null) { //one on left and one on right scenario return root; } else { //Both left or both right scenario return leftLca != null ? leftLca : rightLca; } }
private void InsertIterative(int key) { BinaryTreeNode node = new BinaryTreeNode(key); if (Root == null) { Root = new BinaryTreeNode(key); } else { BinaryTreeNode p = Root; BinaryTreeNode prev = null; while (p != null) { prev = p; if (key <= p.Key) { p = p.Left; } else { p = p.Right; } } if (key <= prev.Key) { prev.Left = node; } else { prev.Right = node; } } }
private BinaryTreeNode InsertRecursive(BinaryTreeNode root, int key) { if (root == null) { root = new BinaryTreeNode(key); } else { if (key <= root.Key) { root.Left = InsertRecursive(root.Left, key); } else { root.Right = InsertRecursive(root.Right, key); } } return root; }
public BinaryTreeNode(int data, BinaryTreeNode left, BinaryTreeNode right) { this.data = data; this.left = left; this.right = right; }
private int HeightIterative(BinaryTreeNode root) { // Time Complexity : O(n) - have to visit all nodes // Space complexity : O(width of tree) if (root == null) return 0; int level = 0; Queue<BinaryTreeNode> q = new Queue<BinaryTreeNode>(); BinaryTreeNode marker = null; q.Enqueue(root); q.Enqueue(marker); while (q.Any()) { BinaryTreeNode temp = q.Dequeue(); if (temp == marker) { //End of level. Increment level++; if (q.Any()) { q.Enqueue(marker); } else { //Reached the end. break; } } else { if (temp.Left != null) { q.Enqueue(temp.Left); } if (temp.Right != null) { q.Enqueue(temp.Right); } } } return level; }
void PreOrderIterative(BinaryTreeNode current) { Stack<BinaryTreeNode> s = new Stack<BinaryTreeNode>(); s.Push(current); while(s.Count!=0) { BinaryTreeNode temp= s.Pop(); Console.WriteLine(temp); s.Push(temp.right); s.Push(temp.left); } }
void PreOrder(BinaryTreeNode current) { if (current == null) return; Console.WriteLine(current); PreOrder(current.left); PreOrder(current.right); }
public BinarySearchTree() { root = null; }
bool MatchTree(BinaryTreeNode n1, BinaryTreeNode n2) { if (n2 == null) return true; if (n1 == null) return false; if (n1.data == n2.data) return MatchTree(n1.left, n2.left) && MatchTree(n1.right, n2.right); else return false; }
void LeveLOrder(BinaryTreeNode current) { Queue<BinaryTreeNode> q = new Queue<BinaryTreeNode>(); q.Enqueue(current); while (q.Count != 0) { BinaryTreeNode temp = q.Dequeue(); Console.WriteLine(temp); if (temp.left != null) q.Enqueue(temp.left); if (temp.right != null) q.Enqueue(temp.right); } }
private BinaryTreeNode FindMinInBST(BinaryTreeNode root) { if (root == null) return null; BinaryTreeNode p = root; while(p.Left!=null) { p = p.Left; } return p; }
int TreeHeight(BinaryTreeNode root) { if (root == null) return 0; return 1 + Math.Max(TreeHeight(root.right), TreeHeight(root.left)); }
private int HeightRecursive(BinaryTreeNode root) { // Time Complexity : O(n) - have to visit all nodes // Space complexity : O(n) if (root == null) { return 0; } else { int lheight = HeightRecursive(root.Left); int rheight = HeightRecursive(root.Right); return 1 + Math.Max(lheight, rheight); } }
public BinaryTreeNode(int data) { this.data = data; this.left = null; this.right = null; }
public clsBinarySearchTree() { _root = null; }
private int IsHeightBalanced(BinaryTreeNode root) { if (root == null) { return 0; } int lh = IsHeightBalanced(root.Left); if (lh == -2) return -2; int rh = IsHeightBalanced(root.Right); if (rh == -2) return -2; if (Math.Abs(lh - rh) > 1) return -2; return (1 + Math.Max(lh, rh)); }
public void Clear() { _root = null; }
private void KthNodeInorder(BinaryTreeNode root, ref BinaryTreeNode KthNode, ref int count, int k) { if (root == null) { return; } KthNodeInorder(root.Left, ref KthNode, ref count, k); count++; if (count == k) { KthNode = root; return; } KthNodeInorder(root.Right,ref KthNode ,ref count, k); }
public BinaryTreeNode NextRight; //Used in some of the problems public BinaryTreeNode(int val, BinaryTreeNode left = null, BinaryTreeNode right = null) { Value = val; Left = left; Right = right; }
private BinaryTreeNode LCA_BST(BinaryTreeNode root, int p , int q) { if (root == null) return null; BinaryTreeNode temp = root; while(temp!=null) { if(p< temp.Key && q < temp.Key) { //Go left temp = temp.Left; } else if(p> temp.Key && q> temp.Key) { //Go right temp = temp.Right; } else { //found lca return temp; } } return null; }
public void KthNodeInorder(ref BinaryTreeNode KthNode, int k) { int count = 0; KthNodeInorder(Root, ref KthNode, ref count, k); }
private BinaryTreeNode SearchRecursive(BinaryTreeNode root, int key) { if (root == null) return null; if (root.Key == key) return root; if (key < root.Key) { return SearchRecursive(root.Left, key); } else { return SearchRecursive(root.Right, key); } }
public void PrintDoublyLinkedList(BinaryTreeNode root) { //Print the doubly linked list if (Root == null) return; if(Root.Left==null && Root.Right == null) { Console.WriteLine(Root.Key); return; } BinaryTreeNode p = Root; while (p.Left != null) p = p.Left; while(p!=null) { Console.WriteLine(p.Key); p = p.Right; } }
public void BinaryTreeToDoublyLinkedList(ref BinaryTreeNode visited) { BinaryTreeToDoublyLinkedList(Root, ref visited); }
private void BinaryTreeToDoublyLinkedList(BinaryTreeNode root, ref BinaryTreeNode visited) { if (root == null) return; BinaryTreeToDoublyLinkedList(root.Left, ref visited); if(visited!=null) { visited.Right = root; root.Left = visited; } visited = root; //traverse right BinaryTreeToDoublyLinkedList(root.Right, ref visited); }
public void Insert(int key, bool isIterative = false) { if (isIterative) { InsertIterative(key); } else { BinaryTreeNode temp = InsertRecursive(Root, key); if (Root == null) { Root = temp; } } }