Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
cclarke411 authored Aug 6, 2019
1 parent a69cc21 commit e16c25e
Showing 1 changed file with 144 additions and 0 deletions.
144 changes: 144 additions & 0 deletions Data Science Bootcamp/Unit-5/Lesson 1/Trees.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Node:\n",
" def __init__(self, val):\n",
" self.left = None\n",
" self.right = None\n",
" self.val = val"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
"root = Node(1) \n",
"root.left = Node(2) \n",
"root.right = Node(3) \n",
"root.left.left = Node(4) \n",
"root.left.right = Node(5) \n",
"root.right.left = Node(6)\n",
"root.right.right = Node(7)\n",
"root.left.left.left = Node(8)\n",
"root.left.left.right = Node(9)\n",
"root.left.right.left = Node(10)\n",
"root.left.right.right= Node(11)\n",
"root.right.left.left = Node(12)\n",
"root.right.left.right = Node(13)\n",
"root.right.right.left = Node(14)\n",
"root.right.right.right = Node(15)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"# Function to print level order traversal of tree \n",
"def Order(root): \n",
" h = height(root) \n",
" for i in range(1, h+1): \n",
" Level(root, i) \n",
" \n",
" \n",
"# Print nodes at a given level \n",
"def Level(root , level): \n",
" if root is None: \n",
" return\n",
" if level == 1: \n",
" print(\"%d\" %(root.val)), \n",
" elif level > 1 : \n",
" Level(root.left , level-1) \n",
" Level(root.right , level-1) "
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"def height(node): \n",
" if node is None: \n",
" return 0 \n",
" else : \n",
" # Compute the height of each subtree \n",
" lheight = height(node.left) \n",
" rheight = height(node.right) \n",
"\n",
" #Use the larger one \n",
" if lheight > rheight : \n",
" return lheight+1\n",
" else: \n",
" return rheight+1"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n"
]
}
],
"source": [
"Order(root)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit e16c25e

Please sign in to comment.