Non-Linear Structures

Slides:



Advertisements
Similar presentations
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.
Advertisements

Computer Science C++ High School Level By Guillermo Moreno.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 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.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Trees Dr. Andrew Wallace PhD BEng(hons) EurIng
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
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.
1 Tree Indexing (1) Linear index is poor for insertion/deletion. Tree index can efficiently support all desired operations: –Insert/delete –Multiple search.
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 Search Trees (BST)
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
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.
Lecture 23 Red Black Tree Chapter 10 of textbook
Data Structures and Design in Java © Rick Mercer
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
BST Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Trees.
Trees Lecture 12 CS2110 – Fall 2017.
Binary Search Tree Chapter 10.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Lecture 18. Basics and types of Trees
CSCS-200 Data Structures and Algorithms
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Data Structures Balanced Trees CSCI
Lecture 26 Multiway Search Trees Chapter 11 of textbook
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
singleRightRotation 
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Trees Lecture 9 CS2110 – Fall 2009.
B-Tree.
CMSC 202 Trees.
Computer Science and Engineering
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
short illustrative repetition
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Binary Search Trees.
AVL Tree Chapter 6 (cont’).
Binary Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Non-Linear Structures Trees

A Tree Structure - Each node may have multiple child nodes. President VP - Mktg VP - Oper VP - Sales CIO Sales Mgr. A Tree Structure - Each node may have multiple child nodes. Show parent/child relationship

data data data data data data data data data data data data The height of this tree is 4. Balanced Tree – The depth of all nodes differs by no more than one. Full Tree – Every node (except leaves) has two children. Complete Tree – All levels, except the last one, are full. data data data data

Building a Binary Search Tree Incoming values: 70, 23, 90, 47, 88, 52, 17, 99, 35, 28, 65, 55, 1, 41, 87 70 23 90 17 47 88 99 1 35 52 87 28 41 65 55

BTNode - data : Object - left : BTNode - right : BTNode + createBTNode() + getData() : Object + setData() : void + getLeft() : BTNode + setLeft() : void + getRight(): BTNode + setRight(): void

Generally, the procedure is the same as for adding a node to the tree. Follow the path until you run into a node with the value you're searching for. If you reach a null pointer, the value doesn't exist in the tree. Searching a BST

70 23 90 47 88 52 17 99 35 28 65 55 1 41 87 Searching a BST

Deleting a Node from a BST After deletion, the tree still must qualify as a BST. Three different cases: The node to be removed is a leaf. The node to be removed has one child. The node to be removed has two children. Deleting a Node from a BST

Deleting – Case 1 70 23 90 47 88 52 17 99 35 28 65 55 1 41 87 Delete node containing 1

Deleting – Case 2 70 23 90 17 47 88 99 35 52 87 Delete node containing 88. 28 41 65 55

Deleting – Case 3 70 23 90 17 47 87 99 35 52 Delete node containing 47 28 41 65 55

Deleting – Case 3 (Step 1) 70 23 90 17 41 87 99 35 52 28 47 65 55

Deleting – Case 3 (Step 2) 70 23 90 17 41 87 99 35 52 28 65 55

BST Deletion Algorithm Find the node with the value to be deleted (n1). If it's a leaf Delete the node. If it has one child Assign a reference to n1's child to n1's parent. If it has 2 children Find the node with the highest value in the left sub-branch (n2). Swap the values of n1 and n2. Call delete recursively to remove the original value. BST Deletion Algorithm

BST Methods insert – Inserts a node at the proper location. delete – Deletes a node based on a key. Returns true if the node deleted, and false if the key not found. find – Finds a node based on a key, and returns the data object found in the node. Returns null if the key is not found. clear – Empties the tree of all nodes. BST Methods