Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

binary tree

  • Loading branch information...
commit 431e732adfbff9d222febcdc8e7dc99a658fb7e7 1 parent 8b54203
@robertwb robertwb authored
Showing with 64 additions and 0 deletions.
  1. +6 −0 binary_tree_cython.h
  2. +58 −0 binary_tree_cython.pyx
View
6 binary_tree_cython.h
@@ -0,0 +1,6 @@
+/** Constructs a new object of type zzz_type by calling tp_new
+* directly, with no arguments.
+*/
+
+#define PY_NEW(zzz_type, args) \
+(((PyTypeObject*)(zzz_type))->tp_new((PyTypeObject*)(zzz_type), args, NULL))
View
58 binary_tree_cython.pyx
@@ -0,0 +1,58 @@
+cdef extern from "binary_tree_cython.h":
+ object PY_NEW(object, args)
+ void disable_gc(object)
+
+cdef class Node:
+ cdef Node left
+ cdef Node right
+ cdef long item
+
+cdef Node make_tree(long item, long depth):
+ cdef Node t = <Node>PY_NEW(Node, ())
+ t.item = item
+ if depth:
+ depth -= 1
+ item <<= 1
+ t.left = make_tree(item - 1, depth)
+ t.right = make_tree(item, depth)
+ return t
+
+cdef long check_tree(Node t):
+ if t.left is None:
+ return t.item
+ else:
+ return t.item + check_tree(t.left) - check_tree(t.right)
+
+
+def test(int n):
+
+ # By definition, trees can't have cycles. However, Python's garbage
+ # collector will do lots of wasteful tree traversals to try to collect
+ # circular references. Normal reference counting still happens.
+ import gc
+ gc.disable()
+
+ cdef int min_depth, max_depth, stretch_depth, depth
+ min_depth = 4
+ max_depth = max(min_depth + 2, n)
+ stretch_depth = max_depth + 1
+
+ cdef int i, iterations
+ cdef long check
+
+ print "stretch tree of depth %d\t check:" % stretch_depth, check_tree(make_tree(0, stretch_depth))
+
+ long_lived_tree = make_tree(0, max_depth)
+
+ iterations = 1 << max_depth
+ for depth in xrange(min_depth, stretch_depth, 2):
+
+ check = 0
+ for i in xrange(1, iterations + 1):
+ check += check_tree(make_tree(i, depth))
+ check += check_tree(make_tree(-i, depth))
+
+ print "%d\t trees of depth %d\t check:" % (iterations * 2, depth), check
+ iterations /= 4
+
+ print "long lived tree of depth %d\t check:" % max_depth, check_tree(long_lived_tree)
Please sign in to comment.
Something went wrong with that request. Please try again.