COMP 53 – Week Fourteen Trees.

Slides:



Advertisements
Similar presentations
Chapter 4: Trees Part II - AVL Tree
Advertisements

Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Starting at Binary Trees
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.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary 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.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
CSE332: Data Abstractions Lecture 7: AVL Trees
Non Linear Data Structure
Trees Chapter 15.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
BST Trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees Lecture 12 CS2110 – Fall 2017.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Complete Binary Trees Chapter 10 introduces trees.
Week 11 - Friday CS221.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
COMP 103 Binary Search Trees.
AVL Tree 27th Mar 2007.
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Tree Rotations and AVL Trees
A Robust Data Structure
Binary Search Trees.
CSE 332: Data Abstractions AVL Trees
Binary Trees, Binary Search Trees
Trees.
CS202 - Fundamental Structures of Computer Science II
CSC 143 Java Applications of Trees.
CSC 143 Binary Search Trees.
Chapter 20: Binary Trees.
AVL Tree Chapter 6 (cont’).
CO4301 – Advanced Games Development Week 4 Binary Search Trees
การวิเคราะห์และออกแบบขั้นตอนวิธี
1 Lecture 13 CS2013.
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Complete Binary Trees Chapter 9 introduces trees.
CS202 - Fundamental Structures of Computer Science II
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

COMP 53 – Week Fourteen Trees

Data Structures Data Structures Linear Direct Access Array Hashtable Sequential Access List Stack Queue Nonlinear Hierarchical Tree Heap

Topics Project Five Preview Tree Overview Tree Traversal CRUD Operations

Why Do We Care Need to Know Trees! On the final VERY Common data structure in programs Foundational concept of data organization

Trees Introduction Trees are complex data structures Recall linked list: nodes have only one pointer  next node Trees have two, & sometimes more, pointers to other nodes Trees are normally created in sorted order Trees may need to be rebalanced  expensive operation

Binary Tree Diagram Trees consist of subtrees  naturally recursive Note that the data elements are sorted with smaller items to the left Storage Rules: All values in left sub-tree < root All values in right sub-tree ≥ root Same is true of roots of each sub-tree

Binary Tree Data Structure

Tree Properties Notice paths Notice here each node has two links From top to any node No "cycles" – follow pointers, will reach "end" Notice here each node has two links Called binary tree Most common type of tree Root node Similar to linked list’s head Leaf nodes Both link variables are NULL (no subtrees)

Tree Processing Options Preorder Processing: Process data in root node Process left subtree Process right subtree In-order Processing: Process data in root Postorder Processing: Binary Search Tree: Traversals: Inorder  values "in order" Preorder  "prefix" notation Postorder  "postfix" notation Natural Data Structure for Recursion

Balanced Tree There are the same number of leaves on each side of the root Recursive definition – the left/right root has same number of leaves on each of its side. Optimal for searching  O(log n) processing

Unbalanced Binary Tree Natural sort process will keep inserting to the right Worst case search is O(n) – same as linked list

Re-Balancing is Required Go through a process of pivot and rotate at each node

AVL Balancing Algorithm Insertion Calculate a balance factor for each node = height of left subtree – height of right subtree A tree is considered balanced when -1 < height < 1 Work your way down to tree to find where imbalance starts Taken from - http://en.wikipedia.org/wiki/AVL_tree (Adelson-Velskii and Landis' tree, named after the inventors) Then pivot node to become new root

Complete Binary Trees First node remains the root Second node is always to the left And third is always to the right The next node must be the right child of the root.

Adding More Rows to Complete Tree Next row starts at the left most leaf ...and the right child of Colorado. Where would the next node after this go?... By the way, there is also an interesting story about Idaho. Apparently congress decreed that the border between Idaho and Montana was to be the continental divide. So the surveyers started at Yellowstone Park, mapping out the border, but at “Lost Trail Pass” they made a mistake and took a false divide. They continued along the false divide for several hundred miles until they crossed over a ridge and saw the Clark Fork River cutting right across what they thought was the divide. At this point, they just gave up and took the rest of the border straight north to Canada. So those of you who come from Kalispell and Missoula Montana should really be Idahonians. And are added left to right

Final Result for Complete Tree Then the right child of Florida.

So What is so Great About Complete Tree Minimizes space – only nodes allocated for required data Minimizes the depth of the tree Max tree height <= log N in size Data is stored in order of addition to tree (root is oldest) Lower right is newest

Is This Complete? Just to check your understanding, is this binary tree a complete binary tree? No, it isn't since the nodes of the bottom level have not been added from left to right.

Is This Complete? Is this complete? No, since the third level was not completed before starting the fourth level.

Is This Complete? This is also not complete since Washington has a right child but no left child.

Are These Complete? But this binary tree is complete. It has only one node, the root. The right hand side represents an empty tree. It is also considered complete.

Topics Tree Overview Tree Traversal CRUD Operations

Printing a BST What sort of traversal needed? Goal is to display the tree with sorted values. 34 19 50 1 21 46 87

Destructor What order traversal do we need? 34 Big O? 19 50 1 21 46 87

Tree Practice (1 of 3) Define a TreeNode Class with properties T data; TreeNode *lChild; TreeNode *rChild; Add the following methods Default and General constructors Destructor Print() method

Insertion into a BST Interactive Tool 58 34 Big O? 19 50 1 21 46 87

Tree Practice (2 of 3) Add the insertion method Input parameter is an item to be added (data=item) Recursively traverse the tree based upon the item value if item < data If left tree is null, add a new node Else go to left tree pointer If item > data, If right tree is null, add a new node Else go to right tree pointer End of Lecture 1

Searching a BST What traversal is recommended? 34 Big O? 19 50 1 21 46 87

Tree Practice (3 of 3) Add the search method Returns true if item is found Recursively search the tree based upon sorted value If item < data recursively call the search with the left node If item > data recursively call the search with the right node Last statement should be return false

Deletion from a BST Cases to Consider? Item not in Tree / Tree Empty Item is Root: Node with 0 Children Node with 1 Child Node with 2 Children Item is not Root:

Deleting Node with 0 Children 21 34 19 50 1 21 46 87

Deleting Node with 1 Child 50 34 19 50 1 21 46

Deleting Node with 1 Child 50 50 34 19 46 1 21

Deleting Node with 2 Children Which nodes could replace root? 34 34 19 50 1 21 46 87

Which nodes could replace root? Deletion from a BST Which nodes could replace root? 34 34 19 50 1 21 46 87

Which nodes could replace root? Deletion from a BST Which nodes could replace root? 34 46 19 50 1 21 87

Non-Binary Trees

Coding the Delete Practice If there is time!

Generative Grammars

Key Takeaways Trees are most efficient way to search for data Balanced trees are most efficient storage model Power of recursion makes tree management easy Trees can be beautiful!