Browse Source

RBTree seems generally OK now

git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@11379 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/bxinterval-return
Jim Paris 9 years ago
parent
commit
d34b980516
2 changed files with 29 additions and 8 deletions
  1. +1
    -1
      nilmdb/rbtree.py
  2. +28
    -7
      tests/test_rbtree.py

+ 1
- 1
nilmdb/rbtree.py View File

@@ -187,7 +187,7 @@ class RBTree(object):
# y is the node to splice out, x is its child
y.left = z.left
y.right = z.right
z.parent = z.parent
y.parent = z.parent
z.left.parent = y
z.right.parent = y
if z is z.parent.left:


+ 28
- 7
tests/test_rbtree.py View File

@@ -26,23 +26,44 @@ class TestRBTree:
def test_rbtree_big(self):
import random
random.seed(1234)
# make a set of 500 intervals

# make a set of 500 intervals, inserted in order
rb = RBTree()
j = 500
for i in xrange(j):
rb.insert(RBNode(None, i, i+1))

# show the graph
rb.render_dot_live("in-order insert")

# remove about half of them
for i in random.sample(xrange(j),j):
if random.randint(0,1):
rb.delete(rb.find(i, i+1))

# show the graph
rb.render_dot_live("in-order insert, random delete")

# make a set of 500 intervals, inserted at random
rb = RBTree()
j = 500
for i in random.sample(xrange(j),j):
print "inserting", i
rb.insert(RBNode(None, i, i+1))

# show the graph
rb.render_dot_live("after insert")
rb.render_dot_live("random insert")

# remove about half of them
for i in random.sample(xrange(j),j):
if random.randint(0,1):
print "deleting", i
if i == 300:
rb.render_dot_live("before deleting 300")
rb.delete(rb.find(i, i+1))

# show the graph
rb.render_dot_live("after delete")
rb.render_dot_live("random insert, random delete")

# in-order insert of 250 more
for i in xrange(250):
rb.insert(RBNode(None, i+500, i+501))

# show the graph
rb.render_dot_live("random insert, random delete, in-order insert")

Loading…
Cancel
Save