Data Structures CSCI 132, Spring 2014 Lecture 35 Binary Trees

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

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
1 Data Structures CSCI 132, Spring 2014 Lecture 37 Binary Search Trees II.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
Void write_method() /* Post: A short string identifying the abstract data type used for the structure is written. */ Project 2 -- Analysis.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Data Structures Chapter 10 Binary Trees Andreas Savva.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Kruse/Ryba ch101 Object Oriented Data Structures Binary Trees Binary Search Trees Height Balance:AVL Trees Splay Trees.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
A Binary Search Tree Binary Search Trees.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
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:
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Traversing Trees. Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Binary Trees.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
Data Structure and Algorithms
Binary Trees.
Paul Tymann and Andrew Watkins
Section 8.1 Trees.
Binary Tree Applications
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Chapter 20: Binary Trees.
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
Binary Trees.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Binary Tree Traversal Methods
Trees.
Binary Trees.
Binary Trees.
Binary Tree Traversal Methods
Binary Tree Traversals
Binary Trees.
Chapter 20: Binary Trees.
Data Structures Using C++ 2E
A Binary Tree is a tree in which each node has at most 2 children
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Data Structures CSCI 132, Spring 2014 Lecture 35 Binary Trees

Binary Trees Definition: A binary tree is either empty or it consists of a root together with two binary trees called the left subtree and the right subtree. Examples: Two node trees Three node trees

Traversing Binary Trees To traverse a tree, we move through the tree and visit each node one at a time. This can be done in various orders: preorder: VLR--Visit a node, then visit the left subtree, then visit the right subtree. inorder: LVR--Visit the left subtree, then visit the node, then visit the right subtree. postorder: LRV--Visit the left subtree, then visit the right subtree, then visit the node.

Tree traversal examples preorder: 1 2 3 inorder: 2 1 3 postorder: 2 3 1 1 2 3 1 We will work this one out in class. 2 3 4 5

Expression trees

Comparison trees for binary search Binary search of the following list: Amy Ann Dot Eva Guy Jan Jim Jon Kay Kim Ron Roy Tim Tom Note that inorder traversal gives the list in alphabetical order.

Binary_node struct template <class Entry> struct Binary_node { // data members: Entry data; Binary_node<Entry> *left; Binary_node<Entry> *right; // constructors: Binary_node( ); Binary_node(const Entry &x); };

Binary_tree class template <class Entry> class Binary_tree { public: // Add methods here. protected: // Add auxiliary function prototypes here. Binary_node<Entry> *root; };

Binary search tree with pointers

Implementation of constructor and empty( ) functions template <class Entry> Binary_tree<Entry> :: Binary_tree( ) { root = NULL; } bool Binary_tree<Entry> :: empty( ) const return root == NULL;

In order traversal template <class Entry> void Binary_tree<Entry> :: inorder(void (*visit)(Entry &)) { recursive_inorder(root, visit); } void Binary_tree<Entry> :: recursive_inorder(Binary_node<Entry> *sub_root, void (*visit)(Entry &)) //We'll work this out in class

Binary_tree specification template <class Entry> class Binary_tree { public: Binary_tree( ); bool empty( ) const; void preorder(void (*visit)(Entry &)); void inorder(void (*visit)(Entry &)); void postorder(void (*visit)(Entry &)); int size( ) const; void clear( ); int height( ) const; void insert(const Entry &); Binary_tree (const Binary_tree<Entry> &original); Binary_tree & operator = (const Binary_tree<Entry> &original); ~Binary_tree( ); protected: // Add auxiliary function prototypes here. Binary_node<Entry> *root; };