Data Structures and Algorithms for Information Processing Lecture 5: Trees Lecture 5: Trees
Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may be stored at each node. Each node is either a leaf, having no children, or an internal node, with one or two children In many ways, a tree is like the other structures you have seen: A tree consists of nodes, and each node can contain data of one sort or another. Lecture 5: Trees
Binary Trees Lecture 5: Trees
Some Terminology Root is the unique node with no parent Leaves or terminals are nodes with no children A subtree is a node together with all its descendents Level of a node is number of nodes on path from the node to root Lecture 5: Trees
Arithmetic Expressions A*(((B+C)*(D*E))+F) Example use of Trees Arithmetic Expressions A*(((B+C)*(D*E))+F) Each internal node labeled by an operator Each leaf labeled by a variable or numeric value Tree determines a unique value Lecture 5: Trees
Example Use of Trees Representing Chinese Characters Characters composed hierarchically into boxes Boxes can be oriented left-right, top-down, or outside-inside Each leaf labeled by one of 300-400 radicals Lecture 5: Trees
Binary vs. Binary-Unary Important distinction : Sometimes binary trees are defined to allow internal nodes with one or two children. There is a big difference... Lecture 5: Trees
Counting Trees The number of binary trees with internal nodes is There is no such formula if we allow unary internal nodes! Lecture 5: Trees
Exercise Write efficient Java functions int numBTrees(int n) int numBUTrees(int n) that return the number of binary trees and binary/unary trees, respectively. Lecture 5: Trees
Properties of Trees There is exactly one path connecting any two nodes Any two nodes have a least common ancestor A tree with N internal nodes has N+1 external nodes (easy to prove by induction) Lecture 5: Trees
The Best Trees A binary tree is full if internal nodes fill every level, except possibly the last. The height of a full binary tree with internal nodes is Lecture 5: Trees
Complete Binary Trees A complete tree is one having levels and leaves Complete trees have a simple implementation using arrays (How?) Lecture 5: Trees
Class for Binary Nodes public class BTNode { private Object data; private BTNode left; private BTNode right; ... Lecture 5: Trees
Class for Binary Nodes public BTNode(Object obj, BTNode l, BTNode r) { data = obj; left = l; right= r; } public boolean isLeaf() return (left == null) && (right == null); ... Lecture 5: Trees
Copying Trees public static BTNode treeCopy(BTNode t) { if (t == null) return null; else BTNode leftCopy = treeCopy(t.left); BTNode rightCopy = treeCopy(t.right); return new BTNode(t.data, leftCopy, rightCopy); } Lecture 5: Trees
Tree Traversals Preorder traversal P M L E S R A A T E E Lecture 5: Trees
Tree Traversals Inorder traversal P M L E S R A A T E E Lecture 5: Trees
Tree Traversals Postorder traversal P M L E S R A A T E E Lecture 5: Trees
Tree Traversals Level order traversal P M L E S R A A T E E Lecture 5: Trees
Preorder Traversal Process the root Process the nodes in the left subtree Process the nodes in the right subtree Lecture 5: Trees
Preorder Print public void preorderPrint() { System.out.println(data); if (left != null) left.preorderPrint(); if (right != null) right.preorderPrint(); } Lecture 5: Trees
Inorder Traversal Profess the nodes in the left subtree Process the root Process the nodes in the right subtree Lecture 5: Trees
Inorder Print public void preorderPrint() { if (left != null) left.preorderPrint(); System.out.println(data); if (right != null) right.preorderPrint(); } Lecture 5: Trees