Data Structures – Binary Tree

Slides:



Advertisements
Similar presentations
Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key.
Advertisements

Chapter 10: Data Structures II
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
BST Data Structure A BST node contains: A BST contains
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
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 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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 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.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
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.
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.
David Stotts Computer Science Department UNC Chapel Hill.
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.
Discrete Mathematics Chapter 5 Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
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.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
AA Trees.
Recursive Objects (Part 4)
Binary search tree. Removing a node
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Tree.
Section 8.1 Trees.
Trees.
Tonga Institute of Higher Education
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 10: Non-linear Data Structures
Chapter 21: Binary Trees.
Abstract Data Structures
Binary Search Trees.
Chapter 20: Binary Trees.
Trees.
Non-Linear data structures
Data Structures Using C++ 2E
Data Structures – Binary Tree
Presentation transcript:

Data Structures – Binary Tree

What is a tree?

Where do you see trees? Ummm...outside file systems

Tree Terminology node – any element of the tree root – the topmost node of the tree parent – a node that has one or more nodes connected below it (children) child – a node that has a connected node above it (parent) leaf – any child node at the bottom of the tree subtree – a parent and all the nodes below it

Name that part! root / parent subtree parent / child’ child / leaf

So what’s a binary tree? A parent can have at most TWO children (left child, right child) The left child’s data and all the data in the left subtree is “less than” the parent’s data The right child’s data all the data in the right subtree is “greater than” the parent’s data NOTE: The “less than” and “greater than” requirements of the subtrees is commonly known outside of the IB world as a Binary Search Tree

Example – Valid Binary Tree 6 2 10 4 1

Example – INVALID Binary Tree 3 2 10 4 1

Practice 1 Create a binary tree by inserting the following numbers in the given order: 6, 4, 2, 7, 8, 14, 9

Practice 2 Create a binary tree by inserting the following numbers in the given order : 14, 75, 2, 34, 25, 26, 27, 28

Another Binary Tree h d m a k x

Practice 3 Create a binary tree by inserting the following strings: Wanda, Alpos, Vazbyte, Fecso, Downs, Mata, Montante

Practice 4 Create a binary tree by inserting the following strings: Ramos, Dia, Khurelbaatar, Nice, Ren, Shahid, Zetkulic

Adding to a Binary Tree Start at the root Compare against the current node Go left if less than current node OR insert if there is none (creating a new leaf) Go right if greater than current node OR insert if there is none (creating a new leaf) Repeat step 2

Searching in a Binary Tree Start at the root Compare against the current node Found the node if they are equal Go left if less than current node OR if there is no left, then does not exist Go right if greater than current node OR if there is no left, then does not exist Repeat step 2

Search(4) – What path do you take? 6 2 10 4 1 13

Search(9) – What path do you take? 6 2 10 4 1 13

Removing from a Binary Tree Search for matching node If node is a leaf, then unlink its parent If node is a parent of one child, then link the node’s parent to the node’s child If node is a parent of two children, then travel down its right subtree to find the left-most leaf (smallest value of the right subtree). Take the value and put it in the original node that was being removed. Unlink the right-most leaf that you found.

Remove(4) – 0 children case 6 2 10 4 1 13

Remove(10) – 1 child case 6 2 10 4 1 13

Remove(6) – 2 children case 10 4 1 13

When do we use binary trees? ...whenever we need to search for quickly Inherent binary searching capabilities Large trees can be searched quickly What is the best case scenario? What is the worst case scenario? What is the average case scenario? Compare a Linked List to a Binary Tree

Tree Traversal Add, remove, search only go down 1 path How do you “walk-through” all the nodes of a tree?

In-order tree traversal Left-subtree traversal if it exists and rerun Action on the current node (e.g. print) Right-subtree traversal if it exists and rerun Note: In-order traversal often used to visit nodes in their inherent order

In-order Traversal (1, 2, 4, 6, 8, 10, 13) 6 2 10 4 1 8 13

Pre-order tree traversal Action on the current node (e.g. copy) Left-subtree traversal if it exists and rerun Right-subtree traversal if it exists and rerun Note: Pre-order traversal is often used to duplicate a tree

Pre-order Traversal (6, 2, 1, 4, 10, 8, 13)

Post-order tree traversal Left-subtree traversal if it exists and rerun Right-subtree traversal if it exists and rerun Action on the current node (e.g. print) Note: Post-order traversal is often used to completely delete or free up all nodes by visiting children and lowest levels first. (not really necessary with garbage collection)

Post-order Traversal (1, 4, 2, 8, 13, 10, 6)

Other Resources http://www.csanimated.com/animation.php?t=Binary_search_tree