Data Structures – Binary Tree

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
BST Data Structure A BST node contains: A BST contains
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Binary Search Trees Chapter 7 Objectives
Binary Trees Chapter 6.
Data Structures – Binary Tree
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Tree.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Lecture 17 Non-Linear data structures Richard Gesick.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
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.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
Data Structures and Design in Java © Rick Mercer
AA Trees.
The Tree Data Structure
Binary search tree. Removing a node
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Tree.
Section 8.1 Trees.
Source: Muangsin / Weiss
COMP 103 Binary Search Trees.
Tonga Institute of Higher Education
Binary Search Tree In order Pre order Post order Search Insertion
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees
Chapter 21: Binary Trees.
Binary Trees.
Trees Part 2!!! By JJ Shepherd.
Data Structures and Algorithms
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees.
Binary Search Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Data Structures and Algorithms
CMSC 341 Splay Trees.
Chapter 20: Binary Trees.
Lecture 9: Intro to Trees
Non-Linear data structures
8.2 Tree Traversals Chapter 8 - Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Data Structures – Binary Tree

What is a tree?

Where do you see trees? Ummm...outside Family trees 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 – any node and all its descendants

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

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

Creating a binary tree Insert the following numbers in the given order: 6, 4, 2, 7, 8, 14, 9

Your Turn Create a binary tree by inserting the following numbers in the given order : 14, 75, 2, 5, 60, 25, 1, 9

Your Turn Again Create a binary tree by inserting the following numbers in the given order : 2, 4, 6, 8, 10 Notice anything weird? It’s unbalanced! 

Another Binary Tree h d m a k x

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

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 the node is a leaf (0 children), unlink its parent If the node has 1 child, then link the node’s parent to the node’s child If the node has 2 children, then go to its right subtree and find the left-most child (smallest value of the right subtree). Take the value and put it in the original node that was being removed. Repeat the remove process on the node with 2 children

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? Balanced tree? Unbalanced tree? 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