Over a million developers have joined DZone.

Breadth First Search - C#

· IoT Zone

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;
            }
        }
Topics:
c#

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}