Tree Data Structures CSC 221

Similar documents
Data Structure - Binary Tree 1 -

Binary Trees, Binary Search Trees

IX. Binary Trees (Chapter 10)

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Data Structures And Algorithms

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

March 20/2003 Jayakanth Srinivasan,

Programming II (CS300)

Introduction to Computers and Programming. Concept Question

BBM 201 Data structures

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

Data and File Structures Laboratory

Topic Binary Trees (Non-Linear Data Structures)

Binary Search Trees. What is a Binary Search Tree?

TREES. Trees - Introduction

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Binary Trees

Tree Structures. A hierarchical data structure whose point of entry is the root node

CSCI2100B Data Structures Trees

CHAPTER 5. Trees CHAPTER 5 1/70

Trees, Binary Trees, and Binary Search Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Chapter 20: Binary Trees

3 Trees: traversal and analysis of standard search trees

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Data Structures and Algorithms for Engineers

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

CE 221 Data Structures and Algorithms

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

Successor/Predecessor Rules in Binary Trees

Principles of Computer Science

Binary Trees and Binary Search Trees

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

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

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

Name CPTR246 Spring '17 (100 total points) Exam 3

Information Science 2

[ DATA STRUCTURES ] Fig. (1) : A Tree

Module 04 Trees Contents

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

Trees: examples (Family trees)

Binary Trees. Examples:

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.


CSI33 Data Structures

Topic 14. The BinaryTree ADT

DATA STRUCTURES USING C

3 Trees: traversal and analysis of standard search trees. Summer Term 2010


Associate Professor Dr. Raed Ibraheem Hamed

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSC148-Section:L0301

INF2220: algorithms and data structures Series 1

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Binary Trees. Height 1

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

LECTURE 13 BINARY TREES

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Data Structures - Binary Trees and Operations on Binary Trees

Trees. CSE 373 Data Structures

CS 206 Introduction to Computer Science II

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

tree nonlinear Examples

Trees and Binary Trees

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

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

CSC148 Week 6. Larry Zhang

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

CS302 - Data Structures using C++

Binary Trees Fall 2018 Margaret Reid-Miller

CHAPTER 5 TREES PART I

Tree Travsersals and BST Iterators

Tree traversals and binary trees

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

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

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

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

CSE 230 Intermediate Programming in C and C++ Binary Tree

Lecture 7. Binary Trees

Trees CONTENTS. Hours: 8. Marks: 12. Anuradha Bhatia 1

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

CSI33 Data Structures

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

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

Transcription:

Tree Data Structures CSC 221

Specialized Trees Binary Tree: A restriction of trees such that the maximum degree of a node is 2. Order of nodes is now relevant May have zero nodes (emtpy tree) Formal Definition: A binary tree is a finite set of nodes that either is empty or consists of a root and two disjoint subtrees called the left subtree and the right subtree.

Properties of Binary Trees Given definition of binary tree, interesting (useful?) properties that hold: Maximum number of nodes on level i is 2 i-1 Maximum number of nodes in a binary tree of depth k is 2 k 1 A binary tree with n nodes could have between log 2 n and n depth/height.

Questions What is the maximum number of nodes in a k-ary tree of height h? For 3 = 1 + 3 + 9 + 27 + 81 For 4 = 1 + 4 + 16 + 64 For k = 1 + k + k 2 + k 3 Sum of geometric series: (k h -1) / (k-1) k = 3, h = 4 = (3 4 1) / (3-1) = 80/2 = 40 Does it hold for 2? (2 h -1) / (2-1) = 2 h -1

Properties of Binary Trees A binary tree with n nodes could have between log 2 n and n depth/height. If completely skewed Only one child per node height n. If complete, 1 + 2 + 4 + 8 are counts we can have on each new level. Label levels starting at 1 Sum after every level i is 2 i -1 (think of all i bits on) Need to find I such that 2 i -1 = n 2 i = n+1 i = log 2 (n+1)

Binary Tree Node class BinaryTree; // forward declaration class BinaryTreeNode friend class BinaryTree; private: char data; BinaryTreeNode* leftchild; BinaryTreeNode* rightchild; };

Binary Tree Class class BinaryTree public: // public member methods private: BinaryTreeNode* root; };

Binary Tree Node A(nother?) with linked node representation: Difficult to find parent OK: Could add a parent pointer link to all nodes if particularly important without substantial changes, but with n (number of nodes) bytes more use of memory If is important, when traversing list maintain a parent pointer (like previous pointer) Recursive algorithms may implicitly keep it for us

Binary Tree Class What are functions of interest? class BinaryTree public: BinaryTree(); // create empty tree BinaryTree(char data, BinaryTree bt1, BinaryTree bt2); //construct a new tree, setting root data to data and links to //other trees bool isempty(); // test whether empty BinaryTree leftchild(); // get left child of *this BinaryTree rightchild(); // get right child of *this char Data(); // return data in root node of *this };

Binary Tree Functions Most operations performed on binary trees require moving through the tree: Visiting nodes Inserting an element Deleting an element Height Useful to have a simple mechanism for moving through trees

Binary Tree Traversal Tree traversal Visit each node in the tree exactly once Perform some operation Print data Add to sum Check for max height A traversal produces a linear ordering of the nodes [the order of visits]

Binary Tree Traversal Treat trees and subtrees in same fashion Traverse in same order Use recursion! Let the following define traversal operations: L => Move left V => Visit [Perform operation print, sum, ] R => Move right

Binary Tree Traversal Six possible methods of traversal: LVR, LRV, VLR, VRL, RVL, RLV Usually, only use three of these, all with left before right: LVR LRV VLR Inorder Postorder Preorder

Binary Tree Traversal Let s trace the traversal functions with examples of expressions (operators and operand) Traversal Name corresponds to order of outputted expression

Binary Tree Traversal + * E * D / C Inorder: LVR A / B * C * D + E Infix expression Visit left child before parent A B

Binary Tree Traversal Inorder implementation: void BinaryTree::inorder() inorder(root); } Void BinaryTree::inorder(BinaryTreeNode* node) if (node) inorder(node->leftchild); cout << node->data; // replace cout with inorder(node->rightchild); //arbitrary processing } }

Binary Tree Traversal + * E * D / C Postorder: LRV A B / C * D * E + Postfix expression Visit left and right child before parent A B

Binary Tree Traversal Postorder implementation: void BinaryTree::postorder() postorder(root); } void BinaryTree::postorder(BinaryTreeNode* node) if (node) postorder(node->leftchild); postorder(node->rightchild); cout << node->data; } }

Binary Tree Traversal + * E * D / C Preorder: VLR + * * / A B C D E Prefix expression Visit parent before either child A B

Binary Tree Traversal Preorder implementation: void BinaryTree::preorder() preorder(root); } Void BinaryTree::preorder(BinaryTreeNode* node) if (node) cout << node->data; preorder(node->leftchild); preorder(node->rightchild); } }