Trees. 2 Tree Concepts Previous data organizations place data in linear order Some data organizations require categorizing data into groups, subgroups.

Slides:



Advertisements
Similar presentations
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Advertisements

Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
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.
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.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Transforming Infix to Postfix
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Fundamentals of Python: From First Programs Through Data Structures
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Marc Smith and Jim Ten Eyck
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.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Data Structures TREES.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Fundamentals of Programming II Introduction to Trees
A Binary Search Tree Implementation
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Section 8.1 Trees.
Trees What is a Tree? Tree terminology Why trees?
A Binary Search Tree Implementation
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees.
CSC 143 Java Trees.
B.Ramamurthy Chapter 9 CSE116A,B
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Trees

2 Tree Concepts Previous data organizations place data in linear order Some data organizations require categorizing data into groups, subgroups This is hierarchical classification Data items appear at various levels within the organization

3 Hierarchical Organization Example: File directories Computer files organized into folders.

4 Hierarchical Organization Example: A university's organization A university's administrative structure.

5 Hierarchical Organization Example: Family trees Carole’s children and grandchildren.

6 Hierarchical Organization Example: Family trees Jared’s parents and grandparents.

7 Tree Terminology A tree is A set of nodes (vertices) Connected by arcs (edges) The arcs indicate relationships among nodes Nodes arranged in levels Indicate the nodes’ hierarchy Top level is a single node called the root

8 Tree Terminology

9 Nodes at a given level are children of nodes of previous level Node with children is the parent node of those children Nodes with same parent are siblings Node with no children is a leaf node The only node with no parent is the root node All others have one parent each

10 Tree Terminology Empty trees? Some authors specify a general tree must have at least the root node This text will allow all trees to be empty A node is reached from the root by a path The length of the path is the number of arcs that compose it The height of a tree is the number of levels in the tree (alternatively, the number of arcs to a node) The subtree of a node is a tree rooted at a child of that node

11 Binary Trees Each node has at most two children Three binary trees.

12 Binary Trees A binary tree is either empty or has the following form Where T left and T right are binary trees

13 Binary Trees Every non-leaf in a full binary tree has exactly two children A complete binary tree is full to its next-to-last level Leaves on last level filled from left to right The number of nodes in a full binary tree is n=2 h -1 (recall: root is at level (height) 1)* The height of a binary tree with n nodes that is either complete or full is thus log 2 (n + 1) *Note: for root at depth d=0, n=2 d+1 -1

14 Binary Trees The number of nodes in a full binary tree as a function of the tree's height.

15 Traversals of a Tree Visiting a node Processing the data within a node This is the action performed on each node during traversal of a tree A traversal can pass through a node without visiting it at that moment For a binary tree Visit the root Visit all nodes in the root’s left subtree Visit all nodes in the root’s right subtree

16 Traversals of a Tree Preorder traversal: visit root before the subtrees The visitation order of a preorder traversal. This is an example of a depth-first traversal.

17 Traversals of a Tree Inorder traversal: visit root between visiting the subtrees The visitation order of an inorder traversal.

18 Traversals of a Tree Postorder traversal: visit root after visiting the subtrees The visitation order of a postorder traversal.

19 Traversals of a Tree Level-order traversal: begin at the root, visit nodes one level at a time The visitation order of a level-order traversal. This is an example of a breadth-first traversal.

20 Traversals of a General Tree A general tree has traversals that are in Level order Preorder Postorder Inorder traversal not well defined for a general tree

21 Traversals of a General Tree The visitation order of two traversals of a general tree: (a) preorder; (b) postorder.

22 Examples of Binary Trees Expression Trees Expression trees for four algebraic expressions.

23 Examples of Binary Trees Algorithm for evaluating an expression tree in postorder traversal Algorithm evaluate(expressionTree) if (expressionTree is empty) return 0 else { firstOperand = evaluate(left subtree of expressionTree) secondOperand = evaluate(right subtree of expressionTree) operator = the root of expressionTree return the result of the operation operator and its operands firstOperand and secondOperand }

24 Decision Trees A decision tree can be the basis of an expert system Helps users solve problems, make decisions A binary decision tree.

25 Decision Trees Possible interface for a binary decision tree. public interface DecisionTreeInterface extends BinaryTreeInterface { //get the data in the current node public Object getCurrentData(); //determine whether current node contains an answer (i.e., is a leaf) public boolean isAnswer(); //move current node to the left (right) child of the current node public void advanceToNo(); public void advanceToYes(); //set current node to the root of the tree public void reset(); }

26 Decision Trees An initial decision tree for a guessing game.

27 Decision Trees The decision tree for a guessing game after acquiring another fact.

28 Binary Search Trees A search tree organizes its data so that a search is more efficient Binary search tree Nodes contain Comparable objects A node's data is greater than the data in the node's left subtree A node's data is less than the data in the node's right subtree

29 Binary Search Trees A binary search tree of names.

30 Binary Search Trees Two binary search trees containing the same names

31 Binary Search Trees Algorithm for searching a binary search tree Algorithm bstSearch(binarySearchTree, desiredObject) // Searches a binary search tree for a given object. Returns true if found. if (binarySearchTree is empty) return false else if (desiredObject = = object in the root of binarySearchTree) return true else if (desiredObject < object in the root of binarySearchTree) return bstSearch(left subtree of binarySearchTree, desiredObject) else return bstSearch(right subtree of binarySearchTree, desiredObject)

32 Heaps A complete binary tree Nodes contain Comparable objects Each node contains no smaller (or no larger) than objects in its descendants Maxheap Object in a node is ≥ its descendant objects Minheap Object in a node is ≤ descendant objects

33 Heaps (a) A maxheap and (b) a minheap that contain the same values Consider how to use a heap to implement a priority queue…

34 Examples of General Trees A parse tree for the algebraic expression a * (b + c) expression>::= | + | - ::= | * | / ::= | ( ) ::=a | b | … | z | A | B | … | Z

35 Examples of General Trees A portion of a game tree for tic-tac-toe

36 Java Interfaces for Trees An interface that specifies operations common to all trees public interface TreeInterface { public Object getRootData(); public int getHeight(); public int getNumberOfNodes(); public boolean isEmpty(); public void clear(); }

37 Java Interfaces for Trees Interface for iterators for various traversals import java.util.Iterator; public interface TreeIteratorInterface { public Iterator getPreorderIterator(); public Iterator getPostorderIterator(); public Iterator getInorderIterator(); public Iterator getLevelOrderIterator(); }

38 Java Interfaces for Trees Interface for a class of binary trees public interface BinaryTreeInterface extends TreeInterface, TreeIteratorInterface{ //Set existing (perhaps just created) binary tree to a new one-node binary tree public void setTree(Object rootData); //Set existing (perhaps just created) binary tree to a new binary tree public void setTree(Object rootData, BinaryTreeInterface leftTree, BinaryTreeInterface rightTree); }

39 // represent each leaf as a one-node tree BinaryTreeInterface dTree = new BinaryTree(); dTree.setTree("D"); BinaryTreeInterface fTree = new BinaryTree(); fTree.setTree("F"); BinaryTreeInterface gTree = new BinaryTree(); gTree.setTree("G"); BinaryTreeInterface hTree = new BinaryTree(); hTree.setTree("H"); BinaryTreeInterface emptyTree = new BinaryTree(); // form larger subtrees BinaryTreeInterface eTree = new BinaryTree(); eTree.setTree("E", fTree, gTree); // subtree rooted at E BinaryTreeInterface bTree = new BinaryTree(); bTree.setTree("B", dTree, eTree); // subtree rooted at B BinaryTreeInterface cTree = new BinaryTree(); cTree.setTree("C", emptyTree, hTree); // subtree rooted at C BinaryTreeInterface aTree = new BinaryTree(); aTree.setTree("A", bTree, cTree); // desired tree rooted at A

40 // display root, height, number of nodes System.out.println("Root of tree is " + aTree.getRootData()); System.out.println("Height of tree is " + aTree.getHeight()); System.out.println("Tree has " + aTree.getNumberOfNodes() + " nodes"); // display nodes in preorder Iterator preorder = aTree.getPreorderIterator(); while (preorder.hasNext()) System.out.print(preorder.next() + " ");//note: should write a special print routine System.out.println(); See complete source code…