Basic Data Structures - Trees

Slides:



Advertisements
Similar presentations
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
Advertisements

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.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
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.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
(2,4) Trees1 What are they? –They are search Trees (but not binary search trees) –They are also known as 2-4, trees.
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
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]
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
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.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Binary Search Trees Chapter 7 Objectives
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
AA Trees.
File Organization and Processing Week 3
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Search Trees.
BST Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Binary Search Trees.
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Binary Search Trees < > = Binary Search Trees
ITEC 2620M Introduction to Data Structures
Binary Tree and General Tree
Binary Search Trees.
Representation of a Threaded Tree
Wednesday, April 18, 2018 Announcements… For Today…
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > = Binary Search Trees
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
Binary Search Trees < > =
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
m-Way Search Trees A m-way Search tree of degree ‘m’ can have
Sorted Binary Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Balanced search trees: trees.
Presentation transcript:

Basic Data Structures - Trees Informal: a tree is a structure that looks like a real tree (up-side-down) Formal: a tree is a connected graph with no cycles.

Trees A tree T is a set of nodes storing values in a parent-child relationship with following properties: T has a special node called root. Each node different from root has a parent node. When there is no node, T is an empty tree.

Trees Every node must have its value(s) Non-leaf node has subtree(s) size=8 subtree root value height=3 leaf Every node must have its value(s) Non-leaf node has subtree(s) Non-root node has a single parent node

Binary Trees Multi-way Trees (order k) Each node can have at most 2 sub-trees Multi-way Trees (order k) ------------------------------------------------------- Each node can have at most k sub-trees

Binary Search Trees A binary search tree is a tree satisfying the following properties: It is a binary tree. For every node with a value N, all values in its left sub-tree must less than or equal to N, and all values in its right sub-tree must be greater than or equal to N.

This is NOT a binary search tree

Searching in a binary search tree search( s, t ) { If(s==t’s value) return t; If(t is leaf) return null If(s < t’s value) search(s,t’s left tree) else search(s,t’s right tree)} Time per level O(1) O(1) h Total O(h)

Insertion in a binary search tree examples 6 11 6 11 6 6 11 always insert to a leaf Time complexity O(height_of_tree) ? O(log n) if it is balanced n = size of the tree

Insertion in a binary search tree insertInOrder(s, t) { if(t is an empty tree) // insert here return a new tree node with value s else if( s < t’s value) t.left = insertInOrder(s, t.left) else t.right = insertInOrder(s, t.right) return t }

Comparison – Insertion in an ordered list insertInOrder(s, list) { loop1: search from beginning of list, look for an item >= s loop2: shift remaining list to its right, start from the end of list insert s } Insert 6 6 6 6 6 2 3 4 5 6 7 8 9 Time complexity? O(n) n = size of the list

Deleting an item from a list deleteItem(s, list) { loop1: search from beginning of list, look for an item == s loop2: shift remaining list to its left } delete 6 6 6 6 6 2 3 4 5 6 7 8 9 Time complexity? O(n) n = size of the list

Removal in a binary search tree case 1 – deleted item is in a leaf 4 Easy! 4 6 Time complexity O(height_of_tree)

Removal in a binary search tree case 2 – deleted item is NOT in a leaf 6

Removal in a binary search tree case 2 – deleted item is NOT in a leaf Where is the value next to 5? delete 5 ? right subtree ! 6 It is in the left most node of “5” s right sub tree. leftmost

Removal in a binary search tree case 2 – deleted item is NOT in a leaf move 6 up Time complexity O(height_of_tree)

Removal in a binary search tree deleteItem(s,t) { x = searchItem(s,t) if(x == null) return // nothing to remove if(x is a leaf) remove node x else if(x.right != null) y = findLeftmost(x.right) else if(x.left != null) y = findRightmost(x.left) move value in y to x, then remove node y }