mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-07 09:58:03 +00:00
Updated bst to keep track of its parent and to return the node it inserts.
This commit is contained in:
@@ -4,6 +4,7 @@ class Node(object):
|
|||||||
self.data = data
|
self.data = data
|
||||||
self.left = None
|
self.left = None
|
||||||
self.right = None
|
self.right = None
|
||||||
|
self.parent = None
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.data)
|
return str(self.data)
|
||||||
@@ -13,10 +14,14 @@ def insert(root, data):
|
|||||||
if data <= root.data:
|
if data <= root.data:
|
||||||
if root.left is None:
|
if root.left is None:
|
||||||
root.left = Node(data)
|
root.left = Node(data)
|
||||||
|
root.left.parent = root
|
||||||
|
return root.left
|
||||||
else:
|
else:
|
||||||
insert(root.left, data)
|
return insert(root.left, data)
|
||||||
else:
|
else:
|
||||||
if root.right is None:
|
if root.right is None:
|
||||||
root.right = Node(data)
|
root.right = Node(data)
|
||||||
|
root.right.parent = root
|
||||||
|
return root.right
|
||||||
else:
|
else:
|
||||||
insert(root.right, data)
|
return insert(root.right, data)
|
||||||
@@ -38,7 +38,9 @@
|
|||||||
"* Can we assume all left descendents <= n < all right descendents?\n",
|
"* Can we assume all left descendents <= n < all right descendents?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* For simplicity, can we use just a Node class without a wrapper Tree class?\n",
|
"* For simplicity, can we use just a Node class without a wrapper Tree class?\n",
|
||||||
" * Yes"
|
" * Yes\n",
|
||||||
|
"* Do we have to keep track of the parent nodes?\n",
|
||||||
|
" * This is optional"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,7 +38,9 @@
|
|||||||
"* Can we assume all left descendents <= n < all right descendents?\n",
|
"* Can we assume all left descendents <= n < all right descendents?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* For simplicity, can we use just a Node class without a wrapper Tree class?\n",
|
"* For simplicity, can we use just a Node class without a wrapper Tree class?\n",
|
||||||
" * Yes"
|
" * Yes\n",
|
||||||
|
"* Do we have to keep track of the parent nodes?\n",
|
||||||
|
" * This is optional"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -108,6 +110,7 @@
|
|||||||
" self.data = data\n",
|
" self.data = data\n",
|
||||||
" self.left = None\n",
|
" self.left = None\n",
|
||||||
" self.right = None\n",
|
" self.right = None\n",
|
||||||
|
" self.parent = None\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __str__(self):\n",
|
" def __str__(self):\n",
|
||||||
" return str(self.data)\n",
|
" return str(self.data)\n",
|
||||||
@@ -117,13 +120,17 @@
|
|||||||
" if data <= root.data:\n",
|
" if data <= root.data:\n",
|
||||||
" if root.left is None:\n",
|
" if root.left is None:\n",
|
||||||
" root.left = Node(data)\n",
|
" root.left = Node(data)\n",
|
||||||
|
" root.left.parent = root\n",
|
||||||
|
" return root.left\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" insert(root.left, data)\n",
|
" return insert(root.left, data)\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" if root.right is None:\n",
|
" if root.right is None:\n",
|
||||||
" root.right = Node(data)\n",
|
" root.right = Node(data)\n",
|
||||||
|
" root.right.parent = root\n",
|
||||||
|
" return root.right\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" insert(root.right, data)"
|
" return insert(root.right, data)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user