Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Trees Based on slides by Alyssa Harding & Marty Stepp

Similar presentations


Presentation on theme: "Binary Trees Based on slides by Alyssa Harding & Marty Stepp"— Presentation transcript:

1 Binary Trees Based on slides by Alyssa Harding & Marty Stepp
CSE 143 Lecture 20 Binary Trees Based on slides by Alyssa Harding & Marty Stepp

2 Question Say you want to write a collection optimized for these tasks:
storing/accessing elements in sorted order adding/removing elements in order searching the collection for a given element What implementation would work well? An array? A sorted array? A linked list? index 1 2 3 value 7 11 24 49 front 7 11 24 49

3 Trees tree: A directed, acyclic structure of linked nodes.
directed : Has one-way links between nodes. acyclic : No path wraps back around to the same node twice. binary tree: Each node has at most two children. A tree can be defined as either: empty (null), or a root node that contains: data, a left subtree, and a right subtree. (The left and/or right subtree could be empty.) 7 6 3 2 1 5 4 root The definition of a tree shown here is recursive.

4 Trees in computer science
folders/files on a computer family genealogy; organizational charts AI: decision trees compilers: parse tree a = (b + c) * d; cell phone T9 d + * a = c b

5 Binary trees Actually shaped more like an upside down tree: 9 21 30 78
5 29 82 16

6 Definition The recursive definition lets us build any shape tree:

7 Terminology Root Leaf Branch 9 21 30 78 5 29 82 16
The node at the top of the tree Leaf A node with two empty subtrees Branch A node with one or more non-empty subtrees 9 21 30 78 5 29 82 16

8 Terminology Root Leaf Branch Root node 9 21 30 78 5 29 82 16
The node at the top of the tree Leaf A node with two empty subtrees Branch A node with one or more non-empty subtrees Root node 9 21 30 78 5 29 82 16 Branch nodes Leaf nodes 8

9 Terminology Looking at our 78 node: Child Parent Sibling 9 21 30 78 5
Any node our node refers to Parent The node that refers to our node Sibling Another child of the parent of our node 9 21 30 78 5 29 82 16

10 Terminology Looking at our 78 node: Child Parent Sibling sibling
Any node our node refers to Parent The node that refers to our node Sibling Another child of the parent of our node sibling parent 9 21 30 78 5 29 82 16 child 10

11 Terminology Ancestor Descendent 9 21 30 78 5 29 82 16
A parent of a parent of…our node 5 is an ancestor of 82 Descendent A child of a child of…our node 16 is a descendent of 30 9 21 30 78 5 29 82 16

12 Terminology Depth of a node Height of a tree 9 21 30 78 5 29 82 16
Length of the path from the root to the node Depth of the 29 node is 2 Height of a tree Length of longest path from the root to a leaf node Height of tree rooted at node 5 is 3 * Note: Sometimes you will hear height defined as the number of nodes in the longest path from the given node down to a leaf node. So the height of the tree rooted at 5 would be 4 instead of 3. 9 21 30 78 5 29 82 16

13 IntTreeNode So how do we make these trees? We need building blocks
For our LinkedIntList, we had ListNodes For our IntTree, we have IntTreeNodes left data right 42

14 A tree node for integers
A basic tree node object stores data and references to left/right Multiple nodes can be linked together into a larger tree Would it be useful to have a ternary tree? left data right 42 left data right 42 Would it be useful to have a trinary tree? An N-ary tree? Yes, for some applications. A T9 cell phone typing algorithm uses a 26-ary "prefix tree" or "Trie". Databases often use N-ary trees for indexing for speed. But we can do a lot with just two links. left data right 59 left data right 27 left data right 86

15 IntTreeNode class // An IntTreeNode object is one node in a binary tree of ints. public class IntTreeNode { public int data; // data stored at this node public IntTreeNode left; // reference to left subtree public IntTreeNode right; // reference to right subtree // Constructs a leaf node with the given data. public IntTreeNode(int data) { this(data, null, null); } // Constructs a branch node with the given data and links. public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right;

16 IntTree class // An IntTree object represents an entire binary tree of ints. public class IntTree { private IntTreeNode overallRoot; // null for an empty tree methods } Client code talks to the IntTree, not to the node objects inside it Methods of the IntTree create and manipulate the nodes, their data and links between them 7 6 3 2 1 5 4 overallRoot

17 IntTree We have code that will build a random tree of a given height
We have code that prints the structure of the tree We can use JGrasp to view the tree

18 Traversals Great, but what if we want to print out one line of output?
It’s not like a list where we know what order to print in We need to print the root node’s data We need to print the left subtree We need to print the right subtree We get different traversal order from choosing different orders to process the tree

19 Traversals Preorder: root, left, right 5 78 29 82 30 21 9 16
Inorder: left, root, right Postorder: left, right, root 9 21 30 78 5 29 82 16

20 Traversals Sailboat method: A visual way to do traversals 9 21 30 78 5
Trace a path around the nodes Write down the data of the node when you pass… On its left, for a preorder traversal Under it, for an inorder traversal On its right, for a postorder traversal 9 21 30 78 5 29 82 16

21 Example: printPreorder
Now we want a method to print the preorder traversal: public void printPreorder() { } We need to know which node we’re examining

22 Example: printPreorder
We make a private helper method to look at one specific node: public void printPreorder() { System.out.println("Preorder:"); printPreorder(overallRoot); System.out.println(); } private void printPreorder(IntTreeNode root) { The public method also starts the whole process by calling the private method with the overallRoot

23 Example: printPreorder
What is our base case? A null node is an empty tree! private void printPreorder(IntTreeNode root) { if ( root == null ) { // do nothing? } else { ... } Instead of having an empty if statement, invert the test!

24 Example: printPreorder
What is our recursive case? Since it’s preorder, we first want to print the root’s data: private void printPreorder(IntTreeNode root) { if ( root != null ) { System.out.print(root.data + “ “); }

25 Example: printPreorder
We also want to print a preorder traversal of the left subtree. If only we had a method... private void printPreorder(IntTreeNode root) { if ( root != null ) { System.out.print(root.data + “ “); printPreorder(root.left); }

26 Example: printPreorder
The last part is the right subtree: private void printPreorder(IntTreeNode root) { if ( root != null ) { System.out.print(root.data + “ “); printPreorder(root.left); printPreorder(root.right); }

27 Example: printPreorder
It’s amazingly short code, just like we’ve seen before with recursion When we call our recursive method, it prints the entire subtree The code for printInorder and printPostorder are very similar Remember, the difference was in the order in which we processed the root, the left subtree, and the right subtree

28 Example: printInorder
For an inorder traversal, we process the root in the middle: private void printInorder(IntTreeNode root) { if ( root != null ) { printPreorder(root.left); System.out.print(root.data + “ “); printPreorder(root.right); }

29 Example: printPostorder
For a postorder traversal, we process the root last: private void printPostorder(IntTreeNode root) { if ( root != null ) { printPreorder(root.left); printPreorder(root.right); System.out.print(root.data + “ “); }


Download ppt "Binary Trees Based on slides by Alyssa Harding & Marty Stepp"

Similar presentations


Ads by Google