cs1501 presentations l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CS1501 Presentations PowerPoint Presentation
Download Presentation
CS1501 Presentations

Loading in 2 Seconds...

play fullscreen
1 / 23

CS1501 Presentations - PowerPoint PPT Presentation


  • 719 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS1501 Presentations' - omer


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs1501 presentations

CS1501 Presentations

Binary Search Trees

trees
Trees
  • A tree is an abstract data structure consisting of nodes and pointers.
rooted trees
Rooted Trees
  • A rooted tree is a tree with one node designated as the root node.
binary trees
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 trees5
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
Doubly Linked Lists

HEAD

TAIL

Dnode definesa Record

Data isoftype Whatever

Prev isoftype Ptr toa Dnode

Next isoftype Ptr toa Dnode

endrecord // Dnode

binary trees7
Binary Trees

ROOT

  • Keep the head pointer
  • Change the node slightly…
    • still has data
    • still has two pointers
    • but it looks different!
binary trees8
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

structure for binary trees

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 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
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
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 traversal13
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
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
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 traversal16
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
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
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 traversal19
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
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

algorithm to insert a node into a bst

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
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
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