Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Advertisements

Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
CS 240: Data Structures Monday, July 30 th Binary Search Trees.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Data Structures Using C++1 Chapter 11 Binary Trees.
Binary Trees. 2 Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
Binary Trees. Node structure Data A data field and two pointers, left and right.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
“There is never enough time to do everything, but there is always enough time to do the most important thing.” – Brian Tracy Thought for the Day.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Discrete Mathematics Chapter 5 Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
Binary Trees.
Definition and Application of Binary Trees
Data Structure and Algorithms
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Lecture No.13 Data Structures Dr. Sohail Aslam
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Trees.
Binary Trees.
Binary Trees.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Trees.
Tree traversals BST properties Search Insertion
Data Structures Using C++ 2E
Presentation transcript:

Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu

Traversing Trees We often need to work through a tree “visiting” each node Several different ways that we can do this: in-order pre-order post-order breadth-order etc., etc.

Traversal Methods In-Order (LNR): Pre-Order (NLR): Post-Order (LRN): d b e a f c g Pre-Order (NLR): a b d e c f g Post-Order (LRN): d e b f g c a Breadth-Order: a b c d e f g a b c d e f g

Writing Traversal Methods We can use the existing methods in our Tree class Recursion is the easiest way! Challenge (for those who don’t believe me!): write a non-recursive traversal method.

Printing the Contents of a Tree public void LNRPrint (Tree<Character> root) // Recursive in-order traversal of tree // printing out the nodes' data { if (root != null) { LNRPrint(root.left()); System.out.println(root.getData()); LNRPrint(root.right()); } } // LNRPrint Recursive calls

Iterators Tree traversals are common operations Useful to provide methods in tree classes But, we may want to do different things print add to total compare with a “search” value etc.

Solution: Provide methods that supply iterators objects have methods to move through the data structure, and access the data values See how it’s done later...

Ordered Binary Trees Binary Search Trees Trees so far: No requirement for ordering nodes Binary Search Tree Values less than the node’s value: in left subtree Otherwise in right subtree

Example LNR (in-order) traversal visits nodes in ascending order b t a k LNR (in-order) traversal visits nodes in ascending order a b k m m t

Implementation To prevent clients destroying the ordering we need a different design: remove addLeft and addRight methods hide the structure (inner “node” class) Class diagram: BinarySearchTree root insert, remove, contains, getLNRIterator, getNLRIterator, getLRNIterator

The BinarySearchTree Class public class BinarySearchTree <T extends Comparable> { private class BSTreeNode { public T data; public BSTreeNode lt, // Left subtree rt, // Right subtree parent; // Parent node } // inner class BSTreeNode private BSTreeNode root; ... Three pointers

Bounded Generic Type Parameters public class X<T extends Comparable> ... T can be any type that extends (implements) the Comparable interface Restricts the type of objects that can be used Here, Comparable is required for the ordering of the nodes

The Comparable Interface Standard Java interface Specifies the class must provide a method called compareTo: public int compareTo (Object o); /* Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. */

The Comparable Interface Many classes implement this interface wrapper classes, String, Date, etc. Allows us to insert items into the correct position in a binary search tree Is (comparable) object x < object y? if (x.compareTo(y) < 0)...

The insert method public void insert (T newValue) // Add a new node to the tree { if (root == null) root = new BSTreeNode(newValue); else insert(newValue, root); } // insert Overloaded, private method

The Private insert Method private void insert (T value, BSTreeNode root) { assert root != null; if (root.data.compareTo(value) > 0) // Add to left subtree if (root.lt != null) insert(value, root.lt); else root.lt = new BSTreeNode(value, root); else // Add to right subtree if (root.rt != null) insert(value, root.rt); root.rt = new BSTreeNode(value, root); } // insert Recursive calls