Chapter 10 Trees – part B.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program,
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Chapter 19 Trees. Chapter Scope Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals Expression trees.
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.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 9: Trees Java Software Structures: Designing and Using Data.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 19 Trees. Chapter Scope Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals Expression trees.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 10-A Trees Modified
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.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
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.
1 Trees and Binary Search Trees Using binary trees Definition of a binary search tree Implementing binary search trees –Add Element –Remove Element Using.
Chapter 20 Binary Search Trees
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
COS 312 DAY 25 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 7 Not corrected – 3 MIA Quiz 3 May 4 – Chaps – Available 9 AM - 5 PM – 20 M/C 10.
Data Structures Using Java1 Chapter 10 Binary Trees.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Trees and Binary Search Trees Using binary trees Definition of a binary search tree Implementing binary search trees –Add Element –Remove Element Using.
Trees Chapter 19, 20 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
The Tree ADT.
CS 201 Data Structures and Algorithms
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
Trees and Binary Search Trees
Trees Another Abstract Data Type (ADT)
Trees Tree nomenclature Implementation strategies Traversals
Trees What is a Tree? Tree terminology Why trees?
Trees.
Chapter 21 Heaps and Priority Queues
Trees.
Recall: Nodes and Linked Lists
Data Structures Using Java
Java Software Structures: John Lewis & Joseph Chase
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Trees Another Abstract Data Type (ADT)
Trees.
Trees Another Abstract Data Type (ADT)
Trees.
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Trees.
Chapter 20 Binary Search Trees
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Chapter 10 Trees – part B

Chapter Scope - Part B Linked Binary Tree Implementation Expression trees Decision Trees Non Binary trees

A Binary Tree ADT

A Binary Tree ADT

Implementing Binary Trees with Links Now let's explore an implementation of a binary tree using links The LinkedBinaryTree class holds a reference to the root node The BinaryTreeNode class represents each node, with links to a possible left and/or right child

/. BinaryTreeNode represents a node in a binary tree with a /** BinaryTreeNode represents a node in a binary tree with a * left and right child. */ public class BinaryTreeNode<T> { protected T element; protected BinaryTreeNode<T> left, right; /** Creates a new tree node with the specified data. * * @param obj the element that will become a part of the * new tree node public BinaryTreeNode(T obj) element = obj; left = null; right = null; }

/. Creates a new tree node with the specified data /** Creates a new tree node with the specified data. * * @param obj = data element of the new tree node * @param left = tree that will be the left subtree of this node * @param right = tree that will be the right subtree of this node */ public BinaryTreeNode(T obj, LinkedBinaryTree<T> left, LinkedBinaryTree<T> right) { element = obj; // Set data element if (left == null) this.left = null; else this.left = left.getRootNode(); // Set left subtree if (right == null) this.right = null; this.right = right.getRootNode(); // Set right subtree }

/. Returns the number of non-null children of this node /** * Returns the number of non-null children of this node. * @return the number of non-null children of this node */ public int numChildren() { int children = 0; if (left != null) children = 1 + left.numChildren(); if (right != null) children = children + 1 + right.numChildren(); return children; }

/. Return the element at this node /** Return the element at this node. * @return the element stored at this node */ public T getElement() { return element; } /** Return the right child of this node. * * @return the right child of this node */ public BinaryTreeNode<T> getRight() return right; /** Sets the right child of this node. * @param node the right child of this node */ public void setRight(BinaryTreeNode<T> node) right = node;

/. Return the left child of this node /** * Return the left child of this node. * * @return the left child of the node */ public BinaryTreeNode<T> getLeft() { return left; } * Sets the left child of this node. * * @param node the left child of this node public void setLeft(BinaryTreeNode<T> node) left = node;

/. LinkedBinaryTree implements the BinaryTreeADT interface /** LinkedBinaryTree implements the BinaryTreeADT interface */ public class LinkedBinaryTree<T> implements BinaryTreeADT<T>, Iterable<T> { protected BinaryTreeNode<T> root; protected int modCount; /** * Creates an empty binary tree. public LinkedBinaryTree() root = null; modCount = 0; }

/. Creates a binary tree with the specified element as its root /** Creates a binary tree with the specified element as its root. * * @param element = element to be in the root of the binary tree */ public LinkedBinaryTree(T element) { root = new BinaryTreeNode<T>(element); modCount = 0; } /** Creates a binary tree with the specified element as its root and * the given trees as its left child and right child * * @param left the left subtree of this tree * @param right the right subtree of this tree public LinkedBinaryTree(T element, LinkedBinaryTree<T> left, LinkedBinaryTree<T> right) root.setLeft(left.root); root.setRight(right.root); nodCount = 0;

/. Returns a reference to the specified target element if it is /** * Returns a reference to the specified target element if it is * found in this binary tree. Throws a ElementNotFoundException if * the specified target element is not found in the binary tree. * @param targetElement the element being sought in this tree * @return a reference to the specified target * @throws ElementNotFoundException if the element is not in tree */ public T find(T targetElement) throws ElementNotFoundException { BinaryTreeNode<T> current = findNode(targetElement, root); if (current == null) throw new ElementNotFoundException("LinkedBinaryTree"); return (current.getElement()); }

/. Returns a reference to the specified target element if it /** Returns a reference to the specified target element if it * is found in this binary tree. * @param targetElement the element being sought in this tree * @param next the element to begin searching from */ private BinaryTreeNode<T> findNode(T targetElement, BinaryTreeNode<T> next) { if (next == null) return null; if (next.getElement().equals(targetElement)) return next; BinaryTreeNode<T> temp = findNode(targetElement, next.getLeft()); if (temp == null) temp = findNode(targetElement, next.getRight()); return temp; }

/. Performs an inorder traversal on this binary tree by calling an /** Performs an inorder traversal on this binary tree by calling an * overloaded, recursive inorder method that starts with he root. * @return an in order iterator over this binary tree */ public Iterator<T> iteratorInOrder() { ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>(); inOrder(root, tempList); return new TreeIterator(tempList.iterator()); } /** Performs a recursive inorder traversal. * @param node the node to be used as the root for this traversal * @param tempList the temporary list for use in this traversal */ protected void inOrder(BinaryTreeNode<T> node, ArrayUnorderedList<T> tempList) if (node != null) inOrder(node.getLeft(), tempList); tempList.addToRear(node.getElement()); inOrder(node.getRight(), tempList);

Expression Trees An expression tree is a tree that shows the relationships among operators and operands in an expression An expression tree is evaluated from the bottom up

Expression Tree Evaluation Let's look at an example that creates an expression tree from a postfix expression and then evaluates it This is a modification of an earlier solution It uses a stack of expression trees to build the complete expression tree

xxx

Decision Trees A decision tree is a tree whose nodes represent decision points, and whose children represent the options available The leaves of a decision tree represent the possible conclusions that might be drawn A simple decision tree, with yes/no questions, can be modeled by a binary tree Decision trees are useful in diagnostic situations (medical, car repair, etc.)

Decision Trees A simplified decision tree for diagnosing back pain:

General trees Games Organization charts Parts Assembly Taxonomy Genealogy File directories …

General trees k-ary trees -- limit on number of children How to store? Wasted space? No limit on the number of children Special case of graphs

Mapping between general tree to binary tree Correspondence between binary trees and forests