CSE 114 – Computer Science I Lecture 1: Introduction
Download
Report
Transcript CSE 114 – Computer Science I Lecture 1: Introduction
CSE 214 – Computer Science II
Binary Trees
Source: http://thegoddessblogs.com/wp-content/uploads/2007/04/wawona_tree_yosemite_pc_002.jpg
Coding Exam 1
• Next Friday, CS 2129
– 2:15pm – 5:30 pm
• Linked Lists Only
– HWs 1 & 2
• Sample exam has been posted to schedule page
Coding Exam 1 Review Session
• Thursday evening
• Room/Time TBD
• I’ll post online/email class when I know
Trees
• A non-linear data structure
• Components do not form a single simple sequence
• Provide dramatic efficiencies for accessing data &
problem reduction
Common Tree Terms
• Node – building block
• Root – the top most node, provides access to all
other nodes
• Child node – a node accessible from another
node, it’s parent node
• Leaf – a node with no children (children are null)
BA $ 85.18
AIG $ 46.11
MO $ 72.53
Binary Trees
• Has a single root node
• Each node stores a reference to up to 2 other
different nodes
– left child & right child
• Each node has exactly 1 parent except the root
• The root has no parents
More terms
• Sibling – two nodes are siblings if they have the same parent
• Ancestor – a node’s parent is its first ancestor, the parent of the
parent is its next ancestor, etc.
• Descendant – a node’ children are its first descendants, etc.
• Subtree – Any node in a tree can also be viewed as the root of a
new, smaller tree (can be called a subtree)
– A binary tree has a left subtree & right subtree
• Depth of a node – starting at a node, the number of steps up to
reach the root
• Depth of a tree – the maximum depth of any of its leaves
Full Binary Tree
• Every leaf has the same depth and every non-leaf
has 2 children
BA $ 85.18
AIG $ 46.11
AA $ 35.72
AXP $ 45.11
MO $ 72.53
MMM $ 79.95
T $ 37.88
Complete Binary Tree
• Every level, except the deepest, must contain as
many nodes as possible
• The deepest level must have nodes packed as far
left as possible
BA $ 85.18
AIG $ 46.11
AA $ 35.72
AXP $ 45.11
MO $ 72.53
MMM $ 79.95
Exercise
• Draw:
– a full binary tree with 15 nodes
– a complete binary tree with 8 nodes
Full binary tree with 15 nodes
Complete binary tree with 8 nodes
Just to thoroughly confuse you
• Many times complete binary trees are actually
stored in packed arrays
C
O
N
F
U
S
E
D
C
O
N
F
U
S
E
D
Trees as Arrays
•
•
•
•
No more node classes, just data in an array
Methods know how to access data properly
The root of the tree is always at index 0
For a node at index i, what is the index of its
parent node?
– floor((i-1)/2)
• For a node at index I, what is the index of its child
nodes?
– left child at floor(2i + 1)
– right child at floor(2i + 2)
Why would we do this?
public class ArrayBinaryTree
{
private int numElements = 15;
private Object[] data = new Object[numElements];
public ArrayBinaryTree() {}
…
}
• To squeeze performance out of our program
– more efficient memory management
– faster for certain problems
Why not just use sequential arrays?
• We may want the best of both worlds
– efficient problem solving of trees
• trees are good at reducing problems
• trees are good at divide & conquer type algorithms
– speed of processing of arrays
– minimal memory overhead of arrays
• we don’t need to store node data, like links between nodes
• What’s a drawback?
– if tree needs to add more nodes, it’s expensive
• we may have to reconstruct array
But let’s start with something more familiar
• Let’s look again at:
– a tree managing class
– a node class
• Let’s have it store generic data
public class GenericCompleteBinaryTree <E>
{
What’s
private BinaryTreeNode root = null;
public GenericCompleteBinaryTree() {}
inside data?
Whatever we want to
put there, even other
data structures
class BinaryTreeNode <E>
{
protected E data = null;
protected BinaryTreeNode<E> leftNode = null;
protected BinaryTreeNode<E> rightNode = null;
BinaryTreeNode(
E initData,
BinaryTreeNode<E> initLeftNode,
BinaryTreeNode<E> initRightNode)
{
data = initData;
leftNode = initLeftNode;
rightNode = initRightNode;
}
}
}
Trees & Recursion
• To manipulate trees it’s common to:
– keep the tree sorted
– periodically rearrange the tree for better algorithmic
payoff
– have recursive methods that take a node or nodes as
arguments
– have recursive methods defined inside the node class
– have tree methods call a method recursively on all
nodes using one of the previously mentioned
More common uses of trees
•
•
•
•
Decision trees
AI trees
Dialog trees
Etc.