Binary Trees Binary Search Trees.  Not included in the.NET Framework  Data stored in a non-linear fashion  BST imposes rules on how the data in the.

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

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
BST Data Structure A BST node contains: A BST contains
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Marc Smith and Jim Ten Eyck
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
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.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Tree.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS 61B Data Structures and Programming Methodology July 15, 2008 David Sun.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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 Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
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]
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
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.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Data Structure and Algorithms
Binary Trees and Binary Search Trees
Binary Search Tree (BST)
Trees Another Abstract Data Type (ADT)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Chapter 17 Object-Oriented Data Structures
Binary Trees, Binary Search Trees
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.
Trees Another Abstract Data Type (ADT)
Trees Another Abstract Data Type (ADT)
Lecture 12 CS203 1.
Binary Trees, Binary Search Trees
Trees Chapter 10.
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary Trees Binary Search Trees

 Not included in the.NET Framework  Data stored in a non-linear fashion  BST imposes rules on how the data in the tree is arranged ◦ For searching purposes, results in a “sub-linear” search time

 Collection of nodes ◦ Node  Associated data  Set of children  Appear immediately below the node itself  Parent  The node immediately above the node itself  Root  Node which has no parent

 Properties ◦ Only one root ◦ All nodes (except root) have only one parent ◦ No cycles.  if you start somewhere, you cannot take a path leading back to where you started  Hierarchical representation of data

 Subset of “tree” structure ◦ All nodes have at most two children  Left & right ◦ A node that has no children  “leaf node” ◦ Nodes with one or two children  “internal nodes”

 Create a base class “Node” (general tree) ◦ Via inheritance, extend it to binary tree node ◦ Arbitrary number of children  NodeList class ◦ Node class  Data  NodeList instance (children)  Use Generics ◦ Decide at development time, which data type to use in the node

public class Node { // Private member-variables private T data; // contains the node data private NodeList neighbors = null; // node’s children public Node() { } public Node(T data) : this(data, null) {} public Node(T data, NodeList neighbors) { this.data = data; this.neighbors = neighbors; } public T Value { get {return data;} set {data = value;} } protected NodeList Neighbors { get {return neighbors;} set {neighbors = value; } }

public class NodeList : Collection > { public NodeList() : base() { } public NodeList(int initialSize) { // Add the specified number of items for (int i = 0; i < initialSize; i++) base.Items.Add(default(Node )); } public Node FindByValue(T value) { // search the list for the value foreach (Node node in Items) if (node.Value.Equals(value)) return node; // if we reached here, we didn't find a matching node return null; } }

 Node class is a “generic” class ◦ Meets the requirements of a tree, but not a binary tree ◦ We will extend the base Node class for specific needs  Binary tree node ◦ At most two children (left and right) ◦ Extended class must “expose” two properties that operate on base class’ Neighbors property  Left  Right

public class BinaryTreeNode : Node { public BinaryTreeNode() : base() {} public BinaryTreeNode(T data) : base(data, null) {} public BinaryTreeNode(T data, BinaryTreeNode left, BinaryTreeNode right) { base.Value = data; NodeList children = new NodeList (2); children[0] = left; children[1] = right; base.Neighbors = children; } public BinaryTreeNode Left { get { if (base.Neighbors == null) return null; else return (BinaryTreeNode ) base.Neighbors[0]; } set { if (base.Neighbors == null) base.Neighbors = new NodeList (2); base.Neighbors[0] = value; } } public BinaryTreeNode Right { get { if (base.Neighbors == null) return null; else return (BinaryTreeNode ) base.Neighbors[1]; } set { if (base.Neighbors == null) base.Neighbors = new NodeList (2); base.Neighbors[1] = value; } } }

 Right and Left properties make sure that Neighbors Nodelist is created ◦ If not, the “get” accessor returns a null value ◦ “set” accessor creates a new NodeList with two elements

 Now that we have a BinaryTreeNode ◦ Create a BinaryTree Class  Single private member variable  root ◦ Generics used  Type specified for the tree is type used for BinaryTreeNode root ◦ Public method, Clear()  Clears out the contents of the tree  Resets the root to null

public class BinaryTree { private BinaryTreeNode root; public BinaryTree() { root = null; } public virtual void Clear() { root = null; } public BinaryTreeNode Root { get { return root; } set { root = value; } }

BinaryTree btree = new BinaryTree (); btree.Root = new BinaryTreeNode (1); btree.Root.Left = new BinaryTreeNode (2); btree.Root.Right = new BinaryTreeNode (3); btree.Root.Left.Left = new BinaryTreeNode (4); btree.Root.Right.Right = new BinaryTreeNode (5); btree.Root.Left.Left.Right = new BinaryTreeNode (6); btree.Root.Right.Right.Right = new BinaryTreeNode (7); btree.Root.Right.Right.Right.Right = new BinaryTreeNode (8);

 Not contiguous in memory ◦ The BinaryTree class has a reference to root  BinaryTreeNode class has references to left and right ◦ BinaryTreeNode instances can be dispersed throughout CLR managed heap  Searching ◦ No direct access to a particular node ◦ Search can take linear time  Possibly all nodes need to be probed

 Designed to improve search efficiency of a binary tree ◦ For any node n  Each left child’s value is less than n’s value  Each right child’s value is greater than n’s value  Each node in a BST is a unique value ◦ (a) is NOT a BST  10’s child [8] is the right child ◦ (b) is a BST

 BST must determine if one node is less than, greater than or equal to another node ◦ If searching for “10”  Start the search at the root  The root is the only node reference in the BST  All others are referred to from other nodes  7 is LT 10  Check the Right child  11 is GT 10  Check the Left child ◦ If searching for “9”  It should be in the Left child node of 10  No such node, so it doesn’t exist…

 Algorithm for searching a BST ◦ Node n is what we’re searching for ◦ Node c is the one we compare it to (in the tree) 1.If c is a null reference, then exit the algorithm. n is not in the BST. 2.Compare c's value and n's value. 3.If the values are equal, then we found n. 4.If n's value is less than c's then n, if it exists, must be in the c's left subtree. Therefore, return to step 1, letting c be c's left child. 5.If n's value is greater than c's then n, if it exists, must be in the c's right subtree. Therefore, return to step 1, letting c be c's right child.

 Algorithm ◦ Ideally we reduce the node count by half for each successive search of a node  asymptotic running time of log 2 n  Searching 1000 elements would be log  Ideally, 10 nodes would be traversed!

 The search time for a BST depends on its topology ◦ How nodes are laid out with respect to one another  Left-hand BST ◦ Each stage reduces the search time in half  Right-hand BST (worst-case scenario) ◦ Search time is like an array’s, each stage reduces the search by one node

 Topology ◦ Depends on the order of node insertion ◦ Affects the running time for a search algorithm  Adding nodes to a BST ◦ Always insert the new node as a leaf node  Find the parent of this new leaf node…! ◦ Traverse the BST, starting with the root node  At each compare stage, keep track of the parent node  Compare if node to insert is GT, LT or EQ  GTcheck the right child next  LTcheck the left child next  EQdiscard the new node, or raise exception

1.If c is a null reference, then parent will be the parent of n. If n's value is less than parent's value, then n will be parent's new left child; otherwise n will be parent's new right child 2.Compare c and n's values 3.If c's value equals n's value, then the user is attempting to insert a duplicate node. Either simply discard the new node, or raise an exception. (Note that the nodes' values in a BST must be unique.) 4.If n's value is less than c's value, then n must end up in c's left subtree. Let parent equal c and c equal c's left child, and return to step 1 5.If n's value is greater than c's value, then n must end up in c's right subtree. Let parent equal c and c equal c's right child, and return to step 1  Algorithm ends when proper leaf is found ◦ Attaches the new child node of parent  Special case ◦ If BST done not have a root  parent will be null, addition of new node as a child of parent is bypassed

 Inserting “62” as a new node

 Scenario ◦ Insert order: 1,2,3,4,5,6  Approximates an array, with “1” as root, “2” as 1’s right child, “3” as 2’s right child, etc  Ideal order:  4,2,5,1,3,6  4 as the root node

 When deleting a node with children ◦ Some other node needs to be selected to “fill the hole”  Careful selection, so as not to violate the properties of a BST (left child is LT parent, right child is GT parent)

 Locate the node to delete  Identify a node to take its place ◦ 3 cases  Node being delete has no right child  Left child can be used as replacement  Deleted node’s right child has no left child  Then the deleted node’s right child can be replacement  Deleted node has a left child  Replace it with the right child’s left-most descendant  With the deleted node’s right subtree’s smallest value

 Example of 3 cases

 Preorder traversal  Inorder traversal  Postorder traversal  All start at root and visit that node and its children ◦ Difference between