private DoublyListNode FindInorderSuccessor(DoublyListNode root, int num) { DoublyListNode current = FindValueInTree(root, num); if (current == null) { return(null); } if (current.right != null) { return(Findmin(current.right)); } else { DoublyListNode ancestor = root; DoublyListNode successor = null; while (current != ancestor) { if (current.data < ancestor.data) { successor = ancestor; ancestor = ancestor.left; } else { ancestor = ancestor.right; } } return(successor); } }
internal void insertAtPosotion(int num, int position) { DoublyListNode newNode = new DoublyListNode(); newNode.read(num); DoublyListNode q = null; if (position == 1) { insertAtStart(num); return; } DoublyListNode temp = Head; int currentPositionValue = 1; while (currentPositionValue < position - 1) { temp = temp.right; currentPositionValue++; } if (temp.right == null) // last node { insertAtEnd(num); return; } q = temp.right; newNode.left = temp; temp.right = newNode; newNode.right = q; }
internal void breadthfirstsearchLevelOrderTraversal() { if (root == null) { return; } Queue <DoublyListNode> queue = new Queue <DoublyListNode>(); queue.Enqueue(root); while (queue.Count != 0) { DoublyListNode current = queue.Peek(); Console.WriteLine(current.data); if (current.left != null) { queue.Enqueue(current.left); } if (current.right != null) { queue.Enqueue(current.right); } queue.Dequeue(); } }
private DoublyListNode Findmin(DoublyListNode root) { while (root.left != null) { root = root.left; } return(root); }
internal void DeleteNodeFromBST(int num) { if (root == null) { Console.WriteLine("No node exist to delete"); } root = DeleteNodeFromBST(root, num); }
private int FindBSTHeight(DoublyListNode root) { if (root == null) { return(-1); } return(FindMax(FindBSTHeight(root.left), FindBSTHeight(root.right)) + 1); }
internal void traverseDoublyLinkedListFromStart() { DoublyListNode temp = Head; while (temp != null) { temp.print(); temp = temp.right; } }
internal void GetInorderSuccessor(int num) { if (root == null) { Console.WriteLine("Empty tree"); } DoublyListNode successor = FindInorderSuccessor(root, num); Console.WriteLine("InorderSuccessor of 8 is :" + successor != null ? successor.data : 0); }
private void dfsPreorder(DoublyListNode root) { if (root == null) { return; } Console.WriteLine(root.data); dfsPreorder(root.left); dfsPreorder(root.right); }
private bool IsBinarySearchTree(DoublyListNode root) { if (root == null) { return(true); } if (IsLeftSubtreeisLesser(root.left, root.data) && IsRightSubtreeisGreater(root.right, root.data) && IsBinarySearchTree(root.left) && IsBinarySearchTree(root.right)) { return(true); } else { return(false); } }
private bool IsLeftSubtreeisLesser(DoublyListNode left, int data) { if (left == null) { return(true); } if (left.data <= data && IsLeftSubtreeisLesser(left.left, left.data) && IsRightSubtreeisGreater(left.right, left.data)) { return(true); } else { return(false); } }
public void createBST(int[] arr) { int arrLength = arr.Length; if (arrLength < 1) { root = null; return; } for (int k = 0; k < arrLength; k++) { root = Insert(root, arr[k]); } }
private bool IsBinarySearchTree(DoublyListNode root, int minValue, int maxValue) { if (root == null) { return(true); } if (root.data > minValue && root.data < maxValue && IsBinarySearchTree(root.left, minValue, root.data) && IsBinarySearchTree(root.right, root.data, maxValue)) { return(true); } else { return(false); } }
private DoublyListNode FindValueInTree(DoublyListNode root, int num) { if (root.data == num) { return(root); } if (num < root.data) { root = FindValueInTree(root.left, num); } else { root = FindValueInTree(root.right, num); } return(root); }
internal void reverseList() { DoublyListNode temp = Head; DoublyListNode temp1 = null; while (temp != null) { temp1 = temp.right; temp.right = temp.left; temp.left = temp1; if (temp.left == null) { Head = temp; } temp = temp.left; } }
private DoublyListNode Insert(DoublyListNode root, int num) { if (root == null) { root = new DoublyListNode(); root.read(num); } else if (num <= root.data) { root.left = Insert(root.left, num); } else { root.right = Insert(root.right, num); } return(root); }
internal void insertAtStart(int num) { DoublyListNode newNode = new DoublyListNode(); newNode.read(num); if (Head == null) { Head = newNode; return; } DoublyListNode temp = Head; temp.left = newNode; newNode.right = temp; Head = newNode; }
internal void SpiralTraversal() { if (root == null) { return; } Stack <DoublyListNode> S1 = new Stack <DoublyListNode>(); Stack <DoublyListNode> S2 = new Stack <DoublyListNode>(); DoublyListNode P = null; S1.Push(root); while (S1.Count != 0 || S2.Count != 0) { while (S1.Count != 0) { P = S1.Pop(); Console.WriteLine(P.data); if (P.left != null) { S2.Push(P.left); } if (P.right != null) { S2.Push(P.right); } } while (S2.Count != 0) { P = S2.Pop(); Console.WriteLine(P.data); if (P.right != null) { S1.Push(P.right); } if (P.left != null) { S1.Push(P.left); } } } }
internal void deleteAtStart() { if (Head == null) { Console.WriteLine("Nothing exists in this list to delete"); return; } if (Head.right == null) { Head = null; return; } DoublyListNode temp = Head; temp = temp.right; temp.left = null; Head = temp; }
internal void insertAtEnd(int num) { DoublyListNode newNode = new DoublyListNode(); newNode.read(num); if (Head == null) { Head = newNode; return; } DoublyListNode temp = Head; while (temp.right != null) { temp = temp.right; } temp.right = newNode; newNode.left = temp; }
internal void deleteAtMiddle() { if (Head == null) { Console.WriteLine("Nothing exists in this list to delete"); return; } if (Head.right == null) { Head = null; return; } DoublyListNode p = Head; DoublyListNode q = Head; DoublyListNode x = null; DoublyListNode temp = null; while (q.right != null && q.right.right != null) { x = p; p = p.right; q = q.right.right; } if (q.right != null) { x = p.right; temp = x.right; p.right = temp; temp.left = p; } else { temp = p.right; x.right = temp; temp.left = x; } }
private DoublyListNode DeleteNodeFromBST(DoublyListNode root, int num) { if (root == null) { return(root); } if (root.data > num) { root.left = DeleteNodeFromBST(root.left, num); } else if (root.data < num) { root.right = DeleteNodeFromBST(root.right, num); } else { if (root.left == null && root.right == null) { root = null; } else if (root.left == null) { root = root.right; } else if (root.right == null) { root = root.left; } else { DoublyListNode temp = Findmin(root.right); root.data = temp.data; root.right = DeleteNodeFromBST(root.right, temp.data); } } return(root); }
private int countLeaves(DoublyListNode root, int num) { if (root == null) { return(0); } if (root.left == null && root.right == null) { return(1); } int lefttreeLeafCount = countLeaves(root.left, num); int righttreeleafCount = countLeaves(root.right, num); int totalLeafCount = lefttreeLeafCount + righttreeleafCount; if (totalLeafCount == num) { Console.WriteLine(root.data); } return(totalLeafCount); }
internal void deleteAtEnd() { if (Head == null) { Console.WriteLine("Nothing exists in this list to delete"); return; } if (Head.right == null) { Head = null; return; } DoublyListNode temp = Head; DoublyListNode temp1 = null;; while (temp.right != null) { temp1 = temp; temp = temp.right; } temp1.right = null; }
private DoublyListNode LCA(DoublyListNode root, int p, int q) { if (root == null) { return(null); } if (root.data == p || root.data == q) { return(root); } DoublyListNode left = LCA(root.left, p, q); DoublyListNode right = LCA(root.right, p, q); if (left != null && right != null) { return(root); } else { return(left != null ? left : right); } }
internal void deleteAtPosition(int position) { if (Head == null) { Console.WriteLine("Nothing exists in this list to delete"); return; } if (Head.right == null && position == 1) { Head = null; return; } if (position == 1) { deleteAtStart(); return; } DoublyListNode p = Head; DoublyListNode q = null; DoublyListNode x = null; int currentPosition = 1; while (currentPosition < position) { q = p; p = p.right; currentPosition++; } x = p.right; q.right = x; x.left = q; }
public void create(int[] arr) { int arrLength = arr.Length; if (arrLength < 1) { Head = null; return; } int k = 0; Head = new DoublyListNode(); Head.read(arr[k]); DoublyListNode temp = Head; for (k = 1; k < arrLength; k++) { temp.right = new DoublyListNode(); temp.right.left = temp; temp.right.read(arr[k]); temp = temp.right; } }
internal void insertAtmiddle(int num) { DoublyListNode newNode = new DoublyListNode(); newNode.read(num); DoublyListNode p = Head; DoublyListNode q = Head; DoublyListNode ptr = null; if (Head == null) { Head = newNode; } while (q.right != null && q.right.right != null) { ptr = p; p = p.right; q = q.right.right; } if (q.right != null) { ptr = p.right; p.right = newNode; newNode.left = p; newNode.right = ptr; } else { ptr.right = newNode; newNode.left = ptr; newNode.right = p; } }
internal void LCA() { DoublyListNode LCANode = LCA(root, 6, 10); Console.WriteLine(LCANode.data); }
public DoublyLinkedList() { Head = null; }