Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 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,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
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.
Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
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.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
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.
Starting at Binary Trees
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
IS 2610: Data Structures Recursion, Divide and conquer Dynamic programming, Feb 2, 2004.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
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.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 8 (Lafore’s Book) Binary Tree Hwajung Lee.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Chapter 5 : Trees.
MCS680: Foundations Of Computer Science
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Trees.
CE 221 Data Structures and Algorithms
Binary Trees, Binary Search Trees
Trees.
Trees.
Binary Trees, Binary Search Trees
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EE PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING It is a repetitive process in which an algorithm calls itself. Why recursion? It provides a simple mechanism to perform iterative process. It provides much simpler coding. RECURSION

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING RECURSION FUNDAMENTAL The are two commonly used statements in data structure analysis: Proof by Induction Proof by Contradiction/Counter Example

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PROOF BY INDUCTION Prove that the Fibonacci numbers, F 0 =1, F 1 =1, F 2 =2, F 3 =3, F 4 = 5, …. F i = F i-1 + F i-2, and satisfy F i < (5/3) i Proof of induction starts with the simple trivial case to establish the base case. Then, assuming that the theorem is true for the k th case, based on the given conditions, to prove that it is also true for the k+1 th case. If it is true for the case k+1, by the principle of induction, the theorem will be true for any number if n is finite.

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PROOF BY INDUCTION Prove that the Fibonacci numbers, F 0 =1, F 1 =1, F 2 =2, F 3 =3, F 4 = 5, …. F i = F i-1 + F i-2, and satisfy F i < (5/3) i It is quite obvious that F 1 = 1 < 5/3, F 2 = 2 < 25/9. We need to show that F k+1 < (5/3) k+1 Since F k+1 = F k + F k-1, then F k+1 < (5/2) k + (5/2) k-1 (5/2) k + (5/2) k-1 = (3/5)(5/3) k+1 + (3/5) 2 (5/3) k+1 = (3/5 + 9/25) (5/3) k+1 = (24/25) (5/3) k+1 < (5/3) k+1 Thus, F k+1 < (5/3) k+1

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Example: Factorial Factorial (n) = [ 1 n x (n-1) x ….. 2 x 1 if n =0 if n >0 Iterative algorithm Factorial (n) = [ 1 n x (Factorial (n-1)) if n =0 if n >0 Recursive algorithm

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING i= 1; factN =1; loop ( i< n) factN = factN *i; i = i +1; return factN;

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING recursiveFactorial( val n ) if ( n = 0 ) return 1; else return ( n* recursiveFactorial (n-1));

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING RECURSION Most mathematical functions are described by a simple formula. However, some are in more complicated forms. Define a function, F, valid on positive integers, that satisfies F(0) = 0, and F(x) = 2F(x-1) + x 2 From definition, we have: F(1) = 1, F(2) = 6, F(3) = 21, and F(4) = 58. Here, we have a function defines on itself. We call it recursive function. The idea is to implement the recursive function by computer program. Then, how ?

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING RECURSION int F(int x) { /* 1 */ if (x = = 0) return 0; /* 2 */else return 2*F(x-1) + x*x; } In line 1, it is similar to induction case that establishes the base case. It is the case that solved without recursion. The value for which the function is directly known without resorting to recursion. Simply declare the function, F(x) = 2F(x-1) +x 2 without the base case is ambiguous mathematically. line 2 makes the recursion call. (function calls itself)

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING RECURSION int F(int x) { /* 1 */ if (x = = 0) return 0; /* 2 */else return 2*F(x-1) + x*x; } What will happen if the function is called to evaluate F(-1)?

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING RECURSION int Bad(unsigned int N) { /* 1 */ if (N = = 0) return 0; /* 2 */else return Bad (N/3 +1) + N -1; } if Bad(1) is called, then line 2 will be executed as it is defined by line 2. But what is the value Bad(1)? It is not defined in the base case. Then, the computer will keep on executing line 2 until the system runs out of space. In fact, the program does not work for any number except Bad(0).

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Characteristics of Recursion if this is a simple case solve it else redefine the problem using recursion

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING RECURSION For any valid recursion, the fundamental rules are: 1. Base case. It must include some base cases, which can be solved without recursion. 2. Making progress. For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case. In general, every recursive call must either solve a part of the problem or reduce the size of the problem.

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING recursiveFactorial( val n ) if ( n = 0 ) return 1; else return ( n* recursiveFactorial (n-1)); Base Case: if ( n = 0) return 1; Making Progress recursiveFactorial(n-1) *for each call, the argument is towards the base case, n= 0

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Selection Sort

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Selection Sort Find largest element in the array, switch it with the bottom element. Repeat the same action until the whole array is sorted. Algorithm if n is 1 the array is sorted else place the largest array element in the last position Sort the subarray which excludes the last array element

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Algorithm if n is 1 the array is sorted else place the largest array element in the last position Sort the subarray which excludes the last array element void select_sort(int array[], int n) { if (n ==1) return; else { place_largest(array, n); select_sort(array, n-1); } }

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Recursion Development 1. Base case. It must include some base cases, which can be solved without recursion. * Termination of the recursion. 2. Making progress. For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case. * Dividing the problem into sub-problem with “smaller scale”.

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING TREE ADT

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Basic Tree Concepts A tree consists of a finite set of elements called node, and a finite set of directed lines, called branches, that connect the nodes. branch node

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING branch 1 node for node B, branch 1 is an indegree branch. indegree branch is a branch directed towards a node for node A, branch 1 is an outdegree branch. outdegree branch is a branch directed away from a node root

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING A leaf is any node with an outdegree of zero. (C, D, E, G, H, I) Nodes are not the root or leaves, called internal nodes. (B, F) root

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING A node is a parent if its has child/or successor. Any node with a predecessor is a child. Two or more nodes with the same parent are siblings. {(C,D), (G,H, I)} parent parent and child

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Ancestor- is any node in the path from the root to the node (A, B, F) Descendent - is any node in the path below the parent node (B, E, F, C, D, G, H, I) ancestor descendent

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING The level of a node is its distance from the root. The height of the tree is the level of the leaf in the longest path from the root plus 1. By definition, the height of an empty tree is -1. level 0 level 1 level 2

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE It is a tree in which no node can have more than two subtrees. These subtrees are designated as the left subtree, and right subtree.

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PROPERTIES OF BINARY TREE Height of Binary Tree Given that there are N nodes in a tree. The H max. is N The H min is [log 2 N] + 1. Given a height of the binary tree, H, the min. and max. no. of nodes in the tree are: N min = H, and N max = 2 H -1

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PROPERTIES OF BINARY TREE Balance Factor The balance factor of a binary tree is the difference in height between its left and right subtrees. B = H L -H R

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING STUCTURE OF BINARY TREE NODE left subtree data rightSubtree End NODE typedef struct node *NodePtr; struct node { int info; NodePtr left; NodePtr right; };

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE TRAVERSALS A binary tree traversal requires that each node of the tree be processed There are three way of traversals for a binary tree: preorder, inorder, and postorder In the preorder traversal, the root node is processed first, followed by the left subtree and the the right subtree. The root goes before the subtree.

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE PREORDER TRAVERSALS A B C D E F

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE TRAVERSALS algorithm preorder (val root ) if (root is not NULL) process(root); preorder(root-> LeftSubtree); preorder(root-> RightSubtree); return end preorder

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE INORDER TRAVERSALS Inorder traversal processes the left subtree first, the the rootm and finally the right subtree. C B D A E F

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE TRAVERSALS algorithm inorder (val root ) if (root is not NULL) inorder(root-> LeftSubtree); process(root); inorder(root-> RightSubtree); return end inorder

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE POSTORDER TRAVERSALS Postorder traversal processes the leftmost leaf then followed by the right subtrees and finally the root C D B F E A

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE TRAVERSALS algorithm postorder (val root ) if (root is not NULL) postorder(root-> LeftSubtree); postorder(root-> RightSubtree); process(root); return end postorder

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EXPRESSION BINARY TREE An expression tree is a binary tree with the following properties: 1. Each leaf is an operand 2. The root and the internal nodes are operators ( + - * / ) 3. Subtrees are sub-expressions with the root being an operator.

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EXPRESSION BINARY TREE a*(b+c) + d An infix tree with parenthesis ((a*(b+c)) + d)

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PRINTING AN INFIX EXPRESSION BINARY TREE algorithm infix (val tree ) if (tree not empty) if (tree->token is an operand) print (tree->token); else { print (open parenthesis); infix(tree->left); print(tree->token); infix(tree->right); print (close parenthesis);} return; end infix; ((a*(b+c)) + d)

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PRINTING AN PREFIX EXPRESSION BINARY TREE algorithm prefix (val tree ) if (tree not empty) { print (tree->token); prefix(tree->LeftPointer); prefix(tree->RightPointer); } return; end prefix; +*a+bcd

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PRINTING AN POSTFIX EXPRESSION BINARY TREE algorithm postfix (val tree ) if (tree not empty) { postfix(tree->LeftPointer); postfix(tree->RightPointer); print (tree->token); } return; end postfix; abc+*d+

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING CREATING AN EXPRESSION TREE Consider the expression :(a+b)*(c*(d+e)) The corresponding postfix is:ab+cde+**

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING CREATING AN EXPRESSION TREE ab+cde+** algorithm create_tree { do until the end of the expression; {read one value from the expression; if it is an operand { create a one node tree; push it to the stack; } else if it is an operator { pop two elements from the stack; create a tree with the operator as the root; create a right leaf with the first element; create a left leaf with the second element; push the root to the stack;} }}

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE A binary search tree is a binary tree with the following properties: 1. All items in the left subtree are less than the root 2. All items in the right subtree are greater than or equal to the root 3. Each subtree is itself a binary search tree.

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE valid bstinvalid bst

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE preorder : inorder: postorder:

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE inorder: Note: The inorder traversal of a binary search tree produces an ordered list.

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING OPERATIONS ON BINARY SEARCH TREE The common operations on BST are: find min. find max. find the requested data Find minimum is obvious that the leftmost node is the least among all the nodes of the tree. algorithm fmin (val root ) { if (root->left ==NULL) return (root); return fmin(root->left); }

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING OPERATIONS ON BINARY SEARCH TREE The common operations on BST are: find min. find max. find the requested data Find maximum is obvious that the rightmost node is the largest among all the nodes of the tree. algorithm fmax (val root ) { if (root->right ==NULL) return (root); return fmax(root->right); }

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH Search for the letter L

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH Search for 20. Starts from the root < 23 goes to left tree 18 is the root 20 > 18 goes to right tree

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH algorithm searchBST (val root, val arg ) { if (root is NULL) return NULL; if (arg key) return searchBST (root->left, arg); else if (arg > root->key) return searchBST (root->right, arg); else return root; } end searchBST;

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH insert 19 into the tree. root < 23, goes to left root > 18, goes to right root < 20, goes to left since left is null, then add at that point

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH insert 38 into the tree. root > 23 goes to right root < 44 goes to left root > 35 goes to right since right is null, thus insert at that point

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH algorithm insert (ref root, val new ) { if (root==NULL) { root=new; root->left = NULL; root->right=NULL;} else if (new->key key) insert (root->left, new); else insert(root->right, new); return; } end insert;