BBM 201 Data structures

Similar documents
Binary Trees, Binary Search Trees

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

Binary Trees. Examples:

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

Programming II (CS300)

Chapter 20: Binary Trees

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

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

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

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

Data and File Structures Laboratory

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

Binary Trees

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

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

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

Trees. CSE 373 Data Structures

Data Structures And Algorithms

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Binary Trees. Height 1

Trees, Binary Trees, and Binary Search Trees

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

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

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

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

CMPT 225. Binary Search Trees

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

TREES. Trees - Introduction

Introduction to Computers and Programming. Concept Question

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

Principles of Computer Science

Why Do We Need Trees?

Successor/Predecessor Rules in Binary Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

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

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Tree Data Structures CSC 221

Trees. Truong Tuan Anh CSE-HCMUT

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

CS24 Week 8 Lecture 1

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

CE 221 Data Structures and Algorithms

Binary Trees Fall 2018 Margaret Reid-Miller

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

Trees and Graphs. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

March 20/2003 Jayakanth Srinivasan,

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

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Trees (part 2) CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Trees. Trees. CSE 2011 Winter 2007

Trees. A tree is a directed graph with the property

CSI33 Data Structures

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Topic 14. The BinaryTree ADT

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

CS302 - Data Structures using C++

Data Structure - Binary Tree 1 -

Data Structures and Algorithms

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

tree nonlinear Examples

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

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

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Trees. Tree Structure Binary Tree Tree Traversals

Data Structures and Algorithms for Engineers

Data Structures and Algorithms

Algorithms and Data Structures

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Garbage Collection: recycling unused memory

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

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

INF2220: algorithms and data structures Series 1

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

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

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

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

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

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

Data Structures - Binary Trees 1

Binary Trees and Binary Search Trees


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

ITEC2620 Introduction to Data Structures

Data Structures. Trees, Binary trees & Binary Search Trees

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Trees and Binary Trees

COMP : Trees. COMP20012 Trees 219

AP Programming - Chapter 20 Lecture page 1 of 17

Binary Trees and Huffman Encoding Binary Search Trees

Trees. 9/21/2007 8:11 AM Trees 1

Trees. Eric McCreath

Transcription:

BBM 201 Data structures Lecture 11: Trees 2018-2019 Fall

Content Terminology The Binary Tree The Binary Search Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Terminology Trees are used to represent relationships Trees are hierarchical in nature Parent-child relationship exists between nodes in tree. Generalized to ancestor and descendant Lines between the nodes are called edges A subtree in a tree is any node in the tree together with all of its descendants Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Terminology Only access point is the root All nodes, except the root, have one parent like the inheritance hierarchy in Java Traditionally trees are drawn upside down root leaves

Terminology (a) A tree; (b) a subtree of the tree in part a Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Terminology FIGURE 15-2 (a) An organization chart; (b) a family tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Properties of Trees and Nodes siblings: two nodes that have the same parent edge: the link from one node to another path length: the number of edges that must be traversed to get from one node to another edge root siblings path length from root to this node is 3

General Tree A general tree is a data structure in that each node can have infinite number of children A general tree cannot be empty. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Binary Tree A Binary tree is a data structure in that each node has at most two nodes left and right. A Binary tree can be empty. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

n -ary tree A generalization of a binary tree whose nodes each can have no more than n children. n

Example: Algebraic Expressions. FIGURE 15-3 Binary trees that represent Data Structures and Problem algebraic Solving with expressions C++: Walls and Mirrors, Carrano and Henry, 2013

Level of a Node Definition of the level of a node n : If n is the root of T, it is at level 1. If n is not the root of T, its level is 1 greater than the level of its parent. Level = 1 Level = 2 Level = 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Height of Trees The height of a node is the number of edges on the longest downward path between that node and a leaf. Height of tree = 2

The Height of Trees Height 2 Height 4 Height 6 Height 6 Binary trees with the same nodes but different heights

Depth of a Tree The path length from the root of the tree to this node. b a c d e f g h i j k l The depth of a node is its distance from the root a is at depth zero e is at depth 2 The depth of a binary tree is the depth of its deepest node This tree has depth 4

Full, Complete, and Balanced Binary Trees Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Full Binary Trees Definition of a full binary tree If T is empty, T is a full binary tree of height 0. If T is not empty and has height h > 0, T is a full binary tree if its root s subtrees are both full binary trees of height h 1. Every node other than the leaves has two children. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Facts about Full Binary Trees You cannot add nodes to a full binary tree without increasing its height. The number of nodes that a full binary tree of height h can have is 2 (h+1) 1. The height of a full binary tree with n nodes is log 2 (n+1)-1 The height of a complete binary tree with n nodes is floor(log 2 n) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Complete Binary Trees Every level, except possibly the last, is completely filled, and all nodes are as far left as possible FIGURE 15-7 A complete binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Full, Complete A not binary or Other? B C D I H G F E J K L M N O T S R Q P

Full, Complete A or other? B D I H G F K L M N O T S R P

Full, A Complete or other? B D I H G F K L M N O T S R P

Full, A Complete or other? B D I H G F K L M N O T S R P

Full, A Complete or other? B D I H G F K L M N O R P T S

Full, A Complete or Other? B D I H G F K L M N O P Q R T S

Full, A Complete or other? B D I H G F K L M N O P Q R T S

Full, A Complete or Other? B D I H G F K L M N O P Q R

A balanced binary tree has the minimum possible height for the leaves d c g b f a e h i j An unbalanced binary tree

Number of Nodes in a Binary Tree depth: h level: h+1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Traversals of a Binary Tree General form of recursive traversal algorithm 1. Preorder Traversal Each node is processed before any node in either of its subtrees 2. Inorder Traversal Each node is processed after all nodes in its left subtree and before any node in its right subtree 3. Postorder Traversal Each node is processed after all nodes in both of its subtrees Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Preorder Traversal 1. Visit the root 2. Visit the left subtree 3. Visit the right subtree A1 B2 11 D 3I H8 12 G 13 F K4 L5 M9 10 N 14 O T6 S7 15 R 16 P

Algorithm TraversePreorder(n) Process node n if n is an internal node then TraversePreorder( n -> leftchild) TraversePreorder( n -> rightchild)

Inorder Traversal 1. Visit Left subtree 2. Visit the root 3. Visit Right subtree 10 1A 16 B 12 D 12 I H8 11 G 16 F K1 4L M7 N9 14 O 3T 5S 13 R 15 P

Algorithm TraverseInorder(n) if n is an internal node then TraverseInorder( n -> leftchild) Process node n if n is an internal node then TraverseInorder( n -> rightchild)

Postorder Traversals 1. Visit Left subtree 2. Visit Right subtree 3. Visit the root 16 A 9B 15 D 5 I H8 10 G 14 F 1K 424 L M6 N7 13 O T2 3S 11 R 12 P

Algorithm TraversePostorder(n) if n is an internal node then TraversePostorder( n -> leftchild) TraversePostorder( n -> rightchild) Process node n

Traversals of a Binary Tree FIGURE 15-11 Three traversals of a binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Binary Tree Operations Test whether a binary tree is empty. Get the height of a binary tree. Get the number of nodes in a binary tree. Get the data in a binary tree s root. Set the data in a binary tree s root. Add a new node containing a given data item to a binary tree. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Binary Tree Operations Remove the node containing a given data item from a binary tree. Remove all nodes from a binary tree. Retrieve a specific entry in a binary tree. Test whether a binary tree contains a specific entry. Traverse the nodes in a binary tree in preorder, inorder, or postorder. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Represention of Binary Tree ADT A binary tree can be represented using - Linked List - Array Note : Array is suitable only for full and complete binary trees

struct node { int key_value; struct node *left; struct node *right; }; struct node *root = 0;

void inorder(node *p) { if (p!= NULL) { inorder(p->left); printf(p->key_value); inorder(p->right); } } void preorder(node *p) { if (p!= NULL) { printf(p->key_value); preorder(p->left); preorder(p->right); } } void postorder(node *p) { if (p!= NULL) { postorder(p->left); postorder(p->right); printf(p->key_value); } }

void destroy_tree(struct node *leaf) { if( leaf!= 0 ) { destroy_tree(leaf->left); destroy_tree(leaf->right); free( leaf ); } }

The Binary Search Tree Binary tree is ill suited for searching a specific item Binary search tree solves the problem Properties of each node, n n s value is greater than all values in the left subtree T L n s value is less than all values in the right subtree T R Both T R and T L are binary search trees. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

The Binary Search Tree FIGURE 15-13 A binary search tree of names Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

The Binary Search Tree FIGURE 15-14 Binary search trees with the same data as in Figure 15-13 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

The Binary Search Tree FIGURE 15-14 Binary search trees with the same data as in Figure 15-13 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

The Binary Search Tree Binary search trees with the same data as in Figure 15-13 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Binary Search Tree Operations Test whether a binary search tree is empty. Get height of a binary search tree. Get number of nodes in a binary search tree. Get data in binary search tree s root. Insert new item into the binary search tree. Remove given item from the binary search tree. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Binary Search Tree Operations Remove all entries from a binary search tree. Retrieve given item from a binary search tree. Test whether a binary search tree contains a specific entry. Traverse items in a binary search tree in Preorder Inorder Postorder. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

Searching a Binary Search Tree Search algorithm for binary search tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

struct node *search(int key, struct node *leaf) { if( leaf!= 0 ) { if(key==leaf->key_value) { return leaf; } else if(key<leaf->key_value) { return search(key, leaf->left); } else { return search(key, leaf->right); } } else return 0; }

Creating a Binary Search Tree Empty subtree where the search algorithm terminates when looking for Frank Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

void insert(int key, struct node **leaf) { if( *leaf == 0 ) { *leaf = (struct node*) malloc( sizeof( struct node ) ); (*leaf)->key_value = key; /* initialize the children to null */ (*leaf)->left = 0; (*leaf)->right = 0; } else if(key < (*leaf)->key_value) { insert(key, &(*leaf)->left ); } else if(key > (*leaf)->key_value) { insert(key, &(*leaf)->right ); } }

Efficiency of Binary Search Tree Operations The Big O for the retrieval, insertion, removal, and traversal operations of the ADT binary search tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013