github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/tree/tree_test.go (about) 1 package tree 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func generateBinaryTree() *BinaryTree { 9 bt := CreateBinaryTree() 10 bt.Root = &Node{Item: 1} 11 bt.Root.Left = &Node{Item: 12} 12 bt.Root.Right = &Node{Item: 9} 13 bt.Root.Left.Left = &Node{Item: 5} 14 bt.Root.Left.Right = &Node{Item: 6} 15 return bt 16 } 17 18 func TestBinaryTreePostOrder(t *testing.T) { 19 bt := generateBinaryTree() 20 PostOrder(bt.Root) 21 } 22 23 func TestBinaryTreeInOrder(t *testing.T) { 24 bt := generateBinaryTree() 25 InOrder(bt.Root) 26 } 27 28 func TestBinaryTreePreOrder(t *testing.T) { 29 bt := generateBinaryTree() 30 PreOrder(bt.Root) 31 } 32 33 func TestFullBinaryTreeValidation(t *testing.T) { 34 fbt := CreateBinaryTree() 35 fbt.Root = CreateNodeWithItem(1) 36 fbt.Root.Left = CreateNodeWithItem(2) 37 fbt.Root.Right = CreateNodeWithItem(3) 38 fbt.Root.Left.Left = CreateNodeWithItem(4) 39 fbt.Root.Left.Right = CreateNodeWithItem(5) 40 fbt.Root.Right.Left = CreateNodeWithItem(6) 41 fbt.Root.Right.Right = CreateNodeWithItem(7) 42 43 fmt.Println(IsFullBinaryTree(fbt)) 44 } 45 46 func TestIsHeightBalanced(t *testing.T) { 47 bt := CreateBinaryTree() 48 bt.Root = CreateNodeWithItem(1) 49 bt.Root.Left = CreateNodeWithItem(2) 50 bt.Root.Right = CreateNodeWithItem(3) 51 bt.Root.Left.Left = CreateNodeWithItem(4) 52 bt.Root.Left.Right = CreateNodeWithItem(5) 53 bt.Root.Left.Left.Left = CreateNodeWithItem(6) 54 fmt.Println(IsHeightBalanced(bt.Root, 0)) 55 } 56 57 func TestAVLTree(t *testing.T) { 58 avl := CreateBinaryTree() 59 avl.Root = AVLInsertNode(avl.Root, 33) 60 avl.Root = AVLInsertNode(avl.Root, 13) 61 avl.Root = AVLInsertNode(avl.Root, 53) 62 avl.Root = AVLInsertNode(avl.Root, 9) 63 avl.Root = AVLInsertNode(avl.Root, 21) 64 avl.Root = AVLInsertNode(avl.Root, 61) 65 avl.Root = AVLInsertNode(avl.Root, 8) 66 avl.Root = AVLInsertNode(avl.Root, 11) 67 68 PreOrder(avl.Root) 69 fmt.Println() 70 avl.Root = AVLDeleteNode(avl.Root, 13) 71 PreOrder(avl.Root) 72 fmt.Println() 73 } 74 75 func TestAVLTree2(t *testing.T) { 76 avl := CreateBinaryTree() 77 avl.Root = AVLInsertNode(avl.Root, 33) 78 avl.Root = AVLInsertNode(avl.Root, 21) 79 avl.Root = AVLInsertNode(avl.Root, 53) 80 avl.Root = AVLInsertNode(avl.Root, 8) 81 avl.Root = AVLInsertNode(avl.Root, 29) 82 avl.Root = AVLInsertNode(avl.Root, 49) 83 avl.Root = AVLInsertNode(avl.Root, 61) 84 avl.Root = AVLInsertNode(avl.Root, 27) 85 avl.Root = AVLInsertNode(avl.Root, 30) 86 avl.Root = AVLInsertNode(avl.Root, 40) 87 avl.Root = AVLInsertNode(avl.Root, 5) 88 avl.Root = AVLInsertNode(avl.Root, 23) 89 90 PreOrder(avl.Root) 91 fmt.Println() 92 avl.Root = AVLDeleteNode(avl.Root, 5) 93 PreOrder(avl.Root) 94 fmt.Println() 95 } 96 97 func TestBTree(t *testing.T) { 98 btree := NewBTree(2) 99 btree.Insert(8) 100 btree.Insert(9) 101 btree.Insert(10) 102 btree.Insert(11) 103 btree.Insert(15) 104 btree.Insert(20) 105 btree.Insert(17) 106 107 btree.Show() 108 109 if btree.Contain(12) { 110 fmt.Println("found") 111 } else { 112 fmt.Println("not found") 113 } 114 } 115 116 func TestRemoveKey(t *testing.T) { 117 btree := NewBTree(3) 118 btree.Insert(1) 119 btree.Insert(3) 120 btree.Insert(7) 121 btree.Insert(10) 122 btree.Insert(11) 123 btree.Insert(13) 124 btree.Insert(14) 125 btree.Insert(15) 126 btree.Insert(18) 127 btree.Insert(16) 128 btree.Insert(19) 129 btree.Insert(24) 130 btree.Insert(25) 131 btree.Insert(26) 132 btree.Insert(21) 133 btree.Insert(4) 134 btree.Insert(5) 135 btree.Insert(20) 136 btree.Insert(22) 137 btree.Insert(2) 138 btree.Insert(17) 139 btree.Insert(12) 140 btree.Insert(6) 141 142 btree.PreorderShow() 143 144 if btree.Contain(12) { 145 fmt.Println("found") 146 } else { 147 fmt.Println("not found") 148 } 149 150 fmt.Printf("\nremove 6\n") 151 btree.RemoveKey(6) 152 btree.PreorderShow() 153 154 fmt.Printf("\nremove 13\n") 155 btree.RemoveKey(13) 156 btree.PreorderShow() 157 }