ISourceCode

Make the frequent cases fast and the rare case correct

C# – Tree Traversal using Parallel.Invoke Method.

Used the Parallel.Invoke method from Task Parallel library.

Invoke(Action[]) executes the tasks possibly in parallel.

Pass a list of actions to the method.

I already had a tree and i plugged this method.For more check this article

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ParallelTreeTraversal
{
	class Node
	{
		public int Item { get; set; } 
		public Node LeftChild { get; set; }
		public Node RightChild { get; set; }
	}

	class Tree
	{
		private Node root;

		public Tree()
		{ 
			root = null; 
		}

		public Node ReturnRoot()
		{
			return root;
		}

		public void Insert(int id)
		{
			Node newNode = new Node();
			newNode.Item = id;

			if (root == null)
				root = newNode;
			else
			{
				Node current = root;
				Node parent;
				while (true)
				{
					parent = current;
					if (id < current.Item)
					{
						current = current.LeftChild;
						if (current == null)
						{
							parent.LeftChild = newNode;
							return;
						}
					}
					else
					{
						current = current.RightChild;
						if (current == null)
						{
							parent.RightChild = newNode;
							return;
						}
					}
				}
			}
		}

		public void Inorder(Node root, List<int> inorder)
		{
			if (root != null)
			{
				Inorder(root.LeftChild, inorder);
				inorder.Add(root.Item);
				Inorder(root.RightChild, inorder);
			}
		}

		public void Preorder(Node root, List<int> preorder)
		{
			if (root != null)
			{
				preorder.Add(root.Item);
				Preorder(root.LeftChild, preorder);
				Preorder(root.RightChild, preorder);
			}
		}

		public void Postorder(Node root, List<int> postorder)
		{
			if (root != null)
			{
				Postorder(root.LeftChild, postorder);
				Postorder(root.RightChild, postorder);
				postorder.Add(root.Item);
			}
		}

		// By using Parallel.Invoke 
		public void TraverseTree(Tree tree)
		{
			List<int> inorder = new List<int>();
			List<int> preorder = new List<int>();
			List<int> postorder = new List<int>();

			Parallel.Invoke(
				() => {
					tree.Inorder(tree.ReturnRoot(), inorder);
				},
				() => {
					tree.Preorder(tree.ReturnRoot(), preorder);
				},
				() => {
					tree.Postorder(tree.ReturnRoot(), postorder);
				}
			);

			Console.WriteLine("Inorder traversal resulting Tree Sort");
			foreach (int item in inorder)
			{
				Console.Write(item + " ");
			}
			Console.WriteLine(" ");

			Console.WriteLine("Preorder traversal");
			foreach (int item in preorder)
			{
				Console.Write(item + " ");
			}
			Console.WriteLine(" ");

			Console.WriteLine("Postorder traversal");
			foreach (int item in postorder)
			{
				Console.Write(item + " ");
			}
			Console.WriteLine(" ");
		}
	}

	class Program
	{
		public static void Main(string[] args)
		{
			Tree theTree = new Tree();
			theTree.Insert(42);
			theTree.Insert(25);
			theTree.Insert(65);
			theTree.Insert(12);
			theTree.Insert(37);
			theTree.Insert(13);
			theTree.Insert(30);
			theTree.Insert(43);
			theTree.Insert(87);
			theTree.Insert(99);
			theTree.Insert(9);

			theTree.TraverseTree(theTree);

			Console.ReadLine();
		}
	}
}

OUTPUT:

Inorder traversal resulting Tree Sort
9 12 13 25 30 37 42 43 65 87 99
Preorder traversal
42 25 12 9 13 37 30 65 43 87 99
Postorder traversal
9 13 12 30 37 25 43 99 87 65 42

If you found the article useful, please rate the article below. Thanks !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: