Giter VIP home page Giter VIP logo

tree_list's Introduction

tree_list

A tree-based list with logarithmic time insertion

Explanation

Insertion of a single element into index i of an array list is an O(n) operation, because every element from the ith onward must be shifted backward to make room for the new element. Deletion is likewise O(n). Thus, random insertion of n elements into an array list is an O(n^2) operation, which is undesirable.

A TreeList is a tree-based list. Unlike a BST, where each node stores a key, a value, and pointers to its children, a TreeList node stores the value, pointers to children, and the size of the node's left subtree. This last value is used like a key for search, insertion, and deletion. The TreeList can thereby store a sequence of ordered elements that can be inserted into and deleted from without needing to update the index of every element. Like a BST, insertion and deletion into a balanced BST can both be accomplished in O(log n) time. This makes the TreeList a more effective choice than an array list when performing a large number of insertions and deletions. In exchange, the TreeList loses the cache locality and constant-time random access properties of the array list.

As with ropes, each node in a TreeList records the size of its left subtree. A TreeList differs from a rope in that a TreeList stores values in all nodes, whereas a rope stores values exclusively in the leaves. A TreeList is also intended to store values of any type, rather than just strings.

Operations

Operating on a TreeList is mostly similar to operating on a BST.

  • Get: Given index i, perform binary search as you would with a BST. Whenever you descend to a node's right subtree, subtract that node's size_of_left_subtree + 1.
  • Insertion: Similar to BST insertion. When descending to a node's left subtree, increment its size_of_left_subtree.
  • Deletion: Similar to BST deletion. When descending to a node's left subtree, decrement its size_of_left_subtree.
  • Rotation: Similar to BST rotation. Updates to size_of_left_subtree for each node involved can be computed from the relevant node's current size_of_left_subtree parameters, as well as the size of the former root's subtree.

tree_list's People

Contributors

spinrut avatar

Watchers

 avatar

tree_list's Issues

Improve clear() method

Method is currently not tail recursive; results in excessive use of stack space.

Idea for improved algorithm:

  1. Perform left rotations about root until root has no left children.
  2. Replace root with its right child.
  3. Repeat until root is None.

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.