Skip to content

Commit a0ab446

Browse files
committed
trees
1 parent d42d415 commit a0ab446

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Nodes and References Implementation of a Tree\n",
8+
"\n",
9+
"In this notebook is the code corresponding to the lecture for implementing the representation of a Tree as a class with nodes and references!"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 24,
15+
"metadata": {
16+
"collapsed": true
17+
},
18+
"outputs": [],
19+
"source": [
20+
"class BinaryTree(object):\n",
21+
" def __init__(self,rootObj):\n",
22+
" self.key = rootObj\n",
23+
" self.leftChild = None\n",
24+
" self.rightChild = None\n",
25+
"\n",
26+
" def insertLeft(self,newNode):\n",
27+
" if self.leftChild == None:\n",
28+
" self.leftChild = BinaryTree(newNode)\n",
29+
" else:\n",
30+
" t = BinaryTree(newNode)\n",
31+
" t.leftChild = self.leftChild\n",
32+
" self.leftChild = t\n",
33+
"\n",
34+
" def insertRight(self,newNode):\n",
35+
" if self.rightChild == None:\n",
36+
" self.rightChild = BinaryTree(newNode)\n",
37+
" else:\n",
38+
" t = BinaryTree(newNode)\n",
39+
" t.rightChild = self.rightChild\n",
40+
" self.rightChild = t\n",
41+
"\n",
42+
"\n",
43+
" def getRightChild(self):\n",
44+
" return self.rightChild\n",
45+
"\n",
46+
" def getLeftChild(self):\n",
47+
" return self.leftChild\n",
48+
"\n",
49+
" def setRootVal(self,obj):\n",
50+
" self.key = obj\n",
51+
"\n",
52+
" def getRootVal(self):\n",
53+
" return self.key"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"We can see some examples of creating a tree and assigning children. Note that some outputs are Trees themselves!"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 35,
66+
"metadata": {
67+
"collapsed": false
68+
},
69+
"outputs": [
70+
{
71+
"name": "stdout",
72+
"output_type": "stream",
73+
"text": [
74+
"a\n",
75+
"None\n",
76+
"<__main__.BinaryTree object at 0x104779c10>\n",
77+
"b\n",
78+
"<__main__.BinaryTree object at 0x103b42c50>\n",
79+
"c\n",
80+
"hello\n"
81+
]
82+
}
83+
],
84+
"source": [
85+
"from __future__ import print_function\n",
86+
"\n",
87+
"r = BinaryTree('a')\n",
88+
"print(r.getRootVal())\n",
89+
"print(r.getLeftChild())\n",
90+
"r.insertLeft('b')\n",
91+
"print(r.getLeftChild())\n",
92+
"print(r.getLeftChild().getRootVal())\n",
93+
"r.insertRight('c')\n",
94+
"print(r.getRightChild())\n",
95+
"print(r.getRightChild().getRootVal())\n",
96+
"r.getRightChild().setRootVal('hello')\n",
97+
"print(r.getRightChild().getRootVal())"
98+
]
99+
}
100+
],
101+
"metadata": {
102+
"kernelspec": {
103+
"display_name": "Python 2",
104+
"language": "python",
105+
"name": "python2"
106+
},
107+
"language_info": {
108+
"codemirror_mode": {
109+
"name": "ipython",
110+
"version": 2
111+
},
112+
"file_extension": ".py",
113+
"mimetype": "text/x-python",
114+
"name": "python",
115+
"nbconvert_exporter": "python",
116+
"pygments_lexer": "ipython2",
117+
"version": "2.7.11"
118+
}
119+
},
120+
"nbformat": 4,
121+
"nbformat_minor": 0
122+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Nodes and References Implementation of a Tree\n",
8+
"\n",
9+
"In this notebook is the code corresponding to the lecture for implementing the representation of a Tree as a class with nodes and references!"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 24,
15+
"metadata": {
16+
"collapsed": true
17+
},
18+
"outputs": [],
19+
"source": [
20+
"class BinaryTree(object):\n",
21+
" def __init__(self,rootObj):\n",
22+
" self.key = rootObj\n",
23+
" self.leftChild = None\n",
24+
" self.rightChild = None\n",
25+
"\n",
26+
" def insertLeft(self,newNode):\n",
27+
" if self.leftChild == None:\n",
28+
" self.leftChild = BinaryTree(newNode)\n",
29+
" else:\n",
30+
" t = BinaryTree(newNode)\n",
31+
" t.leftChild = self.leftChild\n",
32+
" self.leftChild = t\n",
33+
"\n",
34+
" def insertRight(self,newNode):\n",
35+
" if self.rightChild == None:\n",
36+
" self.rightChild = BinaryTree(newNode)\n",
37+
" else:\n",
38+
" t = BinaryTree(newNode)\n",
39+
" t.rightChild = self.rightChild\n",
40+
" self.rightChild = t\n",
41+
"\n",
42+
"\n",
43+
" def getRightChild(self):\n",
44+
" return self.rightChild\n",
45+
"\n",
46+
" def getLeftChild(self):\n",
47+
" return self.leftChild\n",
48+
"\n",
49+
" def setRootVal(self,obj):\n",
50+
" self.key = obj\n",
51+
"\n",
52+
" def getRootVal(self):\n",
53+
" return self.key"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"We can see some examples of creating a tree and assigning children. Note that some outputs are Trees themselves!"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 35,
66+
"metadata": {
67+
"collapsed": false
68+
},
69+
"outputs": [
70+
{
71+
"name": "stdout",
72+
"output_type": "stream",
73+
"text": [
74+
"a\n",
75+
"None\n",
76+
"<__main__.BinaryTree object at 0x104779c10>\n",
77+
"b\n",
78+
"<__main__.BinaryTree object at 0x103b42c50>\n",
79+
"c\n",
80+
"hello\n"
81+
]
82+
}
83+
],
84+
"source": [
85+
"from __future__ import print_function\n",
86+
"\n",
87+
"r = BinaryTree('a')\n",
88+
"print(r.getRootVal())\n",
89+
"print(r.getLeftChild())\n",
90+
"r.insertLeft('b')\n",
91+
"print(r.getLeftChild())\n",
92+
"print(r.getLeftChild().getRootVal())\n",
93+
"r.insertRight('c')\n",
94+
"print(r.getRightChild())\n",
95+
"print(r.getRightChild().getRootVal())\n",
96+
"r.getRightChild().setRootVal('hello')\n",
97+
"print(r.getRightChild().getRootVal())"
98+
]
99+
}
100+
],
101+
"metadata": {
102+
"kernelspec": {
103+
"display_name": "Python 2",
104+
"language": "python",
105+
"name": "python2"
106+
},
107+
"language_info": {
108+
"codemirror_mode": {
109+
"name": "ipython",
110+
"version": 2
111+
},
112+
"file_extension": ".py",
113+
"mimetype": "text/x-python",
114+
"name": "python",
115+
"nbconvert_exporter": "python",
116+
"pygments_lexer": "ipython2",
117+
"version": "2.7.11"
118+
}
119+
},
120+
"nbformat": 4,
121+
"nbformat_minor": 0
122+
}

0 commit comments

Comments
 (0)