Skip to content

Commit

Permalink
BST and fun with trees
Browse files Browse the repository at this point in the history
  • Loading branch information
ekong2 committed Aug 16, 2017
0 parents commit 829777e
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
Empty file added README.md
Empty file.
183 changes: 183 additions & 0 deletions Whiteboard.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Tree():\n",
" def __init__(self, value):\n",
" self.value = value\n",
" self.right = None\n",
" self.left = None\n",
"\n",
"TreeBST = Tree(100)\n",
"TreeBST.left = Tree(50)\n",
"TreeBST.left.left = Tree(25)\n",
"TreeBST.left.right = Tree (75)\n",
"TreeBST.right = Tree(200)\n",
"TreeBST.right.left = Tree(125)\n",
"TreeBST.right.right = Tree(350)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"counter = 0\n",
"def findNthHighestInBST(root, n):\n",
" if root is None:\n",
" return None\n",
" \n",
" result = findNthHighestInBST(root.right, n)\n",
" if result is not None:\n",
" return result\n",
" \n",
" global counter\n",
" \n",
" counter += 1\n",
" if counter == n:\n",
" return root.value\n",
" \n",
" result = findNthHighestInBST(root.left, n)\n",
" if result is not None:\n",
" return result\n",
" \n",
" return None\n",
"\n",
"# print (findNthHighestInBST(TreeBST, 3))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def MirrorTree(root):\n",
" if root is None:\n",
" return\n",
" \n",
" MirrorTree(root.left)\n",
" MirrorTree(root.right)\n",
" root.left, root.right = root.right, root.right\n",
" \n",
" return root\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from collections import deque\n",
"def DeleteZeroSumSubTrees(root):\n",
" if root == None:\n",
" return root\n",
" \n",
" if getSum(root) == 0:\n",
" return None\n",
" \n",
" q = deque()\n",
" q.append(root)\n",
" while q:\n",
" ele = q.popleft()\n",
" if getSum(ele.left) == 0:\n",
" ele.left = None\n",
" if getSum(ele.right) == 0:\n",
" ele.right = None\n",
" \n",
" if ele.left != None:\n",
" q.append(ele.left)\n",
" if ele.right != None:\n",
" q.append(ele.right)\n",
" \n",
" return root\n",
"\n",
"def getSum(root):\n",
" sum = 0\n",
" if root == None:\n",
" return sum\n",
" \n",
" sum += root.value + getSum(root.left) + getSum(root.right)\n",
" return sum\n",
"\n",
"# print (DeleteZeroSumSubTrees(TreeBST).value)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def deleteZeroSumSubTrees2(root):\n",
" if root != None:\n",
" sum = deleteZeroSumSubTrees2Recursive(root)\n",
" if sum == 0:\n",
" root = None\n",
" \n",
" return root\n",
"\n",
"def deleteZeroSumSubTrees2Recursive(root):\n",
" if root = None:\n",
" return 0\n",
" \n",
" leftSum = deleteZeroSumSubTrees2Recursive(root.left)\n",
" rightSum = deleteZeroSumSubTrees2Recursive(root.right)\n",
" \n",
" if leftSum == 0:\n",
" root.left = None\n",
" \n",
" if rightSum == 0:\n",
" root.right = None\n",
" \n",
" return root.value + leftSum + rightSum"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 829777e

Please sign in to comment.