Updated bst to keep track of its parent and to return the node it inserts.

This commit is contained in:
Donne Martin
2015-08-16 08:21:11 -04:00
parent f26f61e949
commit 6b269d0f45
3 changed files with 20 additions and 6 deletions

View File

@@ -38,7 +38,9 @@
"* Can we assume all left descendents <= n < all right descendents?\n",
" * Yes\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.left = None\n",
" self.right = None\n",
" self.parent = None\n",
"\n",
" def __str__(self):\n",
" return str(self.data)\n",
@@ -117,13 +120,17 @@
" if data <= root.data:\n",
" if root.left is None:\n",
" root.left = Node(data)\n",
" root.left.parent = root\n",
" return root.left\n",
" else:\n",
" insert(root.left, data)\n",
" return insert(root.left, data)\n",
" else:\n",
" if root.right is None:\n",
" root.right = Node(data)\n",
" root.right.parent = root\n",
" return root.right\n",
" else:\n",
" insert(root.right, data)"
" return insert(root.right, data)"
]
},
{