这篇文章是上一篇关于二叉树算法构建的文章的续篇。输出二叉树的基本操作。计算二叉树(这里用括号表示)。计算二叉树的深度。找到二叉树的宽度。输出二叉树//括号中的输出funcDispBTNode(root*BinaryTreeNode){ifroot!=nil{fmt.Printf("%v",root.data)ifroot.lChild!=nil||root.rChild!=nil{fmt.Print("(")DispBTNode(root.lChild)如果root.rChild!=nil{fmt.Print(",")}DispBTNode(root.rChild)fmt.Print(")")}}}求二叉树的高度funcBTHeight(root*BinaryTreeNode)int{ifroot==nil{return0}lHeight:=BTHeight(root.lChild)rHeight:=BTHeight(root.rChild)iflHeight>rHeight{returnlHeight+1}else{returnrHeight+1}}求二叉树的宽度funcBTWidth(root*BinaryTreeNode)int{varmax,width=0,0ifroot==nil{return0}treeQueue:=queue.ItemQueue{}treeQueue.New()treeQueue.Enqueue(root)for!treeQueue.IsEmpty(){width=treeQueue.Size()ifwidth>max{max=width}forindex:=0;索引<宽度;index++{node:=(treeQueue.Front()).((*BinaryTreeNode))treeQueue.Dequeue()ifnode.lChild!=nil{treeQueue.Enqueue(node.lChild)}ifnode.rChild!=nil{treeQueue.Enqueue(node.rChild)}}}returnmax}这里的ItemQueue来自golang-data-structureslookup二叉树中值为value的节点funcFindNode(root*BinaryTreeNode,valuestring)*BinaryTreeNode{ifroot==nil{returnnil}ifroot.data==value{returnroot}lResult:=FindNode(root.lChild,value)iflResult!=nil{returnlResult}returnFindNode(root.rChild,value)}二进制数树节点funcNodes(root*BinaryTreeNode)int{ifroot==nil{return0}lNum:=Nodes(root.lChild)rNum:=Nodes(root.rChild)return1+lNum+rNum}
