An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:

Similar documents
11 TREES DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

void insert( Type const & ) void push_front( Type const & )

Figure 1. A breadth-first traversal.

LECTURE 11 TREE TRAVERSALS

Trees. Carlos Moreno uwaterloo.ca EIT

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

LECTURE 13 BINARY TREES

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

LECTURE 18 AVL TREES

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /08:30-9:50 RCH 105, RCH 110

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

Chapter 5. Binary Trees

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Chapter 20: Binary Trees

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /5:30-7:00

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

CS302 - Data Structures using C++

Traversing Trees with Iterators

Traversing Trees with Iterators

a graph is a data structure made up of nodes in graph theory the links are normally called edges

CS24 Week 8 Lecture 1

Trees. CSE 373 Data Structures

Trees, Binary Trees, and Binary Search Trees

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

Heaps & Priority Queues. (Walls & Mirrors - Remainder of Chapter 11)

Trees. (Trees) Data Structures and Programming Spring / 28

Data Structures and Algorithms

A set of nodes (or vertices) with a single starting point

Binary Tree Implementation

CMSC 341. Binary Search Trees CMSC 341 BST

Assignment 3 Suggested solutions

ADT: Design & Implementation

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III

Priority Queues (Heaps)

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

CSCE 2014 Final Exam Spring Version A

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Principles of Computer Science

We have the pointers reference the next node in an inorder traversal; called threads

ECE 2400 Computer Systems Programming, Fall 2017 Prelim 2 Prep

Another solution: B-trees

Tree traversals and binary trees

CSC212 Data Structure - Section FG

TREES. Trees - Introduction

What is a List? elements, with a linear relationship. first) has a unique predecessor, and. unique successor.

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:30-7:00

BINARY SEARCH TREES (CONTD) C++ TEMPLATES. Problem Solving with Computers-II

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

B-Trees. Based on materials by D. Frey and T. Anastasio

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

CS350: Data Structures Red-Black Trees

CSCI-1200 Data Structures Fall 2014 Lecture 18 Trees, Part III

Topic Binary Trees (Non-Linear Data Structures)

Expression Trees and the Heap

Binary Tree Implementation

Pushing at the back 28/11/2018. Problem. Our linked list class

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Chapter 17: Linked Lists

CMSC 341 Lecture 10 Binary Search Trees

Algorithms. Deleting from Red-Black Trees B-Trees

ECE 250 Algorithms and Data Structures

Binary Trees. Height 1

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

CS302 - Data Structures using C++

(2,4) Trees. 2/22/2006 (2,4) Trees 1

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

CSCI Trees. Mark Redekopp David Kempe

ECE 242. Data Structures

CS302 - Data Structures using C++

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Topic 14. The BinaryTree ADT

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

CS302 - Data Structures using C++

Binary Trees, Binary Search Trees

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

ECE 2400 Computer Systems Programming, Fall 2017 Prelim 2 Prep

Data Structures in Java

Binary Trees. Examples:

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Priority Queues and Huffman Trees

Priority Queues (Heaps)

Organizing Spatial Data

Transcription:

4.2 Abstract Trees Having introduced the tree data structure, we will step back and consider an Abstract Tree that stores a hierarchical ordering. 4.2.1 Description An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include: 1. Accessing the root, and 2. Given a reference to any node within the tree: a. Get a reference to its predecessor (the node s parent), b. Query the number of successors (what is the degree of the node), c. Get a reference to a child, d. Attach a new sub-tree as a child of the current node, and e. Detach the tree rooted at this node from the parent. In a hierarchical ordering, abstract trees will usually not restrict the degree of a node. 4.2.2 Linked List Implementation In this elementary implementation of a Tree ADT, we store the children as a linked list of pointers to those sub-trees. A full implementation of this class is found on the ECE 250 web site under Algorithms/Trees/Simple_trees/. #include <algorithm> #include "Single_list.h" class Simple_tree { private: Type element; Simple_tree *parent_node; ece250::single_list<simple_tree *> children; public: Simple_tree( Type const &, Simple_tree * = 0 ); Type retrieve() const; Simple_tree *parent() const; bool is_root() const; bool is_leaf() const; int degree() const; Simple_tree *child( int n ) const; int size() const; int height() const; ; void attach( Type const & ); void attach( Simple_tree * ); void detach(); Page 1 of 6

While looking at these member functions, you can consider calling these member functions on the various nodes shown in the simple tree shown in Figure 1. Figure 1. A tree of six nodes stored in the Simple_tree data structure. Page 2 of 6

4.2.2.1 Basic Functionality The simple accessing and query member functions have implementations that are similar to what you would expect from the Single_list and Single_node class. Simple_tree<Type>::Simple_tree( Type const &obj, Simple_tree *p ): element( obj ), parent_node( p ) { // Empty constructor Type Simple_tree<Type>::retrieve() const { return element; Simple_tree<Type> *Simple_tree<Type>::parent() const { return parent_node; bool Simple_tree<Type>::is_root() const { return ( parent() == 0 ); int Simple_tree<Type>::degree() const { return children.size(); bool Simple_tree<Type>::is_leaf() const { return ( degree() == 0 ); 4.2.2.2 Accesing the n th Child The user would ask for the n th child. If this is outside the range, we return the zero pointer, otherwise, we step through the linked list until we reach the n th entry. Simple_tree<Type> *Simple_tree<Type>::child( int n ) const { if ( n < 0 n >= degree() ) { return 0; ece250::single_node<simple_tree *> *ptr = children.head(); for ( int i = 1; i < n; ++i ) { ptr = ptr->next(); return ptr->retrieve(); Page 3 of 6

4.2.2.3 Attaching and detaching children If we are attaching a new object into the current node to form a child (suppose we hire a new employee or create a new derived class), we push a new tree onto the back of the linked list: void Simple_tree<Type>::attach( Type const &obj ) { children.push_back( new Simple_tree( obj, this ) ); To detach a tree, we first check if it is already the root of a tree in which case we do nothing. Otherwise, we erase this tree from the children of the parent and set this nodes parent to the zero pointer. void Simple_tree<Type>::detach() { if ( is_root() ) { return; parent()->children.erase( this ); parent_node = 0; If, however, we are attaching an already constructed tree, we must be a little more careful. First, if the tree we are attaching is attached to a different tree, we must detach it from its parent. void Simple_tree<Type>::attach( Simple_tree<Type> *tree ) { if (!tree->is_root() ) { tree->detach(); tree->parent_node = this; children.push_back( tree ); Page 4 of 6

4.2.2.4 A Recursive Size and Height Member Functions As our first two recursive functions, we will see how we can recursively compute the size (number of nodes in) and height of a tree. The size of a tree is one plus the sizes of all the children. int Simple_tree<Type>::size() const { int h = 1; for ( ece250::single_node<simple_tree *> *ptr = children.head(); ptr!= 0; ptr = ptr->next() ) { s += ptr->retrieve()->size(); return s; The height of tree with a single node is zero; however, if there are any children, the height is one more than the maximum height of the children. int Simple_tree<Type>::height() const { int h = 0; for ( ece250::single_node<simple_tree *> *ptr = children.head(); ptr!= 0; ptr = ptr->next() ) { h = std::max( h, 1 + ptr->retrieve()->height() ); return h; Page 5 of 6

4.2.3 Array Implementation An implementation using arrays would be similar to that using a linked list the implementation, however, would be more complex. class Simple_tree { private: Type element; Simple_tree *parent_node; int child_count; int child_capacity; Simple_tree *children; // Everything else is similar to above Simple_tree<Type>::Simple_tree( Type const &obj, Simple_tree *p ): element( obj ), parent_node( p ), child_count( 0 ), child_capacity( 4 ), children( new Simple_tree *[child_capacity] ) { // Empty constructor 4.2.4 Locally Defined Orders The ordering of general trees is usually local: 1. A root node is explicitly defined. 2. A new node is defined as a being a child of a given parent node 3. There is no general definition as to what happens to children when a node is removed 4. Given two nodes in a tree, an algorithm must be used to determine any relationship between them based on the ordering within the tree Page 6 of 6