CS2_29_Trees

CS2_29_Trees - CS2 Module 29 Category: CS Concepts Topic:...

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon
CS2 Module 29 Category: CS Concepts Topic: Trees Objectives Trees
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 29 CS Concepts Trees
Background image of page 2
Trees Hierarchical data structure Nodes with 0 to many children One special node designated root Acyclic Recursive definition
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Binary Trees Hierarchical data structure Nodes with 0 to 2 children One special node designated root Acyclic Recursive definition Used for BST
Background image of page 4
Recursive Definition? A binary tree is either A null pointer A node with two children each of which is a binary tree This recursive definition is why many tree algorithms are very simple when stated recursively
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Binary Search Trees Binary Tree Left subtree contains key values less than node Right subtree contains key value greater than node No duplicates* 50 40 30 37 10 95 63 99 42 *Normally handled specially
Background image of page 6
50 40 30 37 10 95 63 99 42 Binary Search Trees Performance Search O(log N) Insert O(log N) Traverse O(N) Performance O()'s assume tree is relatively balanced 50 40 30 10
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Tree Class The Binary Search Tree class will have some similarities to a Linked List class Need a BSTNode which is either A subclass of a datanode A subclass of Object In either event we will need A Comparable data reference (since it's going to be a BST) A left and a right reference to other tree nodes Appropriate node-ish methods (equals, compareTo, toString() And, of course, a debug main
Background image of page 8
A Tree Class A reference to a root node A constructor Traversal methods in order preorder postorder breadth first? insert contains nodeCount height delete Test main
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
class Tree { private BSTNode root; public Tree() { root = null; } public void inorder() { inorder(root); }
Background image of page 10
// class Tree (continued) private void inorder(BSTNode current) { if(current != null) { inorder(current.getLeft()); System.out.println(current); inorder(current.getRight()); } }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Outline of In-Order Traversal Three principle steps: Traverse Left Process current (root) node ( Current node ) Traverse Right The process can be anything print the node value insert the node in some other structure look for some value do some math with each node etc. Separate process from mechanics of traversal
Background image of page 12
Traverse the tree "In order": Visit current’s left sub-tree Visit current node & process data Visit current’s right sub-tree
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
// class Tree (continued) public static void main(String args[]) { TreeNode t2 = new TreeNode("T2"); TreeNode t4 = new TreeNode("T4"); TreeNode t9 = new TreeNode("T9"); TreeNode t3 = new TreeNode("T3", t2, t4); TreeNode t8 = new TreeNode("T8", null, t9); TreeNode t5 = new TreeNode("T5", t3, t8); /* This is strange */ Tree test = new Tree(); test.root = t5; /* !!!!!!! Done only to test !!! */ test.inorder(); } } // End of class (for testing) 5 2 8 3 9 4
Background image of page 14
The other classic traversals
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
// class Tree (continued) public void preorder() { preorder(root); } private void preorder(BSTNode current) { if(current != null) { System.out.println(current); preorder(current.getLeft()); preorder(current.getRight()); } }
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 118

CS2_29_Trees - CS2 Module 29 Category: CS Concepts Topic:...

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online