Tree data structure.

Slides:



Advertisements
Similar presentations
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Marc Smith and Jim Ten Eyck
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
CS261 Data Structures Trees Introduction and Applications.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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 Search Trees (BST)
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
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.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
Trees Chapter 15.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Multiway Search Trees Data may not fit into main memory
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Binary Search Tree Chapter 10.
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Trees and Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree data structure.
Search Sorted Array: Binary Search Linked List: Linear Search
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Design and Analysis of Algorithms
Binary Search Trees < > =
Data Structures and Algorithms
Binary Trees, Binary Search Trees
Trees.
Tree.
Data Structures and Algorithms
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Lecture 9: Intro to Trees
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Tree data structure

How should I decide which data structure to use? What needs to be stored? Cost of operations Memory usage Ease of implementation What do we use a tree for? Organizational Hierarchy

ROOT Height of x = no. of edges in longest path from x to a leaf For example: Height of 3 = 2 Height of root = 3 = height of tree

Binary Tree Definition: A tree in which each node can ROOT Definition: A tree in which each node can Have at most 2 children

Pointer Tree data Left Childs NULL Right Childs Struct node { Address of Right Child Address of Left Child Struct node { int data; struct node* left; struct node* right } data Applications: Storing naturally hierarchical data -> e.g. file system. Organize data for quick search, insertion, deletion. Network routing algorithm. AND MORE… Left Childs NULL Right Childs

Binary Tree Binary Tree, each node can have at most 2 children

Strict / Proper Binary Tree Each node can have either 2 or 0 children

Complete Binary Tree All levels except possibly the last are completely filled, and all nodes are as left as possible Maximum number of nodes at any level (i) is 2i

Perfect Binary Tree =2No. of levels-1 Maximum number of nodes Maximum number of nodes in a binary tree with height (h) = 20+21+…….+2h =2h+1-1 =2No. of levels-1 Maximum number of nodes = 24-1 = 15

Perfect Binary Tree A complete binary tree in which all levels are full Number of nodes will be maximum for a height

Binary Search Tree Height of a perfect binary tree with n nodes Now, if n = 15 then h = 𝑙𝑜𝑔 2 15+1 −1 0 = 𝑙𝑜𝑔 2 16 – 1 = 4 – 1 = 3 Height : number of edges in longest path from root to a leaf Height of an empty tree = -1 Height of a tree with 1 node = 0

We can implement binary tree using: Dynamically created nodes struct node { int data; struct node *left; Struct node *right; }; b) Arrays 0 1 2 3 4 5 6 0 root But how to store information about the links 1 2 For node at index c in a complete binary tree Left-child index = 2i+1 3 4 5 6 Right-childe index = 2i +2 2 4 6 8 2 4 1 5 8 7 9 2 4 1 5 8 7 9

Binary Search Tree What data structure will you use to store a modifiable collection of data? We want to be able to perform the following operations: search (x) //search for an item x insert(x) // insert an item x remove(x) // delete an item x Choices: array, linked list….

Array (unsorted) search(x): O(n) end insert(x): O(1) insert(7) remove(x): O(n) end remove(3) Linked list: search(x): O(n) insert(x): O(1) //at the head remove(x): O(n) head 1 3 5 7 1 5 7 3 5 7

We can perform binary search in a sorted array in O(logn) end Array (sorted array) Search(x) : O(logn) sorted array Insert(x): O(n) end Remove(x): O(n) insert(6) BST (Balanced)For n records, log n comparisons if 1 comparison = 10 −6 𝑠𝑒𝑐 Search(x): O(logn) n = 2 30 = 30 x 10 −6 𝑠𝑒𝑐 // 2 30 ≈ 1 billion Insert(x): O(logn) Remove(x): O(logn) 3 5 7 9 3 5 6 7 9

Binary Search Tree (BST) root A Binary Tree in which for each node, the value of all the nodes in left subtree is lesser or equal and the value of all the nodes in the right subtree is greater. BST BST root Right-Subtree (greater) Left-Subtree (lesser or equal) 15 9 19 root 7 11 16 24 BST?

So how do we achieve O(log n) in BST for search, insert, and remove? start end end new 7 9 11 15 16 19 24 Search(9) Search Space Reduction Process BST n n/2 log _2 n steps n/4 n/2^k = 1 . 1 2^k = n Search(11) k = log_2 n

root BST (unbalanced) root BST (unbalanced) 24 Search Space Reduction 19 root O(log n) (avg. case) 16 7 15 9 O(n) (worst case) 11 BST (unbalanced) 11 9 15 7 16 19 24

Insert (18) O(log n) Similarly delete() is going to take O(log n)