Download presentation
Presentation is loading. Please wait.
Published byAlexandrina Wright Modified over 9 years ago
1
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Information Technology, 3’rd Semester Lecture 14: Trees Operations
2
Outline Implementation of binary Search tree. Add new Item. Traversing a Binary Search Tree Emank X Mezank !!
3
Implementation of BST 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. 3 Presented & Prepared by: Mahmoud R. Alfarra
4
Build the Basic Node 4 Presented & Prepared by: Mahmoud R. Alfarra Let’s look at the code for the Node class first: Implementation of BST
5
Build a Binary Search Tree 5 Presented & Prepared by: Mahmoud R. Alfarra 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. Implementation of BST
6
6 Presented & Prepared by: Mahmoud R. Alfarra 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. Build a Binary Search Tree Insert method
7
7 Presented & Prepared by: Mahmoud R. Alfarra 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. Build a Binary Search Tree Determining the proper position for a node
8
8 Presented & Prepared by: Mahmoud R. Alfarra 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 Build a Binary Search Tree Determining the proper position for a node
9
9 Presented & Prepared by: Mahmoud R. Alfarra 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. Build a Binary Search Tree Determining the proper position for a node
10
Terminology of Tree 10 Presented & Prepared by: Mahmoud R. Alfarra Insert method
11
Traversing a Binary Search Tree 11 Presented & Prepared by: Mahmoud R. Alfarra 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.
12
Traversing a Binary Search Tree 12 Presented & Prepared by: Mahmoud R. Alfarra Inorder Traversal Order 4 2 3 1 5 6 9 810 5 10 15 50 60 70 80
13
Traversing a Binary Search Tree 13 Presented & Prepared by: Mahmoud R. Alfarra Inorder Traversal Order 5 10 15 50 60 70 80
14
Traversing a Binary Search Tree 14 Presented & Prepared by: Mahmoud R. Alfarra 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. Preorder Traversal Order
15
Traversing a Binary Search Tree 15 Presented & Prepared by: Mahmoud R. Alfarra Preorder Traversal Order 50 10 5 15 70 60 80
16
Traversing a Binary Search Tree 16 Presented & Prepared by: Mahmoud R. Alfarra 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. Postorder Traversal Order
17
Traversing a Binary Search Tree 17 Presented & Prepared by: Mahmoud R. Alfarra Postorder Traversal Order 5 15 10 60 80 70 50
18
Emank X Mezank !! يقول النبي صلى الله عليه وسلم: كفى بالمرء كذبـًا أن يحدثَ بكل ما سمـع
19
Next Lecture Finding a Node and Minimum/Maximum Values in a Binary Search Tree
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.