-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
paramag
authored and
paramag
committed
Apr 6, 2017
1 parent
f26759b
commit 15f90c5
Showing
6 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters