8.2 Tree Traversals Chapter 8 - Trees.

Slides:



Advertisements
Similar presentations
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
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 Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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 Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Tree Data Structures.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
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.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
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.
Data Structure and Algorithms
CSCE 210 Data Structures and Algorithms
AA Trees.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Binary Tree and General Tree
TREES General trees Binary trees Binary search trees AVL trees
Binary Trees.
Chapter 7. Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Trees Chapter 6.
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
B.Ramamurthy Chapter 9 CSE116A,B
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
8.1 Tree Terminology and Applications
8.2 Tree Traversals Chapter 8 - Trees.
Introduction to Trees Chapter 6 Objectives
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

8.2 Tree Traversals Chapter 8 - Trees

Attendance Quiz #26 Stacks

Tip #28: Iterator end(); Trees How does STL guarantee that end will always point to one past the last element in container? The STL does not guarantee this behavior! What's one past the last element in a set or a map? The "end is one past the last element" thing is just a logical concept. Exactly how this behavior is achieved depends on the implementation. Don't take it too literally. end() might be implemented as you intuitively expect or not! What do we know about the STL iterators? begin() returns an iterator referring to the first element in the container. If the container is empty, then begin() == end(). end() returns an iterator which is the "past-the-end" value for the container and is NOT a member of the container. BST<int>::Iterator iter = bst.begin(); BST<int>::Iterator end_iter = bst.end(); if (iter == end_iter) cout << " Empty"; for (; iter != end_iter; iter++) cout << " " << *iter;

Binary Trees

Recursively Searching a Binary Tree Trees Binary search trees All elements in the left subtree precede those in the right subtree A formal definition: A set of nodes T is a binary search tree if either of the following is true: T is empty If T is not empty, its root node has two subtrees, TL and TR, such that TL and TR are binary search trees and the value in the root node of T is greater than all values in TL and is less than all values in TR dog cat wolf canine

Recursively Searching a Binary Tree Trees When searching a BST, a typical probe eliminates half the elements in the tree, so if the tree is relatively balanced, searching can be O(log n). In the worst case, searching is O(n). The elements in a binary search tree never need to be sorted because they always satisfy the required order relationships. When new elements are inserted (or existing elements are removed) properly, the binary search tree maintains its order. In contrast, a sorted array must be expanded whenever new elements are added, and compacted whenever elements are removed—expanding and contracting are both O(n).

Fullness and Completeness Trees A full binary tree is a binary tree where all internal nodes have exactly 2 children. Note: the number of leaf nodes is one more than the number of internal nodes. 7 10 1 12 9 3 5 2 11 6 4 13

Fullness and Completeness Trees 7 10 3 11 9 5 1 6 4 8 2 Note: not a full binary tree as node 9 only has 1 child. is filled (has a value) up to depth (h – 1) and, at depth h, any unfilled nodes are on the right. A complete binary tree of height h Also, with a complete binary tree, All nodes at depth (h – 2) and above have two children. When a node at depth (h – 1) has children, all nodes to the left of it have two children. If a node at depth (h – 1) has one child, it is a left child.

General Trees Trees We do not explore general trees in this chapter, but nodes of a general tree can have any number of subtrees.

General Trees A general tree can be represented using a binary tree. The left branch of a node is the oldest child, and each right branch is connected to the next younger sibling (if any). Children Siblings Children Siblings Children Siblings Children Siblings Children Siblings Siblings

8.2, pgs. 454-455 8.2 Tree Traversals Visualizing Tree Traversals Traversals of Binary Search Trees and Expression Trees 8.2, pgs. 454-455

Tree Traversals Trees Often we want to determine the nodes of a tree and their relationship We can do this by walking through the tree in a prescribed order and visiting the nodes as they are encountered This process is called tree traversal Three common kinds of tree traversal Pre-order In-order Post-order Level-order

Tree Traversals Preorder: visit root node, traverse TL , traverse TR Trees Preorder: visit root node, traverse TL , traverse TR Inorder: traverse TL , visit root node, traverse TR Postorder: traverse TL , traverse TR , visit root node

Visualizing Tree Traversals Trees You can visualize a tree traversal by imagining a mouse that walks along the edge of the tree. If the mouse always keeps the tree to the left, it will trace a route known as the Euler tour. The Euler tour is the path traced in blue in the figure on the right.

Visualizing Tree Traversals Trees If the mouse follows an Euler tour route (blue path) and visits each node before traversing its subtrees (shown by the downward pointing arrows), then we get a pre- order traversal. The sequence in this example is: a b d g e h c f i j

Visualizing Tree Traversals Trees If we record a node as the mouse returns after traversing its left subtree (shown by horizontal black arrows in the figure) we get an in-order traversal. The sequence in this example is: d g b h e a i f j c

Visualizing Tree Traversals Trees If we record each node as the mouse last encounters it, we get a post-order traversal (shown by the upward pointing arrows). The sequence in this example is: g d h e b i j f c a

Visualizing Tree Traversals Trees If we record each node by height within the tree, we get a level-order traversal. The sequence in this example is: a b c d e f g h i j

Quiz A A B C D E F G H I B D A G E C H F I A B D C E G F H I Trees Level-order: In-order: Pre-order: Post-order: A B C D E F G H I B D A G E C H F I A B D C E G F H I D B G E H I F C A

Quiz B A B C D E F G H I J H D I B E A F C G J A B D H I E C F G J Trees Level-order: In-order: Pre-order: Post-order: A B C D E F G H I J H D I B E A F C G J A B D H I E C F G J H I D E B F J G C A

Traversal of Binary Search Tree Trees An in-order traversal of a binary search tree results in the nodes being visited in sequence by increasing data value. dog cat wolf canine The sequence in this example is: canine, cat, dog, wolf

Traversal of Expression Tree Trees An in-order traversal of an expression tree results in the sequence x + y * a + b / c A post-order traversal of an expression tree results in the sequence x y + a b + c / * The postfix or reverse polish form. Operators follow operands. A pre-order traversal of an expression tree results in the sequence * + x y / + a b c This is the prefix form of the expression. Operators precede operands. * / + c y x b a