File: binarytree.tlc.src

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (138 lines) | stat: -rw-r--r-- 1,919 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
main:
    CALL newnode
    PICK 0
    PUSH 20
    SEND insert/1

    PICK 0
    PUSH 10
    SEND insert/1

    PICK 0
    PUSH 15
    SEND insert/1

    PICK 0
    PUSH 30
    SEND insert/1

    PICK 0
    PUSHARG
    SEND search/1

    RETURN


newnode:
    NEW value,left,right,isempty=isempty,insert=insert,search=search
    RETURN

isempty:
    PUSHARG
    GETATTR value
    BR_COND isempty_not
    PUSH 1
    RETURN
  isempty_not:
    PUSH 0
    RETURN

insert: # (n)
    # if self.isempty goto insert_empty
    PUSHARG
    SEND isempty/0
    BR_COND insert_empty

    # if n == self.value goto insert_found
    PUSHARGN 1
    PUSHARG
    GETATTR value
    EQ
    BR_COND insert_found

    # if n < self.value goto insert_left
    PUSHARGN 1
    PUSHARG
    GETATTR value
    LT
    BR_COND insert_left

  insert_right:
    # self.right.insert(n)
    PUSHARG
    GETATTR right
    PUSHARGN 1
    SEND insert/1
    RETURN

  insert_left:
    # self.left.insert(n)
    PUSHARG
    GETATTR left
    PUSHARGN 1
    SEND insert/1
    RETURN

  insert_found:
    RETURN

  insert_empty:
    # self.value = n
    PUSHARG
    PUSHARGN 1
    SETATTR value

    # self.left = Node()
    PUSHARG
    CALL newnode
    SETATTR left

    # self.right = Node()
    PUSHARG
    CALL newnode
    SETATTR right

    RETURN


search: # (n)
    # if self.isempty goto search_empty
    PUSHARG
    SEND isempty/0
    BR_COND search_empty

    # if n == self.value goto search_found
    PUSHARGN 1
    PUSHARG
    GETATTR value
    EQ
    BR_COND search_found

    # if n < self.value goto search_left
    PUSHARGN 1
    PUSHARG
    GETATTR value
    LT
    BR_COND search_left

  search_right:
    PUSHARG
    GETATTR right
    PUSHARGN 1
    SEND search/1
    RETURN

  search_left:
    PUSHARG
    GETATTR left
    PUSHARGN 1
    SEND search/1
    RETURN

  search_found:
    PUSH 1
    RETURN

  search_empty:
    PUSH 0
    RETURN