Trees Part 2!!! By JJ Shepherd.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
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.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
BST Data Structure A BST node contains: A BST contains
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Tree Data Structures.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search 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 Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees Chapter 7 Objectives
AA Trees.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary search tree. Removing a node
UNIT III TREES.
Binary Search Tree (BST)
B+-Trees.
Introduction Applications Balance Factor Rotations Deletion Example
B+-Trees.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Hashing Exercises.
Tonga Institute of Higher Education
ITEC 2620M Introduction to Data Structures
Binary Search Tree In order Pre order Post order Search Insertion
i206: Lecture 13: Recursion, continued Trees
Binary Search Trees Why this is a useful data structure. Terminology
Binary Tree Applications
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Trees 7/14/2009.
Data Structures and Algorithms
AVL Trees: AVL Trees: Balanced binary search tree
Trees CMSC 202, Version 5/02.
A Robust Data Structure
CMSC 202 Trees.
Lecture 9: Self Balancing Trees
Chapter 20: Binary Trees.
Mark Redekopp David Kempe
Heaps By JJ Shepherd.
Sorted Binary Trees.
B-Trees.
Non-Linear data structures
Binary Tree Implementation And Applications
Heapsort.
Data Structures – Binary Tree
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Trees Part 2!!! By JJ Shepherd

Binary Search Tree A tree structure where each node has a comparable key If a node’s value is larger than its parent’s it goes to the right subtree If a node’s value is smaller or equal to its parent’s value it goes in the left subtree Each node has at most two children

Binary Search Tree Can be represented also by an array

Binary Search Tree Each node is an index in the array [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Binary Search Tree To go to left child the formula is leftChildIndex = index*2+1 To go to the right child the formula is rightChildIndex = index*2+2 Each level has 2depth amount of nodes

Binary Search Tree BUT WHERE IS 11 AND 12? They are children of node at spot 5 that is null, so they are null too. No need to waste space. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Example!

Insertion A value is inserted into a tree based on the tree’s definition Smaller values go to the left Larger or equal values go to the right A value traverses the tree following these rules until a null child value is found

Insertion Inserting a 5 into this tree [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Insertion Inserting a 5 is less than 8 go left [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Insertion Inserting a 5 is greater than 3 go right [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Insertion Inserting a 5 is less than 6 go left [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Insertion Inserting a 5 is less greater than 4 go right [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Insertion The right child is null so insert the new node there [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] 5 [20] index 1 2 3 4 5 6 7 8 9 10 11 12 … 20 - 14 13 value

Example!

Searching Searching works much like binary search, hence the name (Derp) The tree is traversed until either the value is found or it hits a null reference

Example!

Traversals How to travel and access the values in a tree Good way to print out values of tree, so it’s great for debugging Three types Pre-order In-order Post-order

Traversals Pre-order requires accessing each of the values of the node then traversing the left subtree and then the right subtree (Left side) Pre-order would be 8 3 1 6 4 7 10 14 13

Traversals In-order requires traversing each left subtree, then accessing the values, then traversing each right subtree (Underneath) Pre-order would be 1 3 4 6 7 8 10 13 14

Traversals Post-order requires traversing each left subtree, then traversing each right subtree, then accessing the values (Right Side) Pre-order would be 1 4 7 6 3 13 14 10 8

Traversals Breadth First visits ever node on the same levels before going lower For Arrays just print it out in index order

Example!

Deletion… AGAIN!!!

Deletion Basic idea is first find if the value is in the tree If it is then there are three cases If it has no children, then simply remove it If it has one child, then replace the removed node with that child If it has two children then Find the smallest value in the right subtree and replace the value with that Recursively delete that value from the right subtree This can also be alternated with the left subtree to avoid unbalanced trees

Deletion Deleting 3 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Deletion 3 is found! [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Deletion Find the smallest value in the right subtree It will be the left most value of that tree [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Deletion It’s a 4 whoa! [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Deletion Replace 3 with 4 [0] [1] 4 [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Deletion Delete 4 but… The process has to be repeated [0] [1] 4 [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Deletion Luckily 4 has no children and can be wiped off the face of the earth [0] [1] 4 [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Deletion If that node had more children to the right they need to be shifted [0] [1] 4 [2] [3] [4] [5] [6] [7] [8] [9] [10] [13] [14] index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - value

Example!

Summary BS Trees are neat but they do have some issues They can become unbalanced and thus inefficient Self balancing trees fix this BS Trees can also be represented by arrays Can take up a lot of memory