From e16c25edfbaa2dcbf5deb374fece6d1ec410d6d8 Mon Sep 17 00:00:00 2001 From: cclarke411 Date: Tue, 6 Aug 2019 12:46:14 -0400 Subject: [PATCH] Add files via upload --- .../Unit-5/Lesson 1/Trees.ipynb | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 Data Science Bootcamp/Unit-5/Lesson 1/Trees.ipynb diff --git a/Data Science Bootcamp/Unit-5/Lesson 1/Trees.ipynb b/Data Science Bootcamp/Unit-5/Lesson 1/Trees.ipynb new file mode 100644 index 0000000..14ab3d4 --- /dev/null +++ b/Data Science Bootcamp/Unit-5/Lesson 1/Trees.ipynb @@ -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 +}