Skip to content

Commit

Permalink
tree problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
paramag authored and paramag committed Apr 6, 2017
1 parent f26759b commit 15f90c5
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 2 deletions.
Binary file modified Algorithm/.vs/Algorithms/v14/.suo
Binary file not shown.
2 changes: 2 additions & 0 deletions Algorithm/BinaryTree/Algorithms.Problem.BinaryTree.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConnectNextNode.cs" />
<Compile Include="LevelSpiralOrder.cs" />
<Compile Include="LowestCommonAncestor.cs" />
<Compile Include="PathRootToLeaf.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
106 changes: 106 additions & 0 deletions Algorithm/BinaryTree/ConnectNextNode.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
6 changes: 4 additions & 2 deletions Algorithm/BinaryTree/LowestCommonAncestor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 66 additions & 0 deletions Algorithm/BinaryTree/PathRootToLeaf.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
2 changes: 2 additions & 0 deletions DataStructures/Trees/BinaryTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 15f90c5

Please sign in to comment.