8.1 Tree Terminology and Applications

Slides:



Advertisements
Similar presentations
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
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 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
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.
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.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
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.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
CS 201 Data Structures and Algorithms
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Data Structures and Design in Java © Rick Mercer
Problems with Linked List (as we’ve seen so far…)
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
CMSC 341 Introduction to Trees.
Lecture 18. Basics and types of Trees
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Monday, March 19, 2018 Announcements… For Today… For Next Time…
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Data Structures and Database Applications Binary Trees in C#
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Lab 08 - BST.
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
8.2 Tree Traversals Chapter 8 - Trees.
Trees and Binary Trees.
General Trees & Binary Trees
Friday, April 13, 2018 Announcements… For Today…
Find in a linked list? first last 7  4  3  8 NULL
Lab 07 – 3D Maze.
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Trees Chapter 6.
CE 221 Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Binary Trees, Binary Search Trees
8.1 Tree Terminology and Applications
8.2 Tree Traversals Chapter 8 - Trees.
Introduction to Trees Chapter 6 Objectives
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

8.1 Tree Terminology and Applications Chapter 8 - Trees

Attendance Quiz #25 Trees

Tip #27: Copying Objects Trees Containers hold objects, but not the ones you give them! Instead, when you add an object to a container (via e.g., insert or push_back, etc.), what goes into the container is a copy of the object. Once in a container, it's not uncommon for it to be copied further. If your container resizes or shrinks, objects will be moved (copied) around. If you insert something or erase something from a vector, string, or deque, existing container elements are typically moved (copied) around. If you use any of the sorting algorithms, remove, unique, rotate or reverse, etc., objects will be moved (copied) around. Yes, copying objects is the STL way! An object is copied by using it copying member function, in particular, the copy constructor and its copy assignment operator. Take away: If you fill a container with objects where copying is expensive, the simple act of putting the objects into the container could prove to be a performance bottleneck. The more things that get moved around in the container, the more memory and cycles you'll blow on making copies.

Lab 07 – 3D Maze

Finding a Path through a Maze Trees Problem Use backtracking to find and display the path through a maze. From each point in a maze you can move to the next cell in a horizontal or vertical direction if the cell is not blocked. Analysis The maze will consist of an array of cells. The starting point is at the top left corner maze[0][0][0]. The exit point is at the bottom right corner, that is maze[HEIGHT - 1][WIDTH - 1][LAYERS - 1]. All cells on the path will have a OPEN value. All cells that represent barriers will have a BLOCKED value. Cells that we have visited will have a TEMPORARY value. If we find a path through the maze, the exit cell value will be EXIT and all cells on the path will have the PATH value.

The Maze Layout Trees The maze size is defined by height x width x layer as specified on the first line of the test file. Width Left Right START 0,0,0 0,1,0 0,2,0 0,3,0 1,0,0 1,1,0 1,2,0 1,3,0 2,0,0 2,1,0 2,2,0 2,3,0 3,0,0 3,1,0 3,2,0 3,3,0 Layer Out IN 0,0,1 0,1,1 0,2,1 0,3,1 1,0,1 1,1,1 1,2,1 1,3,1 2,0,1 2,1,1 2,2,1 2,3,1 3,0,1 3,1,1 3,2,1 3,3,1 0,0,2 0,1,2 0,2,2 0,3,2 1,0,2 1,1,2 1,2,2 1,3,2 2,0,2 2,1,2 2,2,2 2,3,2 3,0,2 3,1,2 3,2,2 3,3,2 Down Up Height 0,0,3 0,1,3 0,2,3 0,3,3 1,0,3 1,1,3 1,2,3 1,3,3 2,0,3 2,1,3 2,2,3 2,3,3 3,0,3 3,1,3 3,2,3 EXIT 3,3,3

The Maze Layout Trees The maze size is defined by height x width x layer as specified on the first line of the test file. Width Left Right (START) (Open) 1 (Blocked) Layer Out IN (Open) 1 (Blocked) (Open) 1 (Blocked) Down Up Height 1 (Blocked) (Open) (Exit)

The Maze Layout (Bonus) Trees Maze values tell which way to proceed thru the maze ("L", "R", "U", "D", "I", or "O".) Width Left Right R (Right) I (In) (Open) 1 (Blocked) Layer Out IN I (In) L (Left) 1 (Blocked) (Open) Down Up Height R (Right) I (In) 1 (Blocked) (Open) 1 (Blocked) D (Down) R (Right) E (Exit)

Dynamic Arrays Trees A dynamic array is basically an array of pointers to arrays. A 2 dimensional dynamic array is created/deleted using a loop as follows: int height = 10; int width = 10; int **myArray = new int*[height]; for(int i = 0; i < height; ++i) { myArray[i] = new int[width]; } Constructor for(int i = 0; i < height; ++i) { delete [] myArray[i]; } delete [] myArray; Destructor

3D Maze Array Trees The 3D maze array is of data type "int***" - a pointer to a pointer to a pointer to an int. int** int* int int*** maze_ vs. int maze_[3][2][2] maze_[2][2][1] = 2; 2

2D Implementation bool find_maze_path(int height, int width) { Trees bool find_maze_path(int height, int width) { if ((height < 0) || (height >= HEIGHT) || (width < 0) || (width >= WIDTH)) return false; // out of bounds (base case #1) if (maze_[height][width] != OPEN) return false; // blocked (base case #2) if ((height == HEIGHT - 1) && (width == WIDTH - 1)) maze_[height][width] = EXIT; // success! (base case #3) return true; } maze_[height][width] = PATH; // possible path, try all paths if (find_maze_path(height - 1, width) || // check up find_maze_path(height + 1, width) || // check down find_maze_path(height, width - 1) || // check left find_maze_path(height, width + 1)) // check right return true; // one of the paths works maze_[height][width] = TEMPORARY; // none work, don’t visit again return false;

Requirements Trees Points Requirement (40 Points) 5 argv[1] and argv[2] used for input / output streams respectively. No execution user interaction (i.e. system("pause"); or getchr();). A Maze class contains a dynamically sized 3-dimensional maze array as specified by the first line of the input file. The Maze class is derived from the abstract MazeInterface interface class. The Maze toString() function returns a formatted string of the maze (as described above.) Backtracking used to correctly solve a 4 x 4 x 4 maze (lab07_in_01.txt). Backtracking used to correctly solve a 5 x 5 x 5 maze (lab07_in_02.txt). Backtracking used to correctly solve a 5 x 10 x 6 maze (lab07_in_03.txt). Your Maze program uses backtracking to detect an unsolvable maze (lab07_in_04.txt). BONUS: Your maze program outputs the solution path using 'L' for move left, 'R' for move right, 'U' for move up on the same layer, 'D' for move down on the same layer, 'I' for move to the next layer (layer + 1), and 'O' for move to the previous layer (layer - 1). In addition, 'E' indicates the exit point. (lab07_in_05.txt). VS_MEM_CHECK macro is included in main to detect memory leaks. No Memory leaks are reported.

Trees Chapter 8

Chapter Objectives Trees To learn how to use a tree to represent a hierarchical organization of information To learn how to use recursion to process trees To understand the different ways of traversing a tree To understand the differences between binary trees, binary search trees, and heaps To learn how to implement binary trees, binary search trees, and heaps using linked data structures and arrays To learn how to use a binary search tree to store information so that it can be retrieved in an efficient manner To learn how to use a Huffman tree to encode characters using fewer bits than ASCII or Unicode, resulting in smaller files and reduced storage requirements

Trees - Introduction Trees All previous data organizations we've studied are linear—each element can have only one predecessor and one successor. Accessing all elements in a linear sequence is O(n). Trees are nonlinear and hierarchical. Tree nodes can have multiple successors (but only one predecessor). Trees can represent hierarchical organizations of information: class hierarchy disk directory and subdirectories business organization

Trees - Introduction Trees Trees are recursive data structures because they can be defined recursively. Many methods to process trees are written recursively. post-order pre-order in-order This chapter focuses on the binary tree. In a binary tree each element has two successors. Binary trees can be represented by arrays and by linked data structures. Searching a binary search tree, generally is more efficient than linearly searching an ordered list—O(log n) versus O(n).

8.1, pgs. 447-453 8.1 Tree Terminology and Applications Binary Trees Some Types of Binary Trees Fullness and Completeness General Trees 8.1, pgs. 447-453

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine

The node at the top of a tree is called its root Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the top of a tree is called its root dog cat wolf canine

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The links from a node to its successors are called branches dog cat wolf canine

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The successors of a node are called its children dog cat wolf canine

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine The predecessor of a node is called its parent

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors Each node in a tree has exactly one parent except for the root node, which has no parent dog cat wolf canine

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors Nodes that have the same parent are siblings dog cat wolf canine

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A node that has no children is called a leaf node

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors Leaf nodes also are known as external nodes, and nonleaf nodes are known as internal nodes dog cat wolf canine A node that has no children is called a leaf node

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog is the parent of cat in this tree dog cat wolf canine A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine cat is the parent of canine in this tree A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine canine is a descendant of cat in this tree A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog is an ancestor of canine in this tree dog cat wolf canine A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A subtree of a node is a tree whose root is a child of that node

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A subtree of a node is a tree whose root is a child of that node

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A subtree of a node is a tree whose root is a child of that node

The level of a node is determined by its distance from the root Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The level of a node is determined by its distance from the root dog cat wolf canine

The level of a node is its distance from the root plus 1 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The level of a node is its distance from the root plus 1 (also called depth) dog cat wolf canine Level 1 Level 2 Level 3

The level of a node is defined recursively Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine Level 1 The level of a node is defined recursively Level 2 Level 3

The level of a node is defined recursively Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine Level 1 The level of a node is defined recursively Level 2 Level 3 If node n is the root of tree T, its level is 1 If node n is not the root of tree T, its level is 1 + the level of its parent

Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The height of a tree is the number of nodes in the longest path from the root node to a leaf node dog cat wolf canine

The height of this tree is 3 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The height of a tree is the number of nodes in the longest path from the root node to a leaf node dog cat wolf canine The height of this tree is 3

Tree Terminology Summary Trees Ancestor – A parent of a node or its ancestors. Branch – Link between nodes. Child – An immediate successor node. Children – Successor nodes. Depth – Level of a node. Descendent – A child node or its descendants. External Node – A node without children (leaf node.) Height – Number of nodes in the longest path from the root to a leaf node. Internal Node – A non-external node (non-leaf node.) Leaf Node – A node that has no children. Level – Distance from the root plus 1. Parent – Predecessor of a node. Root – Node at top of tree (without parents.) Sibling – Node with the same parent. Subtree – A node and all of its children. Successor – A child of a node.

Binary Trees In a binary tree, each node has two subtrees. A set of nodes T is a binary tree if either of the following is true: T is empty. If T is not empty, it has a root node r with 0, 1, or 2 nonempty binary subtrees whose roots are connected to r by a branch. (TL = left subtree; TR = right subtree) TL , TR, or both can be empty trees. From now on, we will consistently use a NULL pointer to represent an empty subtree.

Expression Tree Each node contains an operator or an operand. Trees Each node contains an operator or an operand. Operands are stored in leaf nodes. Parentheses are not stored in the tree because the tree structure dictates the order of operand evaluation. Operators in nodes at higher tree levels are evaluated after operators in nodes at lower sub-tree levels. (x + y) * ((a + b) / c)

Huffman Tree Trees A Huffman tree represents Huffman codes for characters that might appear in a text file. As opposed to ASCII or Unicode, Huffman code uses different numbers of bits to encode letters; more common characters use fewer bits. Many programs that compress files use Huffman codes.

Huffman Tree Trees To form a code, traverse the tree from the root to the chosen character, appending 0 if you branch left, and 1 if you branch right.

Huffman Tree Trees Examples: d : 10110 e : 010