“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.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CS 261 – Winter 2010 Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
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.
Three Types of Depth-First Search Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms.
Binary Trees. Node structure Data A data field and two pointers, left and right.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Traversing a Binary Tree 10/23/ Traversing Binary Tree There are 3 ways of traversing a binary tree T having root R. 1. Preorder Traversing Steps:
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
CS261 Data Structures Trees Introduction and Applications.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Tree Traversal.
 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.
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.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
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.
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.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
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.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
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:
Trees. Family Tree Recursive Definition Tree - Definition A tree is a finite set of one or more nodes such that: 1.There is a specially designated node.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Data Structure and Algorithms
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Trees.
Abstract Data Structures
Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Trees.
Chapter 20: Binary Trees.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Binary Tree Traversal Methods
Trees.
Data Structures Using C++ 2E
Presentation transcript:

“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

Binary Trees Important subcategory of trees: –maximum outdegree is two –“left subtree” and “right subtree” a bc def

A Java Class for Binary Trees Operations? –create new node –access subtrees –access data –add new subtrees

A Java Class for Binary Trees a bc def

Tree data, lt, rt getData, left, right, addLeft, addRight Class Diagram

The Tree Class public class Tree { private T data; private Tree lt, // Ptr to left subtree rt; // Ptr to right subtree... } // class Tree Note: this really describes a single node

Constructors public Tree (T value, Tree left, Tree right) // Constructor: creates a new node with left // and right subtrees { lt = left; rt = right; data = value; } // Constructor public Tree (T value) // Constructor: creates a new node { this(value, null, null); } // Constructor Call first constructor

Other Methods Very simple public Tree left () // Return the left subtree of a tree { return lt; } public void addLeft (Tree left) // Add a left subtree to a node { if (lt != null) throw new …Exception(…); lt = left; } // addLeft public T getData () // Access the data value in this node { return data; }

Using Trees Many applications –decision trees –compilers We will consider a simple “knowledge base” (expert system) application –children’s guessing game

The Guessing Game Program asks the user questions, attempting to identify an animal The questions and answers (i.e. the program’s “knowledge”) are stored in a tree

The Knowledge Base Tree Does it live in water? Does it have webbed feet?Does it fly? birdDoes it bark? duckfish dogcat Note: This is a proper binary tree

The Algorithm Starting at the root: –if at a leaf node: give the answer –else ask the question if answer is positive, go to left subtree otherwise go to right subtree

The Java Code p pos = root; // Start at root w while (pos != null) { if (pos.left() != null) // Question { System.out.println(pos.getData()); if (answer()) pos = pos.left(); else pos = pos.right(); } else // Leaf: must be an answer { System.out.println("It's a " + pos.getData()); break; }

Initialising the Tree ree Tree p; // Temporary pointer oot root = new Tree ("Does it live in water?"); ot root.addLeft(new Tree ("...webbed feet?")); ot.a root.addRight(new Tree ("Does it fly?")); p = root.right(); p.addLeft(new Tree ("bird")); p.addRight(new Tree ("Does it bark?")); p = p.right(); p.addLeft(new Tree ("dog")); p.addRight(new Tree ("cat")); // Return to left subtree of root p = root.left(); p.addLeft(new Tree ("duck")); p.addRight(new Tree ("fish"));

Initialising the Tree: An Alternative Bottom up: Tree left, right, p; left = new Tree ("dog"); right = new Tree ("cat"); p = new Tree ("Does it bark?", left, right);... Uses the constructor rather than the addLeft and addRight methods

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): – 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 bc defg