Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Depth First Search - C#

DZone's Guide to

Depth First Search - C#

· IoT Zone
Free Resource

Build an open IoT platform with Red Hat—keep it flexible with open source software.

Depth First Search - C#

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

            public BinaryTreeNode Right { get; set; }

            public int Data { get; set; }
        }

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

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

            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;
            }
        }

Download Red Hat’s blueprint for building an open IoT platform—open source from cloud to gateways to devices.

Topics:
c#

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}