Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Trees Types and Operations
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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
Tree.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
ICOM 4035 – Data Structures Lecture 13 – Trees ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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.
Tree Data Structures.
Starting at Binary Trees
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
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.
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.
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.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
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.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
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.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Tonga Institute of Higher Education
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
Binary Trees.
Trees 7/14/2009.
Trees Part 2!!! By JJ Shepherd.
Binary Trees.
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

Trees By JJ Shepherd

Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort – Quick Sort Let’s look at a data structure that uses a similar idea

Trees Definition: A data structure that can be defined recursively as a collection of nodes, where each node is a data structure consisting of a value, together with a list of references to nodes, with the constraints that no reference is duplicated, and none points to the root.

Trees The big idea is a tree is a structure that has no self references pointers, no disjoint nodes, and no cycles YESNO

Trees Some definitions. Most of them work much like a family tree. Root – The top node in a tree. Child – A node’s reference which is at a lower level Parent – The converse notion of child. Siblings – Nodes with the same parent. Leaf – a node with no children. Degree – number of sub trees of a node. Edge – connection between one node to another. Path – a sequence of nodes and edges connecting a node with a descendant. Level – The level of a node is defined by 1 + the number of connections between the node and the root. Height of tree –The height of a tree is the number of edges on the longest downward path between the root and a leaf. Height of node –The height of a node is the number of edges on the longest downward path between that node and a leaf. Depth –The depth of a node is the number of edges from the node to the tree's root node.

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

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

Insertion Inserting a 5 is less than 8 go left

Insertion Inserting a 5 is greater than 3 go right

Insertion Inserting a 5 is less than 6 go left

Insertion Inserting a 5 is less greater than 4 go right

Insertion The right child is null so insert the new node there 5

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

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

Traversals In-order requires traversing each left subtree, then accessing the values, then traversing each right subtree (Underneath) Pre-order would be

Traversals Post-order requires traversing each left subtree, then traversing each right subtree, then accessing the values (Right Side) Pre-order would be

Deletion… Ugh…

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

Deletion 3 is found!

Deletion Find the smallest value in the right subtree It will be the left most value of that tree

Deletion It’s a 4 whoa!

Deletion Replace 3 with 4 4

Deletion Delete 4 but… The process has to be repeated 4

Deletion Luckily 4 has no children and can be wiped off the face of the earth 4

Summary Trees are amazing Just the best Wow BS Trees are neat but they do have some issues – They can become unbalanced and thus ineffceint – Self balancing trees fix this