DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Aniruddha has posted 40 posts at DZone. View Full User Profile

Depth First Search - C#

06.01.2012
| 10925 views |
  • submit to reddit
        public class BinaryTreeNode
        {
            public BinaryTreeNode Left { get; set; }

            public BinaryTreeNode Right { get; set; }

            public int Data { get; set; }
        }

public class DepthFirstSearch
        {
            private Stack<BinaryTreeNode> _searchStack;
            private BinaryTreeNode _root;

            public DepthFirstSearch(BinaryTreeNode rootNode)
            {
                _root = rootNode;
                _searchStack = new Stack<BinaryTreeNode>();
            }

            public bool Search(int data)
            {
                BinaryTreeNode _current;
                _searchStack.Push(_root);
                while (_searchStack.Count != 0)
                {
                    _current = _searchStack.Pop();
                    if (_current.Data == data)
                    {
                        return true;
                    }
                    else
                    {
                        _searchStack.Push(_current.Right);
                        _searchStack.Push(_current.Left);
                    }
                }
                return false;
            }
        }
Depth First Search - C#