From 15f90c575054693fa1a7bc6e6ad2ba9b97abf83a Mon Sep 17 00:00:00 2001 From: paramag Date: Thu, 6 Apr 2017 12:24:06 -0700 Subject: [PATCH] tree problems. --- Algorithm/.vs/Algorithms/v14/.suo | Bin 83456 -> 98816 bytes .../Algorithms.Problem.BinaryTree.csproj | 2 + Algorithm/BinaryTree/ConnectNextNode.cs | 106 ++++++++++++++++++ Algorithm/BinaryTree/LowestCommonAncestor.cs | 6 +- Algorithm/BinaryTree/PathRootToLeaf.cs | 66 +++++++++++ DataStructures/Trees/BinaryTreeNode.cs | 2 + 6 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 Algorithm/BinaryTree/ConnectNextNode.cs create mode 100644 Algorithm/BinaryTree/PathRootToLeaf.cs diff --git a/Algorithm/.vs/Algorithms/v14/.suo b/Algorithm/.vs/Algorithms/v14/.suo index 62f16557c227d28f0154bd2743ff62794b2a54c7..1666170e12054c61c2f7eebca8f7ccf3ab63a9f0 100644 GIT binary patch delta 6486 zcmc&23sjV4_P_ITfB^;=5CnA?5l28AbU^Zv1B{4>FequYZpk8*W&`1fkK7y)v7@-j zTBL7nyVP-HZ=wmnU&we99vEzRR;&+e(t>8%|%p|sA%-uur#2)@#@wtaK%+{gF$ zzwh4r-TQr?w^i$H)@_>_UnWVCn&8L2zCJ<{5V-bLCaa=4Rhy27?R^g_NfMZeYgFZ$ zaYUlA*Q)wV!f$ou`b^@DicI?u5z)9>{Xpc)M9tUJAC(jYO&p%l7!rpQM<3}gTH@0P z(5NwCXSi`%5;5rnJYuMondnG`=Mhs05(#E1vQhT6mJ)R@!7_sT2;2nq1l;<5!VLuYw$`p$PlQjkEXuXAaz86(4DqZO-5Q9) zUR{NrS2IW*Gguw#J|}TV9Xu$PFp}|9Lem^Vd^iEujUb#%5U21gdxhv&0&cG%JW8RX z2--LZBxIBM2vmATSW{T%!m_D|8IuSbQ>kWc)Cqk`?K2!Xud8 zSN$}!)u+T?(uFJAs*|tOYZG)$aDtw-GJ$~o6F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N%!?fnDU(NXPvYzR=G% zucnOFijB7V$+481gITHkRvFDqLLz#52C>0bH>q9ehV3^BpKTg)uHo=PKiBM3{Mao+Sh zH^$3i^4Ta|nys{3EL&B`49petS53d0fWjFn5d%SkbhOa%?CYVMr(Hu=vR^J#PX01; z>>`PBO_4lpA-jfX=$|cg^D8h?{>?!v<*Bw93-3$0lNPs$_XFJm_HaAC6I;NBHWyF-Zz5>+~!awz1`fmF9gxZagkuvg%Yz z%a68@!*DH;fZ1(LZ5Ef)Zg#qc9P24e@@zLbh8+8l6H`d;I6z(UolTU7w`PBhcFJ8o zGIHvWb$6N}Pi>*qvUVLARSN!FD=Ua`peOL4zc17k*tRXu>+3}Z7Yh8hz9}ZKn|QCz zC=Z6HCQiQ&b;T}q#m?A@gYrO#rs-7riN?|;@{W$z$Dg;{Sh( zr|-=Eb92U@BsNqcIu*(MUMkACBjTxHE7o!QLr=aIq6OKN35%lUym6k$uKRxe1P4i6 zt(z|Nr7n?F`VBe!5Dkmo{ diff --git a/Algorithm/BinaryTree/Algorithms.Problem.BinaryTree.csproj b/Algorithm/BinaryTree/Algorithms.Problem.BinaryTree.csproj index 6a479af..dc5906d 100644 --- a/Algorithm/BinaryTree/Algorithms.Problem.BinaryTree.csproj +++ b/Algorithm/BinaryTree/Algorithms.Problem.BinaryTree.csproj @@ -44,8 +44,10 @@ + + diff --git a/Algorithm/BinaryTree/ConnectNextNode.cs b/Algorithm/BinaryTree/ConnectNextNode.cs new file mode 100644 index 0000000..1a843f5 --- /dev/null +++ b/Algorithm/BinaryTree/ConnectNextNode.cs @@ -0,0 +1,106 @@ +using DataStructures.Libraries.Trees; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Algorithms.Problem.BinaryTree +{ + public class ConnectNextNode + { + BinaryTreeNode Root { get; } + + public ConnectNextNode(BinaryTreeNode root) + { + this.Root = root; + } + + public void ConnectNode() + { + BinaryTreeNode node = this.Root; + + // 1. Start loop 1 to traverse depth wise. + // 2. Start loop 2 to traverse breadth wise. + // 3. Connect the next node while traversing depth and breadth wise. + // Somtimes the next node will not be present in immediate next, + // in such cases keep going next until we find the next node. + // For eg1: consider the following tree: + /// 20 + /// / \ + /// 10 30 + /// / / \ + /// 5 25 35 + /// + // For eg2: consider the following tree: + /// 20 + /// / \ + /// 10 30 + /// / \ \ + /// 5 12 35 + + // depth wise traversal + while (node != null) + { + BinaryTreeNode node2 = node; + + // breadth wise traversal + while (node2 != null) + { + if (node2.Left != null) + { + if(node2.Right != null) + { + node2.Left.Next = node2.Right; + } + else + { + // Traverse breadth until we find the next node. + node.Left.Next = this.SearchForNextNode(node2.Next); + } + } + + if (node2.Right != null && node2.Next != null) + { + if(node2.Next.Left != null) + { + node2.Right.Next = node2.Next.Left; + } + else + { + // Traverse breadth until we find the next node. + node2.Right.Next = this.SearchForNextNode(node2.Next); + } + } + + node2 = node2.Next; + } + + node = node.Left; + } + } + + private BinaryTreeNode SearchForNextNode(BinaryTreeNode node2) + { + BinaryTreeNode node = node2; + + while(node != null) + { + if(node.Left != null) + { + return node.Left; + } + + else if(node.Right != null) + { + return node.Right; + } + + node = node.Next; + } + + // Return null if nothing is found. + return null; + } + } +} diff --git a/Algorithm/BinaryTree/LowestCommonAncestor.cs b/Algorithm/BinaryTree/LowestCommonAncestor.cs index 5d07a43..4e80d5f 100644 --- a/Algorithm/BinaryTree/LowestCommonAncestor.cs +++ b/Algorithm/BinaryTree/LowestCommonAncestor.cs @@ -37,14 +37,16 @@ public BinaryTreeNode FindLowestCommonAncestor(BinaryTreeNode root, BinaryTreeNo return null; } + // Return the root if there is one match. if (root.Value == node1.Value || root.Value == node2.Value) { return root; } - BinaryTreeNode leftNode = FindLowestCommonAncestor(root.Left, node1, node2); - BinaryTreeNode rightNode = FindLowestCommonAncestor(root.Right, node1, node2); + BinaryTreeNode leftNode = this.FindLowestCommonAncestor(root.Left, node1, node2); + BinaryTreeNode rightNode = this.FindLowestCommonAncestor(root.Right, node1, node2); + // LCA found...if both nodes left and right sends not null. if (leftNode != null && rightNode != null) { return root; diff --git a/Algorithm/BinaryTree/PathRootToLeaf.cs b/Algorithm/BinaryTree/PathRootToLeaf.cs new file mode 100644 index 0000000..f64c1c3 --- /dev/null +++ b/Algorithm/BinaryTree/PathRootToLeaf.cs @@ -0,0 +1,66 @@ +using DataStructures.Libraries.Trees; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Algorithms.Problem.BinaryTree +{ + [TestClass] + public class PathRootToLeaf + { + BinaryTreeNode Root { get; } + + public PathRootToLeaf() + { + } + + public PathRootToLeaf(BinaryTreeNode root) + { + this.Root = root; + } + + public void PathRootToLeafImpl() + { + int[] path = new int[100]; + + this.PathRootToLeafImpl(this.Root, 0, path); + } + + public void PathRootToLeafImpl(BinaryTreeNode root, int counter, int[] path) + { + if (root == null) return; + + path[counter++] = root.Value; + + // Leaf node + if (root.Left == null && root.Right == null) + { + for (int i = 0; i < counter; i++) + { + Console.Write(path[i] + " "); + } + + Console.WriteLine(); + } + else + { + PathRootToLeafImpl(root.Left, counter, path); + PathRootToLeafImpl(root.Right, counter, path); + } + } + + [TestMethod] + public void ValidateTreePathToLeaf() + { + var binarySearchTree = new BinarySearchTree(); + binarySearchTree.PopulateDefaultBalanceTree(); + + var pathRootToLeaf = new PathRootToLeaf(binarySearchTree.Root); + + pathRootToLeaf.PathRootToLeafImpl(); + } + } +} diff --git a/DataStructures/Trees/BinaryTreeNode.cs b/DataStructures/Trees/BinaryTreeNode.cs index 1c579e0..55de4f0 100644 --- a/DataStructures/Trees/BinaryTreeNode.cs +++ b/DataStructures/Trees/BinaryTreeNode.cs @@ -12,6 +12,8 @@ public class BinaryTreeNode public BinaryTreeNode Right { get; set; } + public BinaryTreeNode Next { get; set; } + public int Value { get; set; } public BinaryTreeNode(int data)