Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.

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

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Trees most slides taken from Mike Scott, UT Austin.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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,
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.
Topic 17 Introduction to Trees
1 Introduction to trees Instructor: Dimitrios Kosmopoulos.
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.
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.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Data Structures TREES.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
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.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Topic 18 Binary Trees 樹高千丈﹐落葉巋根 "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
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.
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Trees Saurav Karmakar
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCE 210 Data Structures and Algorithms
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Week 6 - Wednesday CS221.
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Trees Tree nomenclature Implementation strategies Traversals
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Week 11 - Friday CS221.
Binary Search Trees -Monty Python and The Holy Grail
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
Trees 7/14/2009.
Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Trees Definitions Implementation Traversals K-ary Trees
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
CSC 143 Java Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Trees Lecture 10 CS2110 – Spring 2013.
Presentation transcript:

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Definitions A tree is an abstract data type one entry point, the root Each node is either a leaf or an internal node An internal node has 1 or more children, nodes that can be reached directly from that internal node. The internal node is said to be the parent of its child nodes root node internal nodes leaf nodes CS314 Binary Trees

Properties of Trees Only access point is the root All nodes, except the root, have one parent like the inheritance hierarchy in Java Traditionally trees drawn upside down root leaves CS314 Binary Trees

Properties of Trees and Nodes root siblings: two nodes that have the same parent edge: the link from one node to another path length: the number of edges that must be traversed to get from one node to another edge siblings path length from root to this node is 3 CS314 Binary Trees

More Properties of Trees depth: the path length from the root of the tree to this node height of a node: The maximum distance (path length) of any leaf from this node a leaf has a height of 0 the height of a tree is the height of the root of that tree descendants: any nodes that can be reached via 1 or more edges from this node ancestors: any nodes for which this node is a descendant CS314 Binary Trees

Tree Visualization A B D C F G H I J E K L M N O CS314 Binary Trees

Clicker Question 1 What is the depth of the node that contains M on the previous slide? 1 2 3 4 CS314 Binary Trees

Binary Trees There are many variations on trees but we will start with binary trees binary tree: each node has at most two children the possible children are usually referred to as the left child and the right child parent right child left child CS314 Binary Trees

Full Binary Tree full binary tree: a binary tree is which each node was exactly 2 or 0 children CS314 Binary Trees

Clicker Question 2 What is the maximum height of a full binary tree with 11 nodes? 1 3 5 7 Not possible to construct a full binary tree with 11 nodes. CS314 Binary Trees

Complete Binary Tree complete binary tree: a binary tree in which every level, except possibly the deepest is completely filled. At depth n, the height of the tree, all nodes are as far left as possible Where would the next node go to maintain a complete tree? CS314 Binary Trees

Clicker Question 3 What is the height of a complete binary tree that contains N nodes? O(1) O(logN) O(N1/2) O(N) O(NlogN) CS314 Binary Trees

Perfect Binary Tree perfect binary tree: a binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children. a perfect binary tree has the maximum number of nodes for a given height a perfect binary tree has (2(n+1) - 1) nodes where n is the height of the tree height = 0 -> 1 node height = 1 -> 3 nodes height = 2 -> 7 nodes height = 3 -> 15 nodes CS314 Binary Trees

A Binary Node class public class Bnode <E> { private E myData; private Bnode<E> myLeft; private Bnode<E> myRight; public BNode(); public BNode(E data, Bnode<E> left, Bnode<E> right) public E getData() public Bnode<E> getLeft() public Bnode<E> getRight() public void setData(E data) public void setLeft(Bnode<E> left) public void setRight(Bnode<E> right) } CS314 Binary Trees

Binary Tree Traversals Many algorithms require all nodes of a binary tree be visited and the contents of each node processed or examined. There are 4 traditional types of traversals preorder traversal: process the root, then process all sub trees (left to right) in order traversal: process the left sub tree, process the root, process the right sub tree post order traversal: process the left sub tree, process the right sub tree, then process the root level order traversal: starting from the root of a tree, process all nodes at the same depth from left to right, then proceed to the nodes at the next depth. CS314 Binary Trees

Results of Traversals To determine the results of a traversal on a given tree draw a path around the tree. start on the left side of the root and trace around the tree. The path should stay close to the tree. 12 pre order: process when pass down left side of node 12 49 13 5 42 in order: process when pass underneath node 13 49 5 12 42 post order: process when pass up right side of node 13 5 49 42 12 49 42 13 5 CS314 Binary Trees

Tree Traversals A D C F G H J K L CS314 Binary Trees

Attendance Question 2 What is a the result of a post order traversal of the tree on the previous slide? F C G A K H L D J F G C K L H J D A A C F G D H K L J A C D F G H J K L L K J H G F D C A CS314 Binary Trees

Implement Traversals Implement preorder, inorder, and post order traversal Big O time and space? Implement a level order traversal using a queue Implement a level order traversal without a queue target depth CS314 Binary Trees

Breadth First - Depth First from NIST - DADS breadth first search: Any search algorithm that considers neighbors of a vertex (node), that is, outgoing edges (links) of the vertex's predecessor in the search, before any outgoing edges of the vertex depth first search: Any search algorithm that considers outgoing edges (links o children) of a vertex (node) before any of the vertex's (node) siblings, that is, outgoing edges of the vertex's predecessor in the search. Extremes are searched first.

Breadth First A level order traversal of a tree could be used as a breadth first search Search all nodes in a level before going down to the next level CS314 Binary Trees

Breadth First Search of Tree G X Z O W Q P U K B Z L M R CS314 Binary Trees

Breadth First Search search level 0 first C Find Node with B A G X Z O Q P U K B Z L M R CS314 Binary Trees

Breadth First Search Find Node with B search level 1 next C A G X Z O Q P U K B Z L M R CS314 Binary Trees

Breadth First Search Find Node with B search level 2 next C A G X Z O Q P U K B Z L M R CS314 Binary Trees

Breadth First Search Find Node with B search level 3 next C A G X Z O Q P U K B Z L M R CS314 Binary Trees

Depth First Search C Find Node with B A G X Z O W Q P U K B Z L M R CS314 Binary Trees

BFS - DFS Breadth first search typically implemented with a Queue Depth first search typically implemented with recursion which technique do I use? depends on the problem CS314 Binary Trees