Upload
simon-horn
View
219
Download
0
Tags:
Embed Size (px)
Citation preview
Introduction to Algorithms and Data Structures
Lecture 12 - “I think that I shall never see.. a data structure lovely as a”
Binary Tree
What is a Binary Tree
• A binary tree is a a collection of nodes that consists of the root and other subsets to the root points, which are called the left and right subtrees.
• Every parent node on a binary tree can have up to two child nodes (roots of the two subtrees); any more children and it becomes a general tree.
• A node that has no children is called a leaf node.
A Few Terms Regarding Binary Trees
A
E F
CB
G
D
A is the rootB and C are A’s childrenA is B’s parentB & C are the root of two subtreesD, E, and G are leaves
This is NOT A Binary Tree
A
E F
CB
GD H
This is a general tree because C has three child nodes
This is NOT A Binary Tree
A
E F
CB
GD
This is a graph because A, B, E, H, F and C form a circuit
H
Tree Traversal
• There are three common ways to traverse a tree:– Preorder: Visit the root, traverse the left subtree
(preorder) and then traverse the right subtree (preorder)
– Postorder: Traverse the left subtree (postorder), traverse the right subtree (postorder) and then visit the root.
– Inorder: Traverse the left subtree (in order), visit the root and the traverse the right subtree (in order).
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDECFGPostorder: DEBGFCAIn Order: DBEAFGC
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: A(visit each node as your reach it)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: AB(visit each node as your reach it)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABD(visit each node as your reach it)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDE(visit each node as your reach it)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDEC(visit each node as your reach it)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDECF(visit each node as your reach it)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDECFG(visit each node as your reach it)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder:
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder:
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: D
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DE
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEB
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEB
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEB
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEBG
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEBGF
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEBGFC
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEBGFCA
Tree Traversals: An ExampleA
E F
CB
G
D
In Order:
Tree Traversals: An ExampleA
E F
CB
G
D
In Order:
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: D
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DB
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBE
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEA
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEA
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEAF
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEAFG
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEAFGC
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDECFGPostorder: DEBGFCAIn Order: DBEAFGC
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIECGPostorder: HIFDEBGCAIn Order: DHFIBEACG
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: A
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: AB
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABD
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDF
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFH
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHI
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIE
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIEC
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIECG
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder:
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder:
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder:
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder:
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: H
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HI
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIF
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFD
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDE
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEB
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEB
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEBG
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEBGC
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEBGCA
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order:
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order:
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: D
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: D
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DH
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHF
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFI
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIB
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIBE
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIBEA
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIBEAC
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIBEACG
G
H
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIECGPostorder: HIFDEBGCAIn Order: DHFIBEACG
G
H
Basic Implementation of a Binary Tree
• We can implement a binary in essentially the same way as a linked list, except that there are two nodes that comes next:public class Node {
private int data;
private Node left;
private Node right;
public int getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setData(int x) {
data = x;
}
public void setLeft(Node p) {
left = p;
}
public void setRight(Node p) {
right = p;
}
}
The tree Class
public class Tree {
private Node root;
// tree() - The default constructor – Starts
// the tree as empty
public Tree() {
root = null;
}
// Tree() - An initializing constructor that
// creates a node and places in it
// the initial value
public Tree(int x) {
root = new Node();
root.setData(x);
root.setLeft(null);
root.setRight(null);
}
public Node getRoot() {
return root;
}
// newNode() - Creates a new node with a
// zero as data by default
public Node newNode() {
Node p = new Node();
p.setData(0);
p.setLeft(null);
p.setRight(null);
return(p);
}
// newNode() - Creates a new node with the
// parameter x as its value
public Node newNode(int x) {
Node p = new Node();
p.setData(x);
p.setLeft(null);
p.setRight(null);
return(p);
}
//travTree() – initializes recursive
// traversal of tree
public void travTree() {
if (root != null)
travSubtree(root);
System.out.println();
}
//travSubtree() – recursive method used to
// traverse a binary tree (inorder)
public void travSubtree(Node p) {
if (p != null) {
travSubtree(p.getLeft());
System.out.print(p.getData()
+ "\t");
travSubtree(p.getRight());
}
}
// addLeft() - Inserts a new node containing
// x as the left child of p
public void addLeft(Node p, int x) {
Node q = newNode(x);
p.setLeft(q);
}
// addRight() - Inserts a new node containing
// x as the right child of p
public void addRight(Node p, int x) {
Node q = newNode(x);
p.setRight(q);
}
}
A Basic Search Tree
• We can construct a simple search tree if we add new nodes with value x on the tree using this strategy:– Every time x is less than the value in the node
we move down to the left.– Every time x is greater than the value in the
node we move down to the right.
A Sample Search Tree
15
12 18
228
20
5 23
16107
// insert() - Insert value x in a new node to
// be inserted after p
public void insert(int x) {
Node p, q;
boolean found = false;
p = root;
q = null;
while (p != null && !found) {
q = p;
if (p.getData() == x)
found = true;
else if (p.getData() > x)
p = p.getLeft();
else
p = p.getRight();
}
if (found)
error("Duplicate entry");
if (q.getData() > x)
addLeft(q, x);
else
addRight(q, x);
//q = newNode(x);
}
// isXThere() - Is there a node in the
// tree containing x?
public boolean isXThere(int x) {
Node p;
boolean found = false;
p = root;
while (p != null && !found) {
if (p.getData() == x)
found = true;
else if (p.getData() > x)
p = p.getLeft();
else
p = p.getRight();
}
return(found);
}
public void error(String message) {
System.out.println(message);
System.exit(0);
}
// getNode() - Get the pointer for the
// node containing x
public Node getNode(int x) {
Node p, q;
boolean found = false;
p = root;
q = null;
while (p != null && !found) {
q = p;
if (p.getData() == x)
found = true;
else if (p.getData() > x)
p = p.getLeft();
else
p = p.getRight();
}
if (found)
return(q);
else
return(null);
}
public class TestTree {
public static void main(String[] args) {
Tree mytree = new Tree(8);
mytree.addLeft(mytree.getRoot(), 6);
mytree.addRight(mytree.getRoot(), 9);
mytree.insert(4);
mytree.insert(1);
mytree.insert(12);
if (mytree.isXThere(13))
System.out.println("great");
else
System.out.println("not great, Bob");
mytree.travTree();
}
}
Tracing TestTree
8
Tracing TestTree
8
6
Tracing TestTree
8
96
Tracing TestTree
8
96
4
Tracing TestTree
8
96
4
1
Tracing TestTree
8
96
4 12
1