Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Binary Trees Binary Search Trees

2

3  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

4  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

5  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

6  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”

7  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

8 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; } }

9 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; } }

10  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

11 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; } } }

12  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

13  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

14 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; } }

15 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);

16  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

17  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

18  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…

19  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.

20  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 2 1024  Ideally, 10 nodes would be traversed!

21  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

22  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

23 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

24  Inserting “62” as a new node

25  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

26  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)

27  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

28  Example of 3 cases

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


Download ppt "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."

Similar presentations


Ads by Google