Download presentation
Presentation is loading. Please wait.
Published byHartanti Halim Modified over 5 years ago
1
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
Philip Bernhard, PhD Spring 2018
2
Binary Search Trees Recall that in a Binary Search Tree (BST), the data in a given node is greater than the data in all children on the left side and is less than the data of all children on the right side.
3
Binary Search Tree Operations (Review)
Given a BST, how do each of the following operations work? search insertion deletion
4
BST Operations Search for 3, 12, 15 Insert 4, 7, 9, 13, 21
Delete 3, 12, 6,….10? (notice how delete can get complicated!) 10 6 14 3 8 12 20
5
BST Operations Search for 3, 12, 15 Insert 4, 7, 9, 11, 19
Delete 3, 12, 6,….10? 14 10 20 8 12 6 3
6
BST - Operations Starting with an empty BST, insert the following values in the specified order: 9, 7, 13, 2, 8, 11, 25 Now do the same, but in the following order: 13, 25, 9, 11, 8, 7, 2 Lastly, try the following order: 2, 7, 8, 9, 11, 13, 25
7
Balanced vs. Unbalance BSTs
Notice how insertion order determines the structure of a BST. In the first case, the resulting BST is said to be balanced. In the second case, the resulting BST is somewhat balanced, and in the third case, the resulting BST is not balanced at all. The BST in the third case is said to be degenerate.
8
Balanced vs. Unbalance BSTs
How balanced a BST is affects searching. Searching a balanced BST takes logarithmic time. Searching a degenerate BST takes linear time. Logarithmic time good, linear time bad…
9
Balanced vs. Unbalance BSTs
Analysis of algorithms will be discussed in CSE 2010. Also note that there are more sophisticated insertion and re-balancing algorithms that keep a BST balanced, thereby improving search times.
10
Link-based Binary Trees
Recall the typedef introduced to implement a pointer-based Binary tree: typedef struct treeNode { int data; struct treeNode *left; struct treeNode *right; } treeNode;
11
Recursive Print - Linked Tree
Also recall the recursive print procedure: void printTree(treeNode *tempPtr) { if(tempPtr != NULL) { printTree(tempPtr->left); // follow left first printf("\n%d", tempPtr->data); // print data in node printTree(tempPtr->right); // follow right last } return;
12
BST Operations What would be output from the recursive print function for the following binary tree? 10 6 14 3 8 12 20
13
In-Order Traversal The function performs what is called an in-order traversal. Starting at the root of any binary tree, an in-order traversal: performs an in-order traversal of it’s left sub-tree. “visits” the root performs an in-order traversal of it’s right sub-tree. In the current context, “visit” simply means output, but more generally could mean anything.
14
Pre-Order Traversal Similarly, a pre-order traversal will work as follows: Starting at the root of any binary tree, a pre-order traversal: “visits” the root performs a pre-order traversal of it’s left sub-tree. performs a pre-order traversal of it’s right sub-tree. What would a pre-order traversal of the previous binary tree output? And how would the code be modified for print?
15
Post-Order Traversal Similarly, a post-order traversal will work as follows: Starting at the root of any binary tree, a post-order traversal: performs a pre-order traversal of it’s left sub-tree. performs a pre-order traversal of it’s right sub-tree. “visits” the root What would a post-order traversal of the previous binary tree output? And how would the code be modified for print?
16
Array-Based BST A complete program implementing all three traversals using an array-based BST. #include <stdio.h> int tree[1000]; void initializeTree() { int i; for (i=0; i<1000; i++) tree[i] = -1; }
17
Array-Based BST void addNode(int index, int newData) { if(tree[index] == -1) // -1 == NULL tree[index] = newData; else { if(newData < tree[index]) addNode((index*2)+1, newData); else addNode((index*2)+2, newData); } return;
18
Array-Based BST void preOrderPrintTree(int index) { if(tree[index] != -1) { printf(" %d ",tree[index]); preOrderPrintTree((index*2)+1); preOrderPrintTree((index*2)+2); } return; void inOrderPrintTree(int index) { inOrderPrintTree((index*2)+1); inOrderPrintTree((index*2)+2);
19
Array-Based BST void postOrderPrintTree(int index) { if(tree[index] != -1) { postOrderPrintTree((index*2)+1); postOrderPrintTree((index*2)+2); printf(" %d ",tree[index]); } return;
20
Array-Based BST void main() { int num, status; initializeTree(); printf("Please enter an integer to be inserted "); printf("(q to quit): "); status = scanf("%ld", &num); while (status == 1) { addNode(0,num); printf("Please enter next integer (q to quit): "); } printf(“\npreorder:\n"); preOrderPrintTree(0); printf(“\ninOrder:\n"); inOrderPrintTree(0); printf(“\npostOrder:\n"); postOrderPrintTree(0); printf("\n");
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.