Git Product home page Git Product logo

data_structures's People

Contributors

prawn-cake avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

data_structures's Issues

Update BST searches from recursive to iterative

    @classmethod
    def pre_order_traversal(cls, node):
        order = []
        stack = [node]
        while stack:
            current_node = stack.pop()
            order.append(current_node.value)
            if current_node.right:
                stack.append(current_node.right)

            # Put left always on top of stack
            if current_node.left:
                stack.append(current_node.left)

        return order
...
    @classmethod
    def level_order_traversal(cls, node):
        """Level order traversal is implemented with queue
        Steps:
            1. Add node value to order list and go ahead
            2. Put next node to the queue (left AND/OR right if exists)
            3. Declare while loop and repeat first two steps until the length
            is 0
        """
        order = []
        queue = deque([node])

        while len(queue) != 0:
            current_node = queue.pop()
            order.append(current_node.value)
            if current_node.left:
                queue.appendleft(current_node.left)
            if node.right:
                queue.appendleft(current_node.right)

        return order

@classmethod
    def in_order_traversal(cls, node):
        order = []
        stack = []

        current_node = node
        while stack or current_node is not None:
           # go down till the last left child
            if current_node is not None:
                stack.append(current_node)
                current_node = current_node.left
            else:
                # once we reach last left child start to visit closest right child
                current_node = stack.pop()
                order.append(current_node)
                current_node = current_node.right
        return order
...
etc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.