MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE DATA STRUCTURE Information Technology , 3’rd Semester Using C# Lecture 14: Trees Operations Presented By: Mahmoud Rafeek Alfarra Outline Implementation of binary Search tree. Add new Item. Traversing a Binary Search Tree Emank X Mezank !! Implementation of BST 3 A binary search tree is made up of nodes, so we need a Node class that is similar to the Node class we used in the linked list implementation. Presented & Prepared by: Mahmoud R. Alfarra Build the Basic Node Implementation of BST 4 Let’s look at the code for the Node class first: Presented & Prepared by: Mahmoud R. Alfarra Build a Binary Search Tree Implementation of BST 5 Next we’re ready to build a Binary Search Tree (BST) class. The class consists of just one data member—a Node object that represents the root node of the BST. Presented & Prepared by: Mahmoud R. Alfarra Build a Binary Search Tree Insert method 6 We next need an Insert method to add new nodes to our tree: 1. Create a Node object and assign the data the Node holds to the Data variable. 2. See If our BST has a root node. • If not, then this is a new BST and the node we are inserting is the root node. If this is the case, then the method is finished. • Otherwise, the method moves on to the next step. 3. Find the proper insertion point. Presented & Prepared by: Mahmoud R. Alfarra Build a Binary Search Tree Determining the proper position for a node 7 1. Set the parent node to be the current node, which is the root node. 2. If the data value in the new node is less than the data value in the current node, set the current node to be the left child of the current node. 3. If the data value in the new node is greater than the data value in the current node, skip to Step 4. If the value of the left child of the current node is null, insert the new node here and exit the loop. Otherwise, skip to the next iteration of the While loop. 4. Set the current node to the right child node of the current node. 5. If the value of the right child of the current node is null, insert the new node here and exit the loop. Otherwise, skip to the next iteration of the While loop. Presented & Prepared by: Mahmoud R. Alfarra Build a Binary Search Tree Determining the proper position for a node 8 1. Current = Root 2. Parent = current 3. If (new.data <current.data) Current = Current.left If left.data = null insert the new node here and exit the loop Else skip to the next iteration of the While loop. else Skip to step 4 Presented & Prepared by: Mahmoud R. Alfarra Build a Binary Search Tree Determining the proper position for a node 9 4. If (new.data <current.data) 4. Current = Current.right 5. If right.data = null 4. insert the new node here and exit the loop 6. Else 4. skip to the next iteration of the While loop. Presented & Prepared by: Mahmoud R. Alfarra Terminology of Tree 10 Insert method Presented & Prepared by: Mahmoud R. Alfarra Traversing a Binary Search Tree 11 We need to be able to traverse the BST so that we can visit the different nodes in several different orders: There are three traversal methods used with BSTs: 1. Inorder: visits all the nodes in a BST in ascending order of the node key values. 2. Preorder: visits the root node first, followed by the nodes in the subtrees under the left child of the root, followed by the nodes in the subtrees under the right child of the root. 3. Postorder. Presented & Prepared by: Mahmoud R. Alfarra Traversing a Binary Search Tree 12 1 5 2 4 3 6 8 10 9 Inorder Traversal Order Presented & Prepared by: Mahmoud R. Alfarra 5 10 15 50 60 70 80 Traversing a Binary Search Tree Inorder Traversal Order 13 5 10 15 50 60 70 80 Presented & Prepared by: Mahmoud R. Alfarra Traversing a Binary Search Tree Preorder Traversal Order 14 The only difference between the preOrder method and the inOrder method is where the three lines of code are placed. The call to the displayNode method was sandwiched between the two recursive calls in the inOrder method and it is the first line of the preOrder method. Presented & Prepared by: Mahmoud R. Alfarra Traversing a Binary Search Tree Preorder Traversal Order 15 50 10 5 15 70 60 80 Presented & Prepared by: Mahmoud R. Alfarra Traversing a Binary Search Tree Postorder Traversal Order 16 The difference between this method and the other two traversal methods is where the recursive calls and the call to displayNode are placed. In a postorder traversal, the method first recurses over the left subtrees and then over the right subtrees. Presented & Prepared by: Mahmoud R. Alfarra Traversing a Binary Search Tree Postorder Traversal Order 17 5 15 10 60 80 70 50 Presented & Prepared by: Mahmoud R. Alfarra !! Emank X Mezank يقول النيب صلى هللا عليه وسلم: كفى ابملرء كذبـًا أن حيدث بكل ما مسـع َ Next Lecture Finding a Node and Minimum/Maximum Values in a Binary Search Tree
© Copyright 2026 Paperzz