Skip to content

Commit 0a34cf7

Browse files
committed
Commit tests for IsCompleteTree()
1 parent a723b0d commit 0a34cf7

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

binarytree_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,134 @@ func TestIsBalancedTree(t *testing.T) {
483483
}
484484
}
485485

486+
func TestIsCompleteTree(t *testing.T) {
487+
// A complete binary tree
488+
//
489+
// 1
490+
// / \
491+
// 2 3
492+
//
493+
root := binarytree.NewNode(1)
494+
root.InsertLeft(2)
495+
root.InsertRight(3)
496+
497+
if !root.IsCompleteTree() {
498+
t.Fatal("tree should be complete")
499+
}
500+
501+
// A complete binary tree
502+
//
503+
// 1
504+
// / \
505+
// 2 3
506+
// /
507+
// 4
508+
//
509+
root = binarytree.NewNode(1)
510+
root.InsertRight(3)
511+
two := root.InsertLeft(2)
512+
two.InsertLeft(4)
513+
514+
if !root.IsCompleteTree() {
515+
t.Fatal("tree should be complete")
516+
}
517+
518+
// A complete binary tree
519+
//
520+
// __1__
521+
// / \
522+
// 2 3
523+
// / \ /
524+
// 4 5 6
525+
//
526+
root = binarytree.NewNode(1)
527+
two = root.InsertLeft(2)
528+
two.InsertLeft(4)
529+
two.InsertRight(5)
530+
three := root.InsertRight(3)
531+
three.InsertLeft(6)
532+
533+
if !root.IsCompleteTree() {
534+
t.Fatal("tree should be complete")
535+
}
536+
537+
// Not complete binary tree
538+
//
539+
// __1_
540+
// / \
541+
// 2 3
542+
// / \ \
543+
// 4 5 6
544+
//
545+
root = binarytree.NewNode(1)
546+
two = root.InsertLeft(2)
547+
two.InsertLeft(4)
548+
two.InsertRight(5)
549+
three = root.InsertRight(3)
550+
three.InsertRight(6)
551+
552+
if root.IsCompleteTree() {
553+
t.Fatal("tree should not be complete")
554+
}
555+
556+
// Not complete binary tree
557+
//
558+
// 1
559+
// /
560+
// 2
561+
// /
562+
// 3
563+
//
564+
root = binarytree.NewNode(1)
565+
two = root.InsertLeft(2)
566+
two.InsertLeft(3)
567+
568+
if root.IsCompleteTree() {
569+
t.Fatal("tree should not be complete")
570+
}
571+
572+
// Not complete binary tree
573+
//
574+
// __1__
575+
// / \
576+
// 2 3
577+
// \ / \
578+
// 4 5 6
579+
root = binarytree.NewNode(1)
580+
two = root.InsertLeft(2)
581+
two.InsertRight(4)
582+
three = root.InsertRight(3)
583+
three.InsertLeft(5)
584+
three.InsertRight(6)
585+
586+
if root.IsCompleteTree() {
587+
t.Fatal("tree should not be complete")
588+
}
589+
590+
// Not complete binary tree
591+
//
592+
// 1__
593+
// / \
594+
// 2 3
595+
// / \
596+
// 4 5
597+
root = binarytree.NewNode(1)
598+
root.InsertLeft(2)
599+
three = root.InsertRight(3)
600+
three.InsertLeft(4)
601+
three.InsertRight(5)
602+
603+
if root.IsCompleteTree() {
604+
t.Fatal("tree should not be complete")
605+
}
606+
607+
// A complete binary tree with a single root node
608+
root = binarytree.NewNode(1)
609+
if !root.IsCompleteTree() {
610+
t.Fatal("tree should be complete")
611+
}
612+
}
613+
486614
func TestNodeAttributes(t *testing.T) {
487615
root := binarytree.NewNode(1)
488616

0 commit comments

Comments
 (0)