-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbinary_search_tree_with_dup.py
68 lines (50 loc) · 1.79 KB
/
binary_search_tree_with_dup.py
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
# -*- coding: utf-8 -*-
"""
zahlen.ds.tree.binary_search_tree_with_dup
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This module implements a Binary Search Tree with duplicate or repeated
keys.
:copyright: (c) 2014 by Subhajit Ghosh.
:license: MIT, see LICENSE for more details.
"""
import binary_search_tree
class Node(binary_search_tree.Node):
def __init__(self, key):
super(Node, self).__init__(key)
self._count = 1
def __str__(self):
pass
class BinarySearchTreeDupKeys(binary_search_tree.BinarySearchTree):
"""Represents a Binary Search Tree with duplicate keys."""
def insert(self, key):
"""Inserts ``key`` in the tree."""
if not self._root:
self._root = self._Node(key)
else:
node = self._root
parent = None
while node:
parent = node
if key == node.key:
break
node = node.left if key < node.key else node.right
if key == parent.key:
parent._count += 1
elif key < parent.key:
parent.left = self._Node(key)
else:
parent.right = self._Node(key)
self._bubble_up_node_attrs(parent)
def delete(self, key, delete_all=False):
"""Deletes key ``key`` from the tree.
:param delete_all: If true, deletes all the duplicate keys. Else deletes
only a single occurrence of the key.
"""
node = self._search_node(key, silent=False)
if not delete_all and node._count > 1:
node._count -= 1
self._bubble_up_node_attrs(node)
elif node.is_leaf():
self._delete_leaf_node(node)
else:
self._delete_internal_node(node)