Over a million developers have joined DZone.
Platinum Partner

Breadth First Search - C#

· IoT Zone

The IoT Zone is brought to you in partnership with ThingWorx. Explore the ThingWorx Developer Zone and learn how to rapidly develop IoT applications.

Breadth First Search using C#

class BinaryTreeNode
        {
            public BinaryTreeNode Left { get; set; }

            public BinaryTreeNode Right { get; set; }

            public int Data { get; set; }
        }


        public class BreadthFirstSearch
        {
            private Queue _searchQueue;
            private BinaryTreeNode _root;

            public BreadthFirstSearch(BinaryTreeNode rootNode)
            {
                _searchQueue = new Queue();
                _root = rootNode;
            }

            public bool Search(int data)
            {
                BinaryTreeNode _current = _root;
                _searchQueue.Enqueue(_root);

                while (_searchQueue.Count != 0)
                {
                    _current = _searchQueue.Dequeue();
                    if (__current.Data == data)
                    {
                        return true;
                    }
                    else
                    {
                        _searchQueue.Enqueue(_current.Left);
                        _searchQueue.Enqueue(_current.Right);
                    }
                }

                return false;
            }
        }

The IoT Zone is brought to you in partnership with ThingWorx. ThingWorx is the most complete application development platform available that is purpose-built for the IoT.

Topics:
c#

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}