Binary Trees 15-121 Fall 2018 Margaret Reid-Miller
Trees Fall 2018 15-121 (Reid-Miller) 2
Binary Trees A binary tree is either empty or it contains a root node and left- and right-subtrees that are also binary trees. A binary tree is a nonlinear data structure. The top node of a tree is called the root. Any node in a binary tree has at most 2 children. Any node in a binary tree has exactly one parent node (except the root). Fall 2018 15-121 (Reid-Miller) 3
Tree Terminology A root B C internal leaf D E F G Fall 2018 15-121 (Reid-Miller) 4
left-child Tree Terminology A parent B C right-child siblings D E F G Fall 2018 15-121 (Reid-Miller) 5
Tree Terminology left-subtree A root right-subtree B C D E F G Fall 2018 15-121 (Reid-Miller) 6
Types of Binary Trees Expression Trees * + - / 5 7 3 6 2 (6 / 2 + 5) * (7-3) Fall 2018 15-121 (Reid-Miller) 7
Types of Binary Trees Huffman Trees coding A 45% B 30% C 20% D 5% 0 B 0 y 0 C z 1 x 1 A 1 D A 1 B 00 C 010 D 011 1001010100 = ABACAB Fall 2018 15-121 (Reid-Miller) 8
Types of Binary Trees Binary Search Trees 84 24 41 96 50 98 13 37 Fall 2018 15-121 (Reid-Miller) 9
More Terminology A perfect binary tree is a binary tree such that - all leaves have the same level, and - every non-leaf node has 2 children. A full tree is a binary tree such that - every non-leaf node has 2 children. A complete binary tree is a binary tree such that - every level of the tree has the maximum number of nodes possible except possibly the deepest level, and - at the deepest level, the nodes are as far left as possible. Fall 2018 15-121 (Reid-Miller) 10
Examples A A B C B C D E F G D E F PERFECT (and COMPLETE) COMPLETE (but not FULL) Fall 2018 15-121 (Reid-Miller) 11
Binary Trees & Recursion Consider two nodes in a tree, X and Y. X is an ancestor of Y if X is the parent of Y, or X is the ancestor of the parent of Y. It s RECURSIVE! X is a descendant of Y if X is the child of Y, or X is the descendant of the child of Y. Fall 2018 15-121 (Reid-Miller) 12
Binary Trees Levels A Level 0 B C Level 1 D E F G Level 2 Fall 2018 15-121 (Reid-Miller) 13
Binary Trees - Levels The level of a node Y is BASE CASE 0, if the Y is the root RECURSIVE CASE 1 + the level of the parent of Y, if Y is not the root Fall 2018 15-121 (Reid-Miller) 14
Binary Tree - Height The height of a binary tree T is the number of nodes in the longest path from the root node to a leaf node. BASE CASE 0, if T is empty RECURSIVE CASE max(height(left(t)), height(right(t))) + 1, if T is not empty Fall 2018 15-121 (Reid-Miller) 15
Binary Tree Traversals PREORDER INORDER POSTORDER A ABDECFG DBEAFCG DEBFGCA B C D E F G Fall 2018 15-121 (Reid-Miller) 16
Traversal Example B F D A G E C H preorder ABDFGCEHI inorder BFDGAEIHC postorder FGDBIHECA I Fall 2018 15-121 (Reid-Miller) 17
Traversals are Recursive Preorder traversal 1. Visit the root. 2. Perform a preorder traversal of the left subtree. 3. Perform a preorder traversal of the right subtree. Inorder traversal 1. Perform an inorder traversal of the left subtree. 2. Visit the root. 3. Perform an inorder traversal of the right subtree. Postorder traversal 1. Perform a postorder traversal of the left subtree. 2. Perform a postorder traversal of the right subtree. 3. Visit the root. Fall 2018 15-121 (Reid-Miller) 18
Traversals on Expression Trees What do you get when you perform a postorder traversal on an expression tree? + - * / 5 7 3 6 2 6 2 / 5 + 7 3 - * Fall 2018 15-121 (Reid-Miller) 19
Traversals on Binary Search Trees What do you get when you perform an INORDER traversal on a binary search tree? 84 41 96 24 50 98 13 37 13 24 37 41 50 84 96 98 Fall 2018 15-121 (Reid-Miller) 20
Implementing a binary tree Use an array to store the nodes. - mainly useful for complete binary trees (more on this soon) Use a variant of a linked list where each data element is stored in a node with links to the left and right children of that node Instead of a head reference, we will use a root reference to the root node of the tree. Fall 2018 15-121 (Reid-Miller) 21
Binary Tree Node (BTNode inner class) private class BTNode { private E data; private BTNode left; private BTNode right; } public BTNode(E element) { data = element; left = null; right = null; } data Fall 2018 15-121 (Reid-Miller) 22
BinaryTree class (Constructor) public class BinaryTree<E> { private BTNode root; public BinaryTree() { } root = null; Fall 2018 15-121 (Reid-Miller) 23
BinaryTree class (another constructor) public BinaryTree(E element, BinaryTree<E> lefttree, BinaryTree<E> righttree) { } root = new BTNode(element); if (lefttree!= null) root.left = lefttree.root; if (righttree!= null) root.right = righttree.root; Fall 2018 15-121 (Reid-Miller) 24
BinaryTree class short-circuit evaluation public BinaryTree<E> getleftsubtree() { } if (root!= null && root.left!= null) return new BinaryTree<E>(root.left); else return null; protected BinaryTree(BTNode rootref) { root = rootref; special constructor for this method only } same idea for right subtree Fall 2018 15-121 (Reid-Miller) 25
Preorder Traversal public String preorder() { return preorder(root); } private String preorder(node<e> node) { String result = ""; if (node!= null) { result += node.data + " "; result += preorder(node.left) + " "; result += preorder(node.right) + " "; } return result; } Fall 2018 15-121 (Reid-Miller) 26