230 likes | 1.04k Views
CS1501 Presentations Binary Search Trees Trees A tree is an abstract data structure consisting of nodes and pointers. Rooted Trees A rooted tree is a tree with one node designated as the root node. Binary Trees
E N D
CS1501 Presentations Binary Search Trees
Trees • A tree is an abstract data structure consisting of nodes and pointers.
Rooted Trees • A rooted tree is a tree with one node designated as the root node.
Binary Trees • A binary tree adds the constraint that each node may have 0, 1 or a maximum of 2 “children” • Nodes pointed to by a node are called that node’s children • Nodes with no children are called leaves.
Binary Trees • Another way of looking at a binary tree is a root node with a left subtree and a right subtree. left right
Doubly Linked Lists HEAD TAIL Dnode definesa Record Data isoftype Whatever Prev isoftype Ptr toa Dnode Next isoftype Ptr toa Dnode endrecord // Dnode
Binary Trees ROOT • Keep the head pointer • Change the node slightly… • still has data • still has two pointers • but it looks different!
Binary Trees ROOT Tnode definesa Record Data isoftype Whatever Left isoftype Ptr toa Tnode Right isoftype Ptr toa Tnode endrecord // Tnode Now we can arrange these nodes to represent a binary tree
Tree_Node definesa Record End Record Structure for Binary Trees data isoftype <Some DataType> left_child isoftype Ptr toa Tree_Node right_child isoftype Ptr toa Tree_Node Root isoftype Ptr toa Tree_Node Root
Binary Search Trees (BSTs) Binary Search Tree Head Pointer to a Tree Node Root • For each node, the value stored in it is: • Greater than the value in every node in the left subtree • Less than the value in every node in the right subtree 34 25 45 21 29 52 41
Tree Traversal Binary Search Tree Head Pointer to a Tree Node Root • The nodes of a tree can be visited in different orders to produce different results • Inorder • Preorder • Postorder 34 25 45 21 29 52 41
Inorder Traversal • Traverse the tree “In order”: • Visit the tree’s left subtree • Visit the current • Visit right subtree Procedure In_Order( t_ptr isoftype ptr toa Tree_Node ) if ( t_ptr <> NIL ) then In_Order( t_ptr^.left_child ) Do_whatever( t_ptr^.data ) In_Order( t_ptr^.right_child) endif endprocedure
Inorder Traversal Example: In Order Print Procedure InOrderPrint ( current iot in Ptr toa Tree_Node ) If ( current <> NIL ) then InOrderPrint ( current^.left ) Print ( current^.data ) InOrderPrint ( current^.right ) endif endprocedure
Inorder Traversal - Print In Order Current = Done Done InOrderPrint ( current^.left ) 34 Print ( current^.data ) InOrderPrint ( current^.right ) InOrderPrint ( current^.left ) InOrderPrint ( current^.left ) 25 45 Print ( current^.data ) Print ( current^.data ) InOrderPrint ( current^.right ) InOrderPrint ( current^.right ) Current = NIL InOrderPrint ( current^.left ) InOrderPrint ( current^.left ) InOrderPrint ( current^.left ) 21 29 52 Print ( current^.data ) Print ( current^.data ) Print ( current^.data ) InOrderPrint ( current^.right ) InOrderPrint (current^.right) InOrderPrint (current^.right) Current = NIL Current = NIL Current = NIL Current = NIL Current = NIL Current = NIL Output: 29 21 45 52 25 34
Preorder Traversal • Traverse the tree “Preorder”: • Visit the current • Visit the tree’s left subtree • Visit right subtree Procedure Pre_Order( t_ptr isoftype ptr toa Tree_Node ) if ( t_ptr <> NIL ) then Do_whatever( t_ptr^.data ) Pre_Order( t_ptr^.left_child ) Pre_Order( t_ptr^.right_child) endif endprocedure
Preorder Traversal Example: PreOrder Print Procedure PreOrderPrint ( current iot in Ptr toa Tree_Node) If ( current <> NIL ) then Print ( current^.data ) PreOrderPrint ( current^.left ) PreOrderPrint ( current^.right ) endif endprocedure
Preorder Traversal – PreOrder Print Current = Done Done Print ( current^.data ) 34 PreOrderPrint ( current^.left ) PreOrderPrint ( current^.right ) Print ( current^.data ) Print ( current^.data ) 25 45 PreOrderPrint ( current^.left ) PreOrderPrint (current^.left) PreOrderPrint ( current^.right ) PreOrderPrint (current^.right) Current = NIL Print ( current^.data ) Print ( current^.data ) Print ( current^.data ) 21 29 52 PreOrderPrint (current^.left) PreOrderPrint ( current^.left ) PreOrderPrint (current^.left) PreOrderPrint (current^.right) PreOrderPrint (current^.right) PreOrderPrint (current^.right) Current = NIL Current = NIL Current = NIL Current = NIL Current = NIL Current = NIL Output: 29 45 52 34 21 25
Postorder Traversal • Traverse the tree “Postorder”: • Visit the tree’s left subtree • Visit right subtree • Visit the current Procedure Post_Order(t_ptr isoftype ptr toa Tree_Node) if ( t_ptr <> NIL ) then Post_Order( t_ptr^.left_child ) Post_Order( t_ptr^.right_child) Do_whatever( t_ptr^.data ) endif endprocedure
Postorder Traversal Example: PostOrder Print Procedure PostOrderPrt ( current iot in Ptr toa Tree_Node ) If ( current <> NIL ) then PostOrderPrint ( current^.left ) PostOrderPrint ( current^.right ) Print ( current^.data ) endif endprocedure
Postorder Traversal–PostOrder Print Current = Done Done PostOrderPrt ( current^.left ) 34 PostOrderPrt ( current^.right ) Print ( current^.data ) PostOrderPrt ( current^.left ) PostOrderPrt (current^.left) 25 45 PostOrderPrt ( current^.right ) PostOrderPrt (current^.right) Print ( current^.data ) Print ( current^.data ) Current = NIL PostOrderPrt ( current^.left ) PostOrderPrt (current^.left) PostOrderPrt (current^.left) 21 29 52 PostOrderPrt (current^.right) PostOrderPrt (current^.right) PostOrderPrt (current^.right) Print ( current^.data ) Print ( current^.data ) Print ( current^.data ) Current = NIL Current = NIL Current = NIL Current = NIL Current = NIL Current = NIL Output: 29 52 45 21 34 25
84 L R 11 L R 28 L R 94 L R 76 L R 15 L R 84 L R 97 L R 21 L R 65 L R 57 L R Algorithm to Insert a Node into a BST • Algorithm Shell Tree_Node definesa record data isoftype Num left,right isoftype Ptr toa Tree_Node endrecord • Define the Record Algorithm Insert_in_a_BST endalgorithm • Declare the data • Load the Tree • Insert a new node root isoftype Ptr toa Tree_Node // Load the tree Insert_in_BST( root, 73 ) root
Writing a Recursive Module to Insert a Node • Procedure template current isoftype in Ptr toa Tree_Node, • First argument value isoftype in Num ) • Second argument Procedure Insert_in_BST ( <argument>, <argument> ) // Purpose: insert a node in a BST // Preconditions: BST // Postconditions: BST with new node // unchanged if node already there endprocedure // Insert_in_BST • IF template • Terminating condition • Insert a new node • Do we go left or right? • Take a step closer if( <terminating> ) then else endif current = NIL • Make a recursive call current <- new( Tree_Node ) current^.data <- value current^.left <- NIL current^.right <- NIL current current if(current^.data > value) then elseif(current^.data < value) then endif Insert_in_BST( , value ) current^.left data data left right right Insert_in_BST( , value ) current^.right
Web Slides • The previous power point slides are available on-line for review • http://www.cc.gatech.edu/classes/cs1501/ Developed by Cs1501 Presentations Guy Ettinger, Yisrael Lowenstein, & Bill Leahy