Download Tree - UMass CS !EdLab

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Red–black tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
CMPSCI 187
Introduction to Programming
with Data Structures
Computer Science 187
Lecture 16: Trees
Announcements
1. Programming project 4 is due Friday Nov 3
by 5 pm..
2. The next OWL assignment is up and due on
November 8 at 11 pm
3. HWK2 grades are on OWL
4. No….I haven’t (but I will this weekend!)
1
CMPSCI 187
Introduction to Programming
with Data Structures
Trees
trees
binary trees
traversals of trees
data structures for trees
Balancing unbalanced trees
2
CMPSCI 187
Trees


Trees can represent a hierarchy
Corporate structure
3
CMPSCI 187
Trees

Table of contents of a book:
4
CMPSCI 187
Trees


Unix or Macintosh file system
Web page hierarchy
File
Directory
cs121
cs187
Interfaces
In
Java
Java
GUI
Design
8
Queens
5
CMPSCI 187
Trees and Tree Terminology


A tree is a data structure that consists of a set
of nodes and a set of edges (or branches)
that connect nodes.
Trees are our first example of a nonlinear data
structure.
 Components do not form simple sequences, like lists.
 Trees represent hierarchies.
6
CMPSCI 187
Tree: Definition


A tree is a collection of elements (nodes)
Each node may have 0 or more successors
 (Unlike

a list, which has 0 or 1 successor)
Each node has exactly one predecessor
 Except
the starting / top node, called the root.
 Root has zero predecessors
7
CMPSCI 187
Terminology









A is the root node.
B is the parent of D and E. (or ancester)
C is the sibling of B
D and E are the children of B. (or descendents)
D, E, F, G, I are external nodes, or leaves
A, B, C, H are internal nodes
The depth (level) of E is 2
The height of the tree is 3
The degree of node B is 2
Property: (# edges) = (#nodes) - 1
8
CMPSCI 187
More tree terminology

Subtree of a node:
A tree whose root is a child of that node

Level of a node:
A measure of its distance from the root:
Level of the root = 0
Level of other nodes = 1 + level of parent
9
CMPSCI 187
Binary Trees
Computer scientists like
to look at tree upside
down!
A Family Tree is a binary tree
(each node has 0, 1, or 2 branches)
10
CMPSCI 187
Binary Trees
 A binary
tree is an ordered tree in which every node
has at most two children (or at most two nonempty subtrees).
 A set of nodes T is a binary tree if:
 T is empty
 or T consists of a
 root node
(internal node)
 a left binary tree and
 a right binary tree
((((3 x (1 + (4 + 6))) + (2 + 8)) x 5) + ( 4 x (7 + 2)))
11
CMPSCI 187
Expression Trees
+
/
a
*
2
6
b
a/2 + 6*b
12
CMPSCI 187
Expression Trees: An Aside I
+
/
a
*
2
6
public void infix ()
{
if (isEmpty ()) return;
left.infix ();
System.out.println (value ());
right.infix ();
}
b
a/2 + 6*b
Note that we can traverse this entire structure without any
backpointers from child nodes to parent nodes. Whenever
we need to back up, we just pop the stack and pick up where
we left off. Recursion makes traversing binary trees easy. 13
CMPSCI 187
Expression Trees: An Aside II
+
/
a
*
2
6
public void postfix ()
{
if (isEmpty ()) return;
left.postfix ();
right.postfix ();
System.out.println (value ());
}
b
a 2 / 6 b * +
hmmmm….looks like the postfix
form of the expression
14
CMPSCI 187
Binary Tree Example
Decision Tree
Rao’s
15
CMPSCI 187
Binary Sort Trees

Binary sort (or search) trees have this useful
property: An inorder traversal of the tree will process
the items in increasing order.


Relationship to binary search????
Items in left subtree < item at root < items in right subtree
16
CMPSCI 187
Properties of Binary Trees






(# external nodes ) = (# internal nodes) + 1
i
(# nodes at level i)  2
(# external nodes)  2 (height)
(height)  log2 (# external nodes)
(height)  log2 (# nodes) - 1
(height)  (# internal nodes) = ((# nodes) - 1)/2
17
CMPSCI 187
Full Binary Trees
In a full binary tree,

 every leaf node has the same depth
 every non-leaf node (internal node) has two children.
A
A
B
C
D
H
E
I
J
F
K
Not a full binary tree.
B
G
C
D
L
H
E
I
J
F
K
M
G
N
O
L
A full binary tree.
18
CMPSCI 187
Complete Binary Tree

In a complete binary tree
 every level except the deepest must contain as many nodes as
possible ( that is, the tree that remains after the deepest level is
removed must be full).
 at the deepest level, all nodes are as far to the left as possible.
A
A
B
C
D
H
E
I
J
F
C
B
G
K
Not a Complete Binary Tree
D
L
H
F
E
I
J
K
G
L
A Complete Binary Tree
19
CMPSCI 187

A Representation for
Complete Binary Trees
Since tree is full, it maps nicely onto an array representation.
A
C
B
D
H
T:
A
B
C
D
E
0
1
2
3
4
F
E
I
J
F
G
H
I
J
K
L
6
7
8
9
10
11 12
5
K
G
L
last
20
CMPSCI 187


Properties of the Array
Representation
Data from the root node is always in T[0].
Suppose some node appears in T[i]
 data for its parent is always at location T[(i-1)/2]
(using integer division)
 data for its children nodes appears in locations
T[2*i+1] for the left child
T[2*i+2] for the right child
 formulas provide an implicit representation of the edges
 can use these formulas to implement efficient algorithms
for traversing the tree and moving around in various
ways.
21
CMPSCI 187
Proper Binary Tree

In a proper binary tree,
 each
node has exactly zero or two children
 each internal node has exactly two children.
A
A
B
D
H
E
I
B
C
J
F
K
D
G
L
Not a proper binary tree
C
H
E
I
J
F
K
G
L
M
A proper binary tree
22
CMPSCI 187
Not Complete or Full or Proper
A
C
B
D
H
F
E
I
J
K
L
23
CMPSCI 187
Comparing Trees
These two trees are not the same tree!
A
A
B
B
Why?
24
CMPSCI 187
Binary Tree Nodes
Data
Left
Data
Left
Right
Right
Data
Left
Right
{Could put in a reference to a node's parent as well}
25
CMPSCI 187
Tree Implementation Plan


Different from Koffman and Wolfgang
Define interfaces for



Do full implementation of a binary tree



Complete code
But we’ll only hit the highlights in lecture
Read the book to see what they did




BinaryTreeNode
Binary Tree
Very restricted binary tree implementation
Use it to define binary sort trees
Never does a standard binary tree implementation
Jave does even less … we’ll see later
26
CMPSCI 187
An Interface for Nodes in a Binary Tree
Basically the set and get methods for the data fields of
the binary tree node……….
public interface BinaryNodeInterface
{
public Object getData(); //returns the data element at this node.
public void setData(Object myElement); //sets data element to myElement
public void setLeftChild(BinaryNodeInterface myLeft); //set left child to myLeft
public void setRightChild(BinaryNodeInterface myRight); //set right child to myRight
public BinaryNodeInterface getLeftChild(); //returns left child node
public BinaryNodeInterface getRightChild(); //returns right child node
public boolean hasLeftChild(); //returns true if this node has a left child
public boolean hasRightChild(); //returns true if this node has a right child
public boolean isLeaf(); //returns true if node is a leaf node.
}
27
CMPSCI 187
The BTreeNode Class
public class BinaryNode implements BinaryNodeInterface,
java.io.serializable
{ protected Object data;
protected BinaryNode left;
protected BinaryNode right;
//Constructors
public BinaryNode()
{this(null);}
public BinaryNode(Object dataElement)
{ this(dataElement, null,null; }
•
•
•
28
CMPSCI 187
The BTreeNode Class
Constructor
public BinaryNode(Object dataElement,
BinaryNode leftChild, BinaryNode rightChild)
{
data = dataElement;
left = leftChild;
right = rightChild;
}
•
•
•
29
CMPSCI 187
The BTreeNode Class
public boolean isLeaf()
{ return ((left == null) && (right == null)); }
public Object getData()
{ return data;}
public void setData(Object newData)
{ data=newData;}
•
•
•
30
CMPSCI 187
Set,Get LeftChild
public void setLeftChild(BinaryNodeInterface leftChild)
{ left = leftChild;}
Set,Get RightChild
The BTreeNode Class
public void setRightChild(BinaryNodeInterface rightChild)
{ right = rightChild;}
public BinaryNodeInterface getLeftChild()
{ return left;}
public BinaryNodeInterface getRightChild()
{ return right;}
•
•
•
31
CMPSCI 187
The BTreeNode Class
Public boolean hasLeftChild()
{ return left != null }
Public boolean hasRightChild()
{ return right != null }
}//end Binary Node
32
CMPSCI 187
Operations on Binary Trees
tree1:

Common operations on trees:









root:
Create a tree
Determine if a tree is empty
Empty a tree
Set and Get the element at the root
Get the left and right subtrees at the root
Create a left and right subtree for the root
Attach a tree as the left or right subtree of the root
Detach the left or right subtree of the root
Can now define an ADT for the binary tree
root:
tree1:
33
CMPSCI 187
The BinaryTree<E> Class
34