Download Trees - Object Oriented Programming and Data Structures - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!
Trees
Tree Overview
2 Tree : recursive data structure (similar to list) Each cell may have zero or more successors (children) Each cell has exactly one predecessor (parent) except the root , which has none All cells are reachable from root Binary tree : tree in which each cell can have at most two children: a left child and a right child 5 4 7 8 9 2 General tree 5 4 7 8 2 Binary tree 5 4 7 8 Not a tree 5 6 8 List-like tree
Class for Binary Tree Cells
class TreeCell { private T datum; private TreeCell left, right; public TreeCell(T x) { datum = x; } public TreeCell(T x, TreeCell lft, TreeCell rgt) { datum = x; left = lft; right = rgt; } more methods: getDatum, setDatum, getLeft, setLeft, getRight, setRight } 4 ... new TreeCell("hello") ... Points to left subtree Points to right subtree Constructor: datum x, no children Constructor: datum x and children
Binary versus general tree
- In a binary tree each node has exactly two pointers: to the left subtree and to the right one - Of course one or both could be null
- In a general tree a node can have any number of child nodes
- Very useful in some situations ...
- ... one of which will be our assignments! 5
Applications of Trees
Most languages (natural and computer) have a recursive, hierarchical structure This structure is implicit in ordinary textual representation Recursive structure can be made explicit by representing sentences in the language as trees: Abstract Syntax Trees (ASTs) ASTs are easier to optimize, generate code from, etc. than textual representation A parser converts textual representations to AST 7
Example
8
- Expression grammar:
- In textual representation
- Parentheses show hierarchical structure
- In tree representation
- Hierarchy is explicit in the structure of the tree - 34 -^34 (2 + 3) + (^2 ) ((2+3) + (5+7))
(^2 3 5 )
Text AST Representation
Searching in a Binary Tree
10 /** Return true iff x if the datum in a cell of tree node */ public static boolean treeSearch(Object x, TreeCell node) { if (node == null) return false; if (node.datum.equals(x)) return true; return treeSearch(x, node.left) || treeSearch(x, node.right); } 9 8 3 5 7 2 0 Analog of linear search in lists: given tree and an object, find out if object is stored in tree Easy to write recursively, harder to write iteratively
Binary Search Tree (BST)
11
- If the tree data are ordered – in any subtree,
- All left descendents of node come before node
- All right descendents of node come after node
- Search is MUCH faster 2 (^0 3 7 ) 5 8 /** Return true iff x if the datum in a cell of tree node. Precondition: node is a BST */ public static boolean treeSearch (Object x, TreeCell node) { if (node == null) return false; if (node.datum.equals(x)) return true; if (node.datum.compareTo(x) > 0) return treeSearch(x, node.left); else return treeSearch(x, node.right); }
What Can Go Wrong?
13
- A BST makes searches very fast,
unless …
- Nodes are inserted in alphabetical order
- In this case, we’re basically building a linked list (with some extra wasted space for the left fields that aren’t being used)
- BST works great if data arrives
in random order
jan feb mar apr may jun jul
Printing Contents of BST
14
Because of the
ordering rules for a
BST, it’s easy to print
the items in
alphabetical order
- Recursively print left subtree
- Print the node
- Recursively print right subtree /** *Print the BST in alpha. order. / public void show () { show(root); System.out.println(); } private static void show( TreeNode node) { if (node == null) return; show(node.lchild); System.out.print(node.datum + " "); show(node.rchild); }
Some Useful Methods
16 // Return true iff a node is a leaf public static boolean isLeaf(TreeCell node) { return (node != null) && (node.left == null) && (node.right == null); } //Return the height of a node using postorder traversal public static int height(TreeCell node) { if (node == null) return - 1; //empty tree if (isLeaf(node)) return 0; return 1 + Math.max(height(node.left), height(node.right)); } // Return number of nodes using postorder traversal public static int nNodes(TreeCell node) { if (node == null) return 0; return 1 + nNodes(node.left) + nNodes(node.right); }
Useful Facts about Binary Trees
- Max number of nodes at depth d: 2 d
- If height of tree is h
- min number of nodes in tree: h + 1
- Max number of nodes in tree:
- 2 0 + … + 2 h = 2 h+ - 1
- Complete binary tree
- All levels of tree down to a certain depth are completely filled 17 5 4 7 8 2 0 4 depth 0 1 2 5 2 4 Height 2, minimum number of nodes Height 2, maximum number of nodes
Things to Think About
- What if we want to delete data from a BST?
- A BST works great as long as it’s balanced - How can we keep it
balanced? This turns out
to be hard enough to
motivate us to create
other kinds of trees
19 jan feb mar apr jun may jul
Suffix Trees
20
- Given a string s, a suffix tree for s is a tree such that
- each edge has a unique label, which is a nonnull substring of s
- any two edges out of the same node have labels beginning with different characters
- the labels along any path from the root to a leaf concatenate together to give a suffix of s
- all suffixes are represented by some path
- the leaf of the path is labeled with the index of the first character of the suffix in s
- Suffix trees can be constructed in linear time