我的一位朋友在数据结构中询问了关于二叉搜索树部分的问题,但我没有任何解决方案。
考虑我们有一个二进制搜索树类,它具有insert和Sub settings()
' Goal of this function: to get user-desired settings and request permission to clear sheet
' could write these settings to text file and create profiles so that user can skip entering all this every time?
Dim cases As Variant
Dim title As String
title = "K-Map Program"
cases = InputBox("Enter number of inputs.", title)
If Not IsNumeric(cases) Then
Call notnum
End If
clearsheet = MsgBox("Permission to clear Sheet1?", vbYesNo + vbQuestion, title) ' could upgrade by giving user choice of which sheet to clear
If clearsheet = vbYes Then
MsgBox ("Clearing sheet1.")
Sheet1.Cells.Clear
ElseIf clearsheet = vbNo Then
MsgBox ("Sheet1 has not been cleared, program is ending.")
Exit Sub
Else
Call errormessage
End If
numforswitch = MsgBox("Do you want to label the inputs as numbers (1, 2, 3...)?", vbYesNo + vbQuestion, title)
' deposit the switches in columns in 0, 1 format
If numforswitch = vbYes Then
ifshift = MsgBox("Shift=0?", vbYesNo + vbQuestion, title)
If ifshift = vbYes Then
For counter = 1 To cases
Cells(1, counter).Value2 = counter ' will print 1, 2, 3... in the columns
Next
ElseIf shift = vbNo Then
shift = InputBox("What's shift?")
For counter = 1 To cases
Cells(1, counter).Value2 = counter + shift ' will print x, x+1...
Next
ElseIf numforswitch = vbNo Then
MsgBox ("Using letters for switches.")
' alphabet array
Const ALPHABET As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim i As Integer
For i = 1 To VBA.Len(ALPHABET)
Cells(1, i).Value = VBA.Mid(ALPHABET, i, 1)
Next i
Else
Call errormessage
End If
End Sub
方法。当我们向这个树添加新节点时,所有叶子应该彼此指向。当我们调用printlistleafs
方法程序时,应该从第一面到最后一面打印所有叶子节点。
看到这张照片来理解这个问题:
最后在这个结构中我们要添加新节点时所有指针都要更新。
请帮我用java编写这个程序。
答案 0 :(得分:0)
我假设你有一个根节点,你的BinaryTree的根和一个叶子列表。对于您添加的每个节点,您将继续插入。
对于每个插入,您需要执行两个操作:更新树并更新列表。
为了更新列表,只需要处理两种情况