CS2_29_Trees

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

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

CS2 Module 29 Category: CS Concepts Topic: Trees Objectives Trees

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

View Full Document
CS 2 Introduction to Object Oriented Programming Module 29 CS Concepts Trees
Trees Hierarchical data structure Nodes with 0 to many children One special node designated root Acyclic Recursive definition

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

View Full Document
Binary Trees Hierarchical data structure Nodes with 0 to 2 children One special node designated root Acyclic Recursive definition Used for BST
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
class Tree { private BSTNode root; public Tree() { root = null; } public void inorder() { inorder(root); }
// class Tree (continued) private void inorder(BSTNode current) { if(current != null) { inorder(current.getLeft()); System.out.println(current); inorder(current.getRight()); } }

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

View Full Document
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
Traverse the tree "In order": Visit current’s left sub-tree Visit current node & process data Visit current’s right sub-tree

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

View Full Document
// 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
The other classic traversals

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

View Full Document
// 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()); } }
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online