![]() |
Jupyter at Bryn Mawr College |
|
|
Public notebooks: /services/public/dblank / CS206 Data Structures / 2016-Spring / Notebooks |
The Family Tree is also a recursive data structure.
class FamilyTree {
String name;
FamilyTree mother;
FamilyTree father;
FamilyTree(String name) {
this.name = name;
}
}
FamilyTree me = new FamilyTree("Doug");
me.mother = new FamilyTree("Norma");
me.father = new FamilyTree("Scotty");
System.out.println(me.name);
System.out.println(me.father.name);
The data structure. We'll use this the same way that we did the LinkedList. That is we will have:
For BinaryTrees we will use the terms left
and right
instead of next
, and root
instead of head
.
class Node {
double data;
Node left;
Node right;
Node(double data) {
this.data = data;
}
}
class BinaryTree {
Node root;
public void add(Node node) {
if (root == null) {
root = node;
} else {
///
}
}
}
Consider adding the method add(Node)
that would add nodes in no particular order. My natural instincts tell me to let the node do it:
class BinaryTree {
Node root;
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
}
...and then have Node do it recursively... but...
class Node {
double data;
Node left;
Node right;
Node(double data) {
this.data = data;
}
public void add(Node node) {
if (left == null)
left = node;
else if (right == null)
right = node;
else {
/// what would go here?
}
}
}
import java.lang.Math;
class Node {
double data;
Node left;
Node right;
Node(double data) {
this.data = data;
}
public void add(Node node) {
if (left == null)
left = node;
else if (right == null)
right = node;
else {
if (Math.random() < .5) {
left.add(node);
} else {
right.add(node);
}
}
}
}
class BinaryTree {
Node root;
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
}
BinaryTree btree = new BinaryTree();
btree.add(new Node(23));
btree.add(new Node(46));
How can we get some feedback about what is in the tree? We could print out the tree, but that is actually a bit difficult. Why?
First, let's find the "depth" of the tree:
class BinaryTree {
Node root;
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
public int depth() {
return depth(root);
}
public int depth(Node node) {
if (node == null) {
return 0;
} else {
return 1 + Math.max(depth(node.left), depth(node.right));
}
}
}
BinaryTree btree = new BinaryTree();
btree.depth()
btree.add(new Node(23));
btree.depth()
btree.add(new Node(46));
btree.depth()
btree.add(new Node(47));
btree.depth()
btree.add(new Node(47));
btree.depth()
BinaryTree btree = new BinaryTree();
for (int i=0; i < 1000000; i++) {
btree.add(new Node(i));
}
What do you think the depth is?
btree.depth();
btree.depth();
class Node {
double data;
Node left;
Node right;
Node(double data) {
this.data = data;
}
public void add(Node node) {
if (node.data < this.data) {
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
}
}
class BinaryTree {
Node root;
public int depth() {
return depth(root);
}
public int depth(Node node) {
if (node == null) {
return 0;
} else {
return 1 + Math.max(depth(node.left), depth(node.right));
}
}
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
}
BinaryTree btree = new BinaryTree();
for (int i=0; i < 1000; i++) {
btree.add(new Node(i));
}
BinaryTree btree = new BinaryTree();
for (int i=0; i < 1000000; i++) {
btree.add(new Node(Math.random()));
}
btree.depth()
class Node {
double data;
Node left;
Node right;
Node(double data) {
this.data = data;
}
}
class BinaryTree {
Node root;
public int depth() {
return depth(root);
}
public int depth(Node node) {
if (node == null) {
return 0;
} else {
return 1 + Math.max(depth(node.left), depth(node.right));
}
}
public void add(Node node) {
if (root == null) {
root = node;
} else {
Node current = root;
while (true) {
/// What goes here?
}
}
}
}