Lecture 9 Binary Trees Trees General Definition Terminology

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Search Trees CSE 331 Section 2 James Daly.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Department of Computer Science University of Maryland, College Park
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Lecture 21 Trees. “ A useful data structure for many applications”
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
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.
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.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
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.
Tree Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CSC 143 Trees [Chapter 10].
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
353 3/30/98 CSE 143 Trees [Sections , 13.6,13.8]
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
CC 215 Data Structures Trees
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Section 8.1 Trees.
CSE 373 Data Structures Lecture 7
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Chapter 20: Binary Trees.
Depict the Tree Structure in a picture
Chapter 21: Binary Trees.
Trees CSE 373 Data Structures.
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Chapter 20: Binary Trees.
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Data Structures Using C++ 2E
Presentation transcript:

Lecture 9 Binary Trees Trees General Definition Terminology Complete and Full Binary Tree Definition Sample Trees Binary Tree Nodes Binary Search Trees Using Binary Search Trees - Removing Duplicates Find, Insert, Erase

Used to implement STL set, multiset, map, multimap (OACs) Trees k-ary trees Type of graph: undirected, acyclic, connected |E| = |V| - 1 Each node has max of k children Most popular: k = 2 Used to implement STL set, multiset, map, multimap (OACs)

Terminology Root Parent Child Descendant Ancestor Leaf Interior Node Subtree Path Level Depth Height Terminology

Level, Depth, and Path Length

Binary Tree Definition A binary tree T is a finite set of nodes such that (a) T is empty; (b) T consists of a root, R, and exactly two distinct binary trees left subtree TL right subtree TR

Expression Tree a * b + (c – d) / e a b * c d – e / + – + * / e a b c Use a stack to build a binary tree out of posfix notation.

Complete Trees d as fn. of N? 2d <= N < 2(d + 1)  d = floor(lg(N))

Complete Trees (Cont’d)

Complete Trees (Cont’d) A E D C B G F K I H Non- Complete Tree (Depth 3) Nodes at level 3 do not occupy leftmost positions

Full Trees

Sample Trees Degenerate

Node Composition struct Node { Node (const T& v = T (), Node* l = NULL, Node* r = NULL) : data (v), left (l), right (r) { } T data; Node* left; Node* right; // maybe *parent also }; // familiar?

Recursive Tree Visits Systematically explore every node 3 methods In-order – left, node, right Pre-order – node, left, right Post-order – left, right, node void inOrder (Node* t) { if (t != NULL) { inOrder (t->left); process (t->data); inOrder (t->right); }

Calculate Depth int depth (Node* t) { if (t == NULL) return -1; return max (depth (t->left), depth (t->right)) + 1; }

Count Leaves int countLeaves (Node* t) { if (t == NULL) return 0; if (t->left == NULL && t->right == NULL) return 1; return countLeaves (t->left) + countLeaves (t->right); }

Binary Search Trees (BSTs) For each node N Keys in N’s left subtree are < key (N) Keys in N’s right subtree are > key (N) Duplicates?

Binary Search Trees

Using Binary Search Trees Removing Duplicates Insert into set, then copy back to vector

BST Find i = t.find (37); Current Node Action Root = 50 Compare item = 37 and 50 Go left Node = 30 Compare item = 37 and 30 Go right Node = 35 Compare item = 37 and 35 Node = 37 Compare item = 37 and 37. Found. BST Find

Set Class Implementation template<typename T> class Set { // Insert Node struct decl Node* h; size_t sz; public: Set () : h (new Node ()), sz (0) { h->left = h->right = h; } iterator find (const T& v); pair <iterator, bool> insert (const T& v); size_t erase (const T& v); // … };

Find iterator find (const T& seek) { Node* pn = h->right; h->data = seek; T data; while ((data = pn->data) != seek) pn = (seek < data) ? pn->left : pn->right; return iterator (pn); } // Recursive impl?

Insert Operation t.insert (32);

Insert (Cont’d)

Insert (Cont’d) Node* newNode = new Node (item, NULL, NULL, parent); parent->left = newNode; **Always insert as leaf**

Insert (Cont’d) pair <iterator, bool> insert (const T& add) { Node *p = h, *n = h->right; while (n != h) { p = n; if (add < n->data) n = n->left; else if (add > n->data) n = n->right; else return make_pair (iterator (n), false); } if (add < p->data) p = p->left = new Node (add, h, h); p = p->right = new Node (add, h, h); return make_pair (iterator (p), true);

Erase 4 cases Leaf node (no replacement needed) Has only left child Replace with in-order predecessor (IOP) Has only right child Replace with in-order successor (IOS) Has both children Find IOS Splice out IOS Splice in IOS in place of node to erase

Erase (Leaf)

Erase (Only Left)

Erase (Only Right)

Erase (Both) Erase node with two children

Erase (Both)

Erase (Both)