public void AddNode_BinaryTree(int data, NodePractice rootNew) { NodePractice node = GetNewNode(data); NodePractice temp = rootNew; if (temp == null) { root = node; } else { if (countOfNodes % 2 != 0) { while (temp.left != null) { temp = temp.left; } temp.left = node; } else { while (temp.right != null) { temp = temp.right; } temp.right = node; } } countOfNodes++; }
public NodePractice DeleteNode(int data, NodePractice node) { if (data > node.data) { node.right = DeleteNode(data, node.right); } else if (data < node.data) { node.left = DeleteNode(data, node.left); } else { if (node.left == null && node.right == null) { NodePractice temp = node; node = null; } else if (node.left == null) { return(node.right); } else if (node.right == null) { return(node.left); } else { NodePractice temp = FindInOrderSuccessor(node.right); node.data = temp.data; node.right = DeleteNode(temp.data, node.right); } } return(node); }
public NodePractice FindInOrderPredecessor(NodePractice node) { while (node.right != null) { node = node.right; } return(node); }
public NodePractice FindInOrderSuccessor(NodePractice node) { while (node.left != null) { node = node.left; } return(node); }
public int FindMin(NodePractice node) { while (node.left != null) { node = node.left; } return(node.data); }
public int FindMax(NodePractice node) { while (node.right != null) { node = node.right; } return(node.data); }
private NodePractice GetNewNode(int data) { NodePractice node = new NodePractice(); node.data = data; node.left = null; node.right = null; return(node); }
public void PostOrder(NodePractice root) { if (root != null) { PostOrder(root.left); PostOrder(root.right); Console.WriteLine(root.data); } }
public int CountNumberOfNodes(NodePractice temp) { if (temp != null) { CountNumberOfNodes(temp.left); countNumberOfNodes++; CountNumberOfNodes(temp.right); } return(countNumberOfNodes); }
public int CountNumberOfNodes_Meth2(NodePractice temp) { if (temp != null) { return(1 + CountNumberOfNodes_Meth2(temp.left) + CountNumberOfNodes_Meth2(temp.right)); } else { return(0); } }
public bool isCompleteBinaryTree(NodePractice node) { if (node.left == null && node.right == null) { return(true); } else if () { } else { } }
public string CountNumberOfLeaves(NodePractice temp) { if (temp != null) { if (temp.left == null && temp.right == null) { countNumberOfLeaves++; } else { CountNumberOfLeaves(temp.left); countNumberOfNonLeaves++; CountNumberOfLeaves(temp.right); } } return("No of Leaves" + countNumberOfLeaves + "Non Leaves" + countNumberOfNonLeaves); }
public int CountNumberofLeaves_Meth2(NodePractice temp) { if (temp != null) { if (temp.left == null && temp.right == null) { return(1); } else { return(CountNumberofLeaves_Meth2(temp.left) + CountNumberofLeaves_Meth2(temp.right)); } } else { return(0); } }
public void AddNode(int data, NodePractice node) { NodePractice newNode = GetNewNode(data); if (root == null) { root = newNode; } else { NodePractice temp = node; if (temp != null) { if (newNode.data >= temp.data) { if (temp.right != null) { AddNode(newNode.data, temp.right); } else { temp.right = newNode; } } else { if (temp.left != null) { AddNode(newNode.data, temp.left); } else { temp.left = newNode; } } } } }
static void Main(string[] args) { /********************************************************** IMPLEMENT QUEUE WITH TWO STACKS ***********************************************************************/ ImplementQueueWithTwoStacks iqTS = new ImplementQueueWithTwoStacks(); iqTS.Insert(10); iqTS.Insert(20); iqTS.Insert(30); iqTS.Insert(40); Console.WriteLine(iqTS.Delete()); // 10 Console.WriteLine(iqTS.Delete()); // 20 Console.WriteLine(iqTS.Delete()); // 30 /********************************************************** IMPLEMENT QUEUE WITH TWO STACKS ***********************************************************************/ // Console.WriteLine(new InfixToPostFixConversion(7).ConvertToPostfix("a+b*(c-d)")); Console.WriteLine(new InfixToPostFixConversion("a+b*(c^d-e)^(f+g*h)-i".Length).ConvertToPostfix("a+b*(c^d-e)^(f+g*h)-i")); // abcd^e-fgh*+^*+i- /**************************************************************** Trying to implement complete binary tree *****************************************************************/ TreePractice tr = new TreePractice(); tr.AddNode_BinaryTree(10, tr.root); tr.AddNode_BinaryTree(20, tr.root); tr.AddNode_BinaryTree(30, tr.root); tr.AddNode_BinaryTree(40, tr.root); tr.AddNode_BinaryTree(50, tr.root); tr.AddNode_BinaryTree(60, tr.root); /**************************************************************** Trying to implement complete binary tree *****************************************************************/ TreeTraversals trTraversal = new TreeTraversals(); trTraversal.Inorder(tr.root); Console.WriteLine(new TreePractice().CountNumberOfNodes(tr.root)); Console.WriteLine(new TreePractice().CountNumberOfNodes_Meth2(tr.root)); Console.WriteLine(new TreePractice().CountNumberOfLeaves(tr.root)); Console.WriteLine("No of leaves:" + new TreePractice().CountNumberofLeaves_Meth2(tr.root)); Console.WriteLine("No of non-leaves:" + new TreePractice().CountNumberofNonLeaves_Meth2(tr.root)); /************************************************* Binary Search Tree ************************************/ BinarySearchTree bTree = new BinarySearchTree(); /* Add elements in Binary Search Tree */ bTree.AddNode(10, bTree.root); bTree.AddNode(20, bTree.root); bTree.AddNode(30, bTree.root); bTree.AddNode(40, bTree.root); bTree.AddNode(25, bTree.root); bTree.AddNode(15, bTree.root); bTree.AddNode(5, bTree.root); bTree.AddNode(8, bTree.root); bTree.AddNode(9, bTree.root); Console.WriteLine("Maximum Element: " + bTree.FindMax(bTree.root)); Console.WriteLine("Minimum Element: " + bTree.FindMin(bTree.root)); bTree.DeleteNode(25, bTree.root); NodePractice afterDelete = bTree.DeleteNode(40, bTree.root); // bTree.DeleteNode(10, bTree.root); Console.WriteLine(afterDelete.data); trTraversal.Inorder(bTree.root); // should print all elements in a sorted order Console.ReadLine(); }