Implement a Binary Tree

A binary search tree can be useful when you have to traverse a lot of data in sorted order. As this CBinarySearchTree class demonstrates, you can implement binary search trees easily using objects and recursion (both data recursion and procedural recursion):

 'class properties:Private LeftBranch As CBinarySearchTreePrivate RightBranch As CBinarySearchTreePrivate NodeData As String'Adds a new value to the binary treePublic Sub AddNode(NewData As String)	If Len(NodeData) = 0 Then		'Store data in current node if empty		NodeData = NewData	ElseIf NewData < NodeData Then		'Store data in left branch if NewData < NodeData		If LeftBranch Is Nothing Then			Set LeftBranch = New CBinarySearchTree		End If		LeftBranch.AddNode NewData	Else		'Store data in right branch if NewData 		'>= NodeData		If RightBranch Is Nothing Then			Set RightBranch = New CBinarySearchTree		End If		RightBranch.AddNode NewData	End IfEnd Sub'Displays all values in this tree'If called on a child node, displays all 'values in this branchPublic Sub TraverseTree()	'Traverse left branch	If Not LeftBranch Is Nothing Then		LeftBranch.TraverseTree	End If	'Display this node	MsgBox NodeData	'Traverse right branch	If Not RightBranch Is Nothing Then		RightBranch.TraverseTree	End IfEnd Sub

Test this class by creating a new CBinarySearchTree object, calling AddNode a few times to store data, and then calling TraverseTree to see the results. Binary search trees don’t get much simpler than this.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS