Tree 1 2 3 A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes. 4 5 6 7 8 9.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Computer Science C++ High School Level By Guillermo Moreno.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
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,
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
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,
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 CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Rudiments of Trees a Joshua presentation DATA STRUCTURES.
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.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
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.
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 CMSC 341 Introduction to Trees Textbook sections:
Data Structure and Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Recursive Objects (Part 4)
Binary search tree. Removing a node
Binary Search Trees Chapter 7 Objectives
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Problems with Linked List (as we’ve seen so far…)
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Trees.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Chapter 20: Binary Trees.
Data Structures and Database Applications Binary Trees in C#
Chapter 21: Binary Trees.
Trees and Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Binary Search Trees (BSTs)
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Representing binary trees with lists
Non-Linear Structures
Tree.
Trees.
Chapter 20: Binary Trees.
Basic Data Structures - Trees
Trees are Everywhere family genealogy organizational charts
Tree traversals BST properties Search Insertion
Tree and its terminologies
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Tree 1 2 3 A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes. 4 5 6 7 8 9

Root 1 2 3 4 5 6 7 Leaves We'll refer to node 1 as the root node, and external nodes like 5, 6, 7, 8, and 9 as leaves. It's also convention to use the following terms to describe relationships between nodes: nodes 2 and 3 are siblings of each other nodes 5, 6, and 7 are children of node 3 node 2 is the parent of node 4 8 9

Binary Tree 33 99 77 A binary tree is a type of tree in which each node is comprised of some data as well as node pointers to at most two children. 88 22 11 66

typedef struct node { int n; struct node* left; struct node* right; } 55 Here's an example of a binary tree node implementation. Note how each node in a binary tree is comprised of two node pointers and some data. n is the data stored in this node, and left and right are pointers either to the node's children (or to NULL if no children exist).

Binary Search Tree 55 33 77 33 77 A binary search tree is a special type of binary tree that simplifies searching. For each node in a binary search tree, every value on its left child's side is less than its own value, and every value on its right is greater. Notice how all values to the left of the root (55) are less than 55, and all the values to the right are greater than 55. 22 44 66 88 22 44 66 88

bool search(node* root, int val) { if root is NULL return false. if root->n is val return true. if val is less than root->n search left child if val is greater than root->n search right child } Here's some pseudocode to search a binary search tree for a particular value val. If the current node's value is less than the value you're looking for, recursively search its right child. If the current node's value is greater than the value you're looking for, recursively search its left child.