Download presentation
Presentation is loading. Please wait.
1
CSE 214 – Computer Science II Binary Trees
Source:
2
Coding Exam 1 Next Friday, CS 2129 Linked Lists Only
2:15pm – 5:30 pm Linked Lists Only HWs 1 & 2 Sample exam has been posted to schedule page
3
Coding Exam 1 Review Session
Thursday evening Room/Time TBD I’ll post online/ class when I know
4
Trees A non-linear data structure
Components do not form a single simple sequence Provide dramatic efficiencies for accessing data & problem reduction
5
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
6
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
7
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
8
Full Binary Tree Every leaf has the same depth and every non-leaf has 2 children BA $ 85.18 AIG $ 46.11 MO $ 72.53 AA $ 35.72 AXP $ 45.11 MMM $ 79.95 T $ 37.88
9
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 MO $ 72.53 AA $ 35.72 AXP $ 45.11 MMM $ 79.95
10
Exercise Draw: a full binary tree with 15 nodes
a complete binary tree with 8 nodes
11
Full binary tree with 15 nodes
12
Complete binary tree with 8 nodes
13
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
14
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)
15
Why would we do this? To squeeze performance out of our program
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
16
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
17
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
18
Whatever we want to put there, even other data structures
public class GenericCompleteBinaryTree <E> { private BinaryTreeNode root = null; public GenericCompleteBinaryTree() {} 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; } What’s inside data? Whatever we want to put there, even other data structures
19
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
20
More common uses of trees
Decision trees AI trees Dialog trees Etc.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.