Trees Data Structures in Cjava 1207017927754165 4
Trees Data Structures in Cjava 1207017927754165 4
Binary Trees
9-1
Outline
Why Trees? Trees Tree Terminology Binary Trees One Recursive Tree Algorithm
9-2
arrays
use subscripts to immediately access elements fast access, but in the old days would consume more memory that you would like it to (not true anymore)
linked structures
each node refers to the next in the collection. To get to one you often traverse sequentially maybe slower, but maybe manages memory better
9-3
One implementation of a binary tree has nodes with a left and right link field
nodes root 1 2 3
edges
9-4
a directed edge connects a parent to its children one node is distinguished as the root every node (except the root) is connected by an edge from exactly one other node A unique path traverses from the root to each node
9-5
Tree properties
General Trees
Trees store data in a hierarchical manner
A B E
C F
Trees have layers of nodes, where some are higher, others are lower.
Root node is A A's children are B, C, and D E, F, and D are leaves Length of path from A to E is 2 edges
9-6
9-7
Applications of trees
File Systems
Hierarchical files systems include Unix and DOS In DOS, each \ represents an edge (In Unix, it's /) Each directory is a file with a list of all its children
implement data vase management systems compilers: expression tree, symbol tree
A tree
Binary Trees
N-ary tree has n children max from each node A binary tree is a tree where all nodes have zero, one or two children we'll study these binary trees only Each node is a leaf, has a right child, has a left child, or has both a left and right child
A B D E C F
9-9
an empty tree consists of a node, called a root, and zero, one, or two children (left and right), each of which are themselves binary trees
This recursive definition uses the term "empty tree" as the base case Every non-empty node has two children, either of which may be empty
The expression:
+ 3 * 1 ((3+(7*2))-1)
Each parenthesized expression becomes a tree. Each operand is a leaf, each operator is 9-11 an internal node
Take any two leaves Apply the parent's operator to them Replace that operator with the value of the subexpression.
* * 18
+
2 4
9-12
'e'
't' 'a'
9-13
9-14
A Third Constructor
BinaryTreeNode(Object theData) { // Used most often data = theData; left = null; right = null; } BinaryTreeNode(Object theData, BinaryTreeNode leftLink, BinaryTreeNode rightLink) { data = theData; left = leftLink; right = rightLink; } } // end class BinaryTreeNode
9-15
class BinaryTreeNode
Each BinaryTreeNode object has
a reference to an object object so we can store anything a link to the left subtree which could be an empty tree a link to the right subtree which could be an empty tree
two set some data fields to null (left and right)
3 Constructors
Like LinkNode, methods in the enclosing class can reference private instance variables of the inner class
9-16
We do not yet have a nice way to insert new nodes This demonstrates linked BinaryTreeNode objects
BinaryTreeNode root = new BinaryTreeNode("1"); root.left = new BinaryTreeNode("2"); root.right = new BinaryTreeNode("3");
What is the base case? (think simplest possibility) With trees, the recursive case often makes a recursive call on the left subtree and another on the right subtree
}
9-18
Active Learning
1) Draw this tree
BinaryTreeNode aTree = new BinaryTreeNode("1"); aTree.left = new BinaryTreeNode("2"); aTree.right = new BinaryTreeNode("3"); aTree.right.right = new BinaryTreeNode("4"); aTree.right.left = new BinaryTreeNode("5"); aTree.left.right = new BinaryTreeNode("6"); aTree.left.left = new BinaryTreeNode("7"); aTree.left.left.left = new BinaryTreeNode("8"); System.out.println(size(aTree));