Chapter 7. Trees & Binary Trees

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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:
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
(c) University of Washington20-1 CSC 143 Java Trees.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Binary Trees.
CSE 373 Data Structures Lecture 7
The Tree ADT.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
Data Structures Binary Trees 1.
Trees 8/7/2018 2:27 PM Binary Trees © 2010 Goodrich, Tamassia Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
CSE 373 Data Structures Lecture 7
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
8.2 Tree Traversals Chapter 8 - Trees.
Binary Trees.
Trees.
Binary Trees.
Week nine-ten: Trees Trees.
Trees Definitions Implementation Traversals K-ary Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CMSC 202 Trees.
Binary Search Trees.
Binary Trees, Binary Search Trees
Trees Chapter 10.
CE 221 Data Structures and Algorithms
Trees.
Binary Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
CSC 143 Java Trees.
CS210- Lecture 9 June 20, 2005 Announcements
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

Chapter 7. Trees & Binary Trees Lecture 14 Chapter 7. Trees & Binary Trees Set ADT Introduction to trees and binary trees ADS2 Lecture 14

Pictures of trees File System

Pictures of trees

ADS2 Lecture 14

Pictures of trees

Pictures of trees

Pictures of trees

Pictures of trees

Pictures of trees unrooted

Pictures of trees unrooted

unrooted Pictures of trees

unrooted Pictures of trees ADS2 Lecture 14 12

Pictures of trees ADS2 Lecture 14 13

Pictures of trees ADS2 Lecture 14 14

Pictures of trees ADS2 Lecture 14 15

Pictures of trees

Pictures of trees

Pictures of trees

Pictures of trees

Pictures of trees

Pictures of trees

Pictures of trees

Nonlinear data structure Natural way to organise data File system GUI General Trees Nonlinear data structure Natural way to organise data File system GUI Databases Websites Inheritance relation in Java classes Books (chapters, sections, subsections, subsubsections) “nonlinear” organisational structure Not just “before” “after” “above” “below” “part of” Relationships are typically Parent Children Ancestors Descendents Siblings ADS2 Lecture 14

A tree T is a set of nodes with a parent-child relationship Formal definition General Trees A tree T is a set of nodes with a parent-child relationship Each node has one parent, apart from the root (which has no parent) A tree T is either empty or consists of a node r, the root of T, and a (possibly empty) set of trees whose roots are the children of r An edge in T is a pair of nodes (u,v) where u is parent of v or v is parent of u ADS2 Lecture 14

i e c b R d j g f h General Trees a node an edge a path a a child a parent an ancestor a descendant siblings depth of a node height of a node height of a tree a leaf an internal node the root a subtree i e c b R d j g f h a X n p Z

i e c b R d j g f h General Trees n nodes n-1 edges a no cycles X n p Z n nodes n-1 edges no cycles unique path from a node to root

How might we implement a general tree? ADS2 Lecture 14

i e c b R d j g f h General Trees a p Z X n Therefore we might implement a tree as follows Tree<E> Node<E> root Node: <E> element Node<E> parent ArrayList<Node<E>> children Note: there could be order amongst the children ADS2 Lecture 14

i e c b R d j g f h Depth General Trees a Tree<E> X n p Z Tree<E> Node<E> root Node: <E> element Node<E> parent ArrayList<Node<E>> children Depth of a node is how far it is from the root. depth(Node<E> node) if (node.isRoot()) return 0; return 1 + depth(node.parent());

i e c b R d j g f h Height General Trees a Tree<E> X n p Z Tree<E> Node<E> root Node: <E> element Node<E> parent ArrayList<Node<E>> children Height of a node is If node is a leaf then 0 Otherwise 1 + the maximum of the height of its children height(Node<E> node) if (node.isLeaf()) return 0; int h = 0; for (Node<E> child : node.children()) h = Math.max(h,height(child)); return 1 + h;

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children

i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } Whatever “visit” means

a c b R X n d p Z i g f h j i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children a c b R X n d p Z i g f h j Visit the parent then visit its children preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } Whatever “visit” means

O(n) a c b R X n d p Z i g f h j i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children a c b R X n d p Z i g f h j Visit the parent then visit its children preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } O(n) Whatever “visit” means

ADS2 Lecture 14

i e c b R d j g f h Parenthetic Representation General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children (a (c (b ((R),(X),(n))),(d)),(p),(Z),(i (g ((f),(h))),(j))) Also called Caley Notation (I think) and is a preorder print

i e c b R d j g f h Postorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the children then visit the parent Typical use is we want to “assemble” parts An actual use: du in unix

i e c b R d j g f h Postorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the children then visit the parent postorder(Node<E> node) if (node != null) { for (Node<E> child : node.children()) postorder(child); visit(node); }

O(n) i e c b R d j g f h Postorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the children then visit the parent prostorder(Node<E> node) if (node != null) { for (Node<E> child : node.children()) postorder(child); visit(node); } O(n)

O(n) R X n b d c p Z f h g j i a i e c b R d j g f h Postorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children R X n b d c p Z f h g j i a Visit the children then visit the parent prostorder(Node<E> node) if (node != null) { for (Node<E> child : node.children()) postorder(child); visit(node); } O(n)

ADS2 Lecture 14

i e c b R d j g f h Other Traversals General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit depth 0 nodes Visit depth 1 nodes … Visit depth height nodes Also Known As (aka) Breadth First Search (bfs)

O(n) a c p Z i b d g j R X n f h i e c b R d j g f h Other Traversals General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children a c p Z i b d g j R X n f h Visit depth 0 nodes Visit depth 1 nodes … Visit depth height nodes O(n) Also Known As (aka) Breadth First Search (bfs) Note: preorder = dfs!

Binary Trees ADS2 Lecture 14

Binary Trees A binary tree is a tree in which each node has A reference to a left node A value A reference to a right node A binary tree is either empty or Contains a single node r (the root) whose left and right subtrees are binary trees The tree is accessed via a reference to the root. The nodes with both child references null are the leaves. recursive definition! ADS2 Lecture 14

nodes 0 intenal nodes 0 leaf nodes 0 edges 0 height ?

nodes 1 intenal nodes 0 leaf nodes 1 edges 0 height 0

nodes 3 intenal nodes 1 leaf nodes 2 edges 2 height 1

nodes 7 intenal nodes 3 leaf nodes 4 edges 6 height 2

nodes 15 intenal nodes 7 leaf nodes 8 edges 14 height 3

nodes n intenal nodes (nI) h ≤ nI ≤ 2h-1 leaf nodes (nE) 1 ≤ nE ≤ 2h edges (e) e = n-1 height (h) h+1 ≤ n ≤2h+1-1 height (h) log2(n+1)-1 ≤ h ≤ n-1 nodes 15 intenal nodes 7 leaf nodes 8 edges 14 height 3

And this is crucial height (h) log2(n+1)-1 ≤ h ≤ n-1

A Forest! t m w p u z v

This is also a binary tree 3 5 7 9 11

Balance A typical binary tree: This tree is quite well balanced. An extreme unbalanced tree might have no right pointers - would look more like a linked list. Generally tree-based algorithms work most efficiently on balanced trees. A binary tree in which no value occurs in more than one node is injective. We deal only with injective binary trees. ADS2 Lecture 14

Node of binary tree Binary tree node: A question: How would we represent the nodes of a tree (not binary)? We don’t know how many children each node has Binary tree node: public class BTNode<E>{ private E element; private BTNode<E> left; private BTNode<E> right; /**Creates a node with null references*/ public BTNode(){ this(null,null,null); } /** Creates node with the given element, L and R nodes*/ public BTNode(E e, BTNode<E> l,BTNode<E> r){ element = e; left=l; right=r; plus getters and setters ADS2 Lecture 14

For a binary tree of strings

For a binary tree of strings

For a binary tree of strings

For a binary tree of strings

For a binary tree of strings And what is that?

For a binary tree of strings

Definitions p - a node of a binary tree p.left - node of a binary tree - the left subtree p.right - node of a binary tree - the right subtree if p.left = q - p is the parent of q and q is the left child of p if p.right = q - p is the parent of q and q is the right child of p. p Left subtree of p Right subtree of p ADS2 Lecture 14

Traversals A traversal of a binary tree is a sequence of nodes of the tree. Special traversals: 1. Inorder traversal - defined recursively The inorder traversal of an empty tree is the empty sequence The inorder traversal of a non-empty tree is: the inorder traversal of the left subtree (a sequence) the root value (a sequence with one member) the inorder traversal of the right subtree (a sequence) ADS2 Lecture 14

Example (inorder traversal) Inorder traversal is a b c d e f g h i j ADS2 Lecture 14

Traversals contd. 2. Preorder traversal – defined recursively The preorder traversal of an empty tree is the empty sequence The preorder traversal of a non-empty tree is: the root value (a sequence with one member) the preorder traversal of the left subtree (a sequence) the preorder traversal of the right subtree (a sequence) Preorder traversal is e c b a d i g f h j ADS2 Lecture 14

Traversals contd. Postorder traversal defined recursively: The postorder traversal of an empty tree is the empty sequence The postorder traversal of a non-empty tree is: the postorder traversal of the left subtree (a sequence) the postorder traversal of the right subtree (a sequence) the root value (a sequence with one member) Postorder traversal is a b d c f h g j i e ADS2 Lecture 14

An Euler Walk

An ArrayList Representation of a Binary Tree Nodes have an integer position We put the nodes in an Array or ArrayList ADS2 Lecture 14

An ArrayList Representation of a Binary Tree 3 1 2 4 8 5 7 6 12 13 ADS2 Lecture 14

An ArrayList Representation of a Binary Tree 3 1 2 4 8 5 7 6 12 13 We have an ArrayList<Node<E>> T A Node<E> has an integer position attribute The root has position 1, i.e. T[1] is the root node T[i].position() == i ADS2 Lecture 14

An ArrayList Representation of a Binary Tree 3 1 2 4 8 5 7 6 12 13 We have an ArrayList<Node<E>> T A Node<E> has an integer position attribute The root has position 1, i.e. T[1] is the root node T[i].position() == i Left of T[i] is T[i*2] Right of T[i] is T[i*2 + 1] Parent of T[i] is T[i/2] ADS2 Lecture 14

Putting an expression into a tree An Expression Tree (or species tree) ADS2 Lecture 14

Putting an expression into a tree An Expression Tree (or species tree) We use two stacks S1 of Node<E> S2 of String ADS2 Lecture 14

Putting an expression into a tree An Expression Tree (or species tree) We use two stacks S1 of Node<E> S2 of String if we read “(“ S1.push(new Node(null,”!”,null)) if we read is in {+,-,*,/} S2.push(operator) if we read “)” Node r = S1.pop() Node l = S1.pop() Node node = S1.pop() String op = S1.pop() node.setLeft(l); node.setElement(op); node.setRight(r) S1.push(node) if we read String s and it is something else (a leaf) S1.push(new Node(null,s,null)) ADS2 Lecture 14

No Expense Spared ADS2 Lecture 14

.