The Tree ADT. The Tree ADT. Defining the Tree ADT Recursively. Class #14: Introduction to Trees T 2. T k T 1

Similar documents
Binary Search Trees CMPSC 122

Algorithms and Data Structures

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

Ordered Lists and Binary Trees

Binary Search Trees (BST)

Data Structure [Question Bank]

DATA STRUCTURES USING C

Data Structure with C

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

From Last Time: Remove (Delete) Operation

Any two nodes which are connected by an edge in a graph are called adjacent node.

Data Structures and Algorithms

Symbol Tables. Introduction

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.


Analysis of Algorithms I: Binary Search Trees

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Binary Trees and Huffman Encoding Binary Search Trees

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Converting a Number from Decimal to Binary

Big Data and Scripting. Part 4: Memory Hierarchies

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

PES Institute of Technology-BSC QUESTION BANK

Parallelization: Binary Tree Traversal

How To Create A Tree From A Tree In Runtime (For A Tree)

Binary Heap Algorithms

10CS35: Data Structures Using C

Full and Complete Binary Trees

TREE BASIC TERMINOLOGIES

GRAPH THEORY LECTURE 4: TREES

Exam study sheet for CS2711. List of topics

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

M-way Trees and B-Trees

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Data Structures and Algorithms Written Examination

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Data Structures Fibonacci Heaps, Amortized Analysis

CSE 326: Data Structures B-Trees and B+ Trees

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Algorithms Chapter 12 Binary Search Trees

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

CS711008Z Algorithm Design and Analysis

Binary Coded Web Access Pattern Tree in Education Domain

Mathematics for Algorithm and System Analysis

Heaps & Priority Queues in the C++ STL 2-3 Trees

6 March Array Implementation of Binary Trees

Classification/Decision Trees (II)

Algorithms and Data Structures

Binary Heaps. CSE 373 Data Structures

Data Structures. Level 6 C Module Descriptor

International Journal of Software and Web Sciences (IJSWS)

Lecture Notes on Binary Search Trees

Why Use Binary Trees?

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Data Structures Using C++

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Search Trees 3/20/14

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Vector storage and access; algorithms in GIS. This is lecture 6

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Lecture 4: Balanced Binary Search Trees

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

Binary Search Trees. Ric Glassey

Mathematical Induction. Lecture 10-11

Analysis of Algorithms, I

Data Structures and Algorithms(5)

Fast Multipole Method for particle interactions: an open source parallel library component

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

BCS2B02: OOP Concepts and Data Structures Using C++

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Persistent Binary Search Trees

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Database Design Patterns. Winter Lecture 24

Lecture Notes on Binary Search Trees

Chapter 14 The Binary Search Tree

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

The ADT Binary Search Tree

Adding New Level in KDD to Make the Web Usage Mining More Efficient. Abstract. 1. Introduction [1]. 1/10

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Data Structures. Chapter 8

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Topological Properties

Transcription:

The Tree DT lass #14: ntroduction to Trees Software Design (S 340): M. llen, 25 Feb. 15! Trees are collections of objects, related in a branching, ordered fashion! an represent a variety of real-world objects and concepts, like:! Directory/file systems! Ranked organizations! Progress in an search! Structure of nested arithmetic expressions Wednesday, 25 Feb. 2015" Software Design (S 340)" 2" The Tree DT! Like lists, trees are collections of objects of some type! Unlike array-based structures, more like linked collections! tems are usually not directly indexed, but instead are accessed by traversing down the tree from a fixed starting point! Unlike either sort of structure, trees are not linear, but instead have a hierarchical structure:! Tree nodes (vertices) hold data! Tree edges (branches) connect nodes! Like lists, trees have a fixed order of traversal: not first-to-last, but top-down, starting at the top (root) vertex Defining the Tree DT Recursively 1. ny single node r is a tree (the root). 2. f r is a single root node and T 1, T 2,, T k are trees with their own roots q 1, q 2,, q k, then the structure formed by connecting root r to every other root q i is also a tree. q 1 T 1 q 2 T 2 r " q k T k Wednesday, 25 Feb. 2015" Software Design (S 340)" 3" Wednesday, 25 Feb. 2015" Software Design (S 340)" 4" 1

Tree Terminology deg(t) = 3 Root deg(n) = 3 Tree Terminology! Edges are directional, from parent to child, with at most one parent per child! The root is the top node (only one without a parent)! node without any children is called a leaf! ny other node is internal! The degree of a node is the number of its children nternal deg(n) = 2! path is a sequence of nodes n 0, n 1, n k where each n i is the parent of n i+1! The length k of a path is the number of edges in it! The descendants of node n are all the nodes on some path from n to any leaf! The ancestors of node n are all the nodes that are on some path from root to n ncestors of l 0 r n 0 n 1 n 2 path from root to l 6 of length 2 l 0 l 1 l 2 l 3 n 3 l 6! The degree of a tree is the maixmal node-degree Leaves deg(n) = 0! The siblings of node n are all the nodes with same parent pair of siblings l 4 l 5 Descendants of n 2 Wednesday, 25 Feb. 2015" Software Design (S 340)" 5" Wednesday, 25 Feb. 2015" Software Design (S 340)" 6" Tree Terminology! The depth (level) of a node n is the length of the single path from the root down to n! The height of node n is the length of the longest path from n to any leaf! The height of a tree itself is the height of the root height(t) = 2 height(n) = 2 depth(n) = 0 height(n) = 1 depth(n) = 1 height(n) = 0 depth(n) = 2 mplementing Tree Nodes! Typically, we might use a linking structure for nodes class TreeNode<T>! TreeNode<T> left;! TreeNode<T> right;!! One drawback: this only works for trees with nodes that all have degree 2 left! right! " Wednesday, 25 Feb. 2015" Software Design (S 340)" 7" Wednesday, 25 Feb. 2015" Software Design (S 340)" 8" 2

Tree Nodes with Varying Degree! nstead, we could use a more general linking structure, based on child-sibling relations! Using a sibling link allows us to add as many or as few as we need at any level D E F K child! class TreeNode<T>! TreeNode<T> child;! TreeNode<T> sibling;! sibling! D E F K inary Trees! n fact, for many purposes, we will be perfectly happy with trees in which every node has at most two children, and will use basic parent-child pair-linking! Usually, we will draw them without the explicit null nodes at the leaves class inarynode<t>! TreeNode<T> left;! TreeNode<T> right;! Wednesday, 25 Feb. 2015" Software Design (S 340)" 9" Wednesday, 25 Feb. 2015" Software Design (S 340)" 10" Properties of inary Trees! perfect binary tree is one where each node has either 2 or 0 children, and all leaves have the same depth Perfect tree mperfect tree Properties of Perfect inary Trees! perfect binary tree has: 1. 2 L nodes at level L 2. 2 h leaves for height h 3. 2 h+1 1 nodes for height h (equivalently, 2n 1 nodes for n leaves) 4. eight log 2 n for n leaves 2 0 = 1 nodes Total count 2 4 1 = 15 nodes 2 1 = 2 nodes log 2 8 = 3 height 2 2 = 4 nodes 2 3 = 8 leaves 2 3 = 8 nodes Wednesday, 25 Feb. 2015" Software Design (S 340)" 11" Wednesday, 25 Feb. 2015" Software Design (S 340)" 12" 3

Properties of inary Trees! complete binary tree is perfect except for (possibly) the deepest level! f the deepest level is not complete, it must be filled in left-to-right order omplete tree ncomplete tree Properties of omplete inary Trees! complete binary tree has: 1. t most 2 L nodes at level L 2. t least 2 h nodes for height h 3. t most 2 h+1-1 nodes for height h 2 0 = 1 nodes 8 2 3 nodes 8 2 4-1 nodes 2 1 = 2 nodes 2 2 = 4 nodes 1 2 3 nodes Wednesday, 25 Feb. 2015" Software Design (S 340)" 13" Wednesday, 25 Feb. 2015" Software Design (S 340)" 14" Tree Traversal! We can list binary tree nodes using four basic orderings: 1. level-order: list nodes left-to-right on each level in turn, from the top downwards 2. pre-order: list parent first, then repeat recursively for each child, left-to-right 3. post-order: traverse children left-to-right recursively; list parent after all children have been listed 4. in-order: list left sub-tree recursively, then root, then list right sub-tree recursively Traversal Examples! Level-order: L-to-R, level by level.! F,,,, D,,, E,! Pre-order: Parent, then recursively L-to-R on children.! F,,, D,, E,,,! Post-order: Recursively L-to-R on children, then parent.!,, E, D,,,,, F! n-order: Left subtree, then root, then right subtree.!,,, D, E, F,,, Wednesday, 25 Feb. 2015" Software Design (S 340)" 15" Wednesday, 25 Feb. 2015" Software Design (S 340)" 16" 4

n Example: Expression Trees! n expression tree is a binary tree corresponding to some mathematical expression! Each internal node is an arithmetical operator! Each leaf is a value or a declared variable! The tree can be evaluated by traversing the elements and performing computation as the elements are traversed Expression Tree! We can use a stack of trees and an expression in postfix form to create such a tree! We can traverse the tree to generate or evaluate the represented expression! n-order: 3 + 1 * 4 / 2 * 5-5!! Pre-order: * + 3 1 - * / 4 2 5 5! Post-order: 3 1 + 4 2 / 5 * 5 - *! Wednesday, 25 Feb. 2015" Software Design (S 340)" 17" Wednesday, 25 Feb. 2015" Software Design (S 340)" 18" This Week & Next! Topic: Stacks, Queues, ntroduction to Trees! Read: Start reading Text, chapter 04! omework 02: Today, 5:00 PM! Midterm 01: Wednesday, 02 March (in class)! overs all material up to (and including) Stacks & Queues! hapters 1, 2, 3 of text! Lectures 1 9! Office ours: Wing 210! Tuesday: 10:00 11:30 M & 4:00 5:30 PM! Thursday: 4:00 5:30 PM only (M trip) Wednesday, 25 Feb. 2015" Software Design (S 340)" 19" 5