Skip to content

Commit d42d415

Browse files
committed
list rep tree
1 parent 5aa55ce commit d42d415

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed
Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,74 @@
11
{
2-
"cells": [],
3-
"metadata": {},
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Tree Representation Implementation (Lists)\n",
8+
"\n",
9+
"Below is a representation of a Tree using a list of lists. Refer to the video lecture for an explanation and a live coding demonstration!"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {
16+
"collapsed": true
17+
},
18+
"outputs": [],
19+
"source": [
20+
"def BinaryTree(r):\n",
21+
" return [r, [], []]\n",
22+
"\n",
23+
"def insertLeft(root,newBranch):\n",
24+
" t = root.pop(1)\n",
25+
" if len(t) > 1:\n",
26+
" root.insert(1,[newBranch,t,[]])\n",
27+
" else:\n",
28+
" root.insert(1,[newBranch, [], []])\n",
29+
" return root\n",
30+
"\n",
31+
"def insertRight(root,newBranch):\n",
32+
" t = root.pop(2)\n",
33+
" if len(t) > 1:\n",
34+
" root.insert(2,[newBranch,[],t])\n",
35+
" else:\n",
36+
" root.insert(2,[newBranch,[],[]])\n",
37+
" return root\n",
38+
"\n",
39+
"def getRootVal(root):\n",
40+
" return root[0]\n",
41+
"\n",
42+
"def setRootVal(root,newVal):\n",
43+
" root[0] = newVal\n",
44+
"\n",
45+
"def getLeftChild(root):\n",
46+
" return root[1]\n",
47+
"\n",
48+
"def getRightChild(root):\n",
49+
" return root[2]"
50+
]
51+
}
52+
],
53+
"metadata": {
54+
"kernelspec": {
55+
"display_name": "Python 2",
56+
"language": "python",
57+
"name": "python2"
58+
},
59+
"language_info": {
60+
"codemirror_mode": {
61+
"name": "ipython",
62+
"version": 2
63+
},
64+
"file_extension": ".py",
65+
"mimetype": "text/x-python",
66+
"name": "python",
67+
"nbconvert_exporter": "python",
68+
"pygments_lexer": "ipython2",
69+
"version": "2.7.11"
70+
}
71+
},
472
"nbformat": 4,
573
"nbformat_minor": 0
674
}

Trees/Tree Representation Implementation (Lists).ipynb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,48 @@
55
"metadata": {},
66
"source": [
77
"# Tree Representation Implementation (Lists)\n",
8-
"\n"
8+
"\n",
9+
"Below is a representation of a Tree using a list of lists. Refer to the video lecture for an explanation and a live coding demonstration!"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {
16+
"collapsed": true
17+
},
18+
"outputs": [],
19+
"source": [
20+
"def BinaryTree(r):\n",
21+
" return [r, [], []]\n",
22+
"\n",
23+
"def insertLeft(root,newBranch):\n",
24+
" t = root.pop(1)\n",
25+
" if len(t) > 1:\n",
26+
" root.insert(1,[newBranch,t,[]])\n",
27+
" else:\n",
28+
" root.insert(1,[newBranch, [], []])\n",
29+
" return root\n",
30+
"\n",
31+
"def insertRight(root,newBranch):\n",
32+
" t = root.pop(2)\n",
33+
" if len(t) > 1:\n",
34+
" root.insert(2,[newBranch,[],t])\n",
35+
" else:\n",
36+
" root.insert(2,[newBranch,[],[]])\n",
37+
" return root\n",
38+
"\n",
39+
"def getRootVal(root):\n",
40+
" return root[0]\n",
41+
"\n",
42+
"def setRootVal(root,newVal):\n",
43+
" root[0] = newVal\n",
44+
"\n",
45+
"def getLeftChild(root):\n",
46+
" return root[1]\n",
47+
"\n",
48+
"def getRightChild(root):\n",
49+
" return root[2]"
950
]
1051
}
1152
],

0 commit comments

Comments
 (0)