[二叉树] 跳二叉树成二进制搜索树
如何做我很简单. 我们只需浏览到原始二进制树的元素保存到数组,最后通过将插入数组的值转换成树 1 渡轮二叉树结点. 这是 1 二叉查找树.
代码:
program caynhiphan; type MyArray = array[1..1000] of integer; {Mang lu lai cac phan tu cua cay} type typeKey = integer; {Kieu cua khoa} type nodeTree = ^node; {Dinh nghia 1 Node cay nhi phan} node = record key : typeKey; left, right : nodeTree; end; type Tree = nodeTree; {Dinh nghia cay nhi phan} var T, T1 : nodeTree; {Cac bien cay nhi phan} a : MyArray; n, i:integer; function TaoNode(key : typeKey): nodeTree; {Tao 1 node chua du lieu la khoa key} var P:nodeTree; begin new(P); P^.key := key; {Gan khoa} P^.left := nil; {cay con trai = rong} P^.right := nil; {cay con phai = rong} TaoNode := P; {tra lai Node} end; procedure TaoCayRong(var T: Tree); {Tao cay rong} begin T := nil; end; procedure NhapCayNhiPhan(var T : Tree); {Nhap cay nhi phan thong thuong} var key, keyLeft, keyRight : typeKey; begin if (T = nil) then {Neu T rong thi nhap vao goc} begin new(T); write('Nhap vao goc: '); readln(key); T^.key := key; end; new (T^.left); new (T^.right); write('Nhap con trai cua ', T^.key, ' : '); readln(keyLeft); {Nhap vao con trai cua Node} T^.left^.key := keyLeft; write('Nhap con phai cua ', T^.key, ' : '); readln(keyRight); {Nhap vao con phai cua Node} T^.right^.key := keyRight; if (T^.left^.key <> 0) then NhapCayNhiPhan(T^.left) {Neu nhap vao con trai la 0 thi coi nhu rong} else T^.left := nil; if (T^.right^.key <> 0) then NhapCayNhiPhan(T^.right) {Neu nhap vao con phai 0 thi coi nhu rong} else T^.right := nil; end; procedure DuyetTruoc (T : nodeTree); {Duyen cay theo thu tu truoc} begin if T <> nil then begin write(T^.key:5); DuyetTruoc(T^.left); DuyetTruoc(T^.right); end; end; procedure DuyetGiua(T: nodeTree); begin if T <> nil then begin DuyetGiua(T^.left); write(T^.key:5); DuyetGiua(T^.right); end; end; procedure ChuyenSangMang (T : nodeTree; var a : MyArray; var n : integer); {Chuyen cay nhi phan sang mang} begin if (T <> nil) then {Neu cay chua rong thi lam} begin inc(n); a[n] := T^.key; {Gan gia tri cua cay voa mang} ChuyenSangMang(T^.left, a, n); {Duyen sang con trai} ChuyenSangMang(T^.right, a, n); {Duyet sang con phai} end; end; procedure ThemNodeVaoCayNPTK(P: nodeTree; var T : Tree); {Them 1 Node vao cay nhi phan tim kiem} begin if T = nil then T := P {Neu cay rong thi gan bang P} else begin if P^.key > T^.key then ThemNodeVaoCayNPTK(P, T^.right) {Neu Node P > Node hien tai -> duyet phai} else if P^.key < T^.key then ThemNodeVaoCayNPTK(P, T^.left) {Neu Node P < Node hien tai -> duyet trai} else; end; end; procedure ChuyenSangCayNPTK(T : Tree; var T1 : Tree); {Chuyen cay nhi phan thuong sang cay nhi phan tim kiem} var P : nodeTree; i:integer; begin ChuyenSangMang(T, a, n); {Chuyen cay thuong sang mang} TaoCayRong(T1); {Tao cay T1 rong, T1 la cay nhi phan tim kiem can tao ra} for i:=1 to n do {Them lan luot cac phan tu cua mang vao cay T1} begin P := TaoNode(a[i]); {Tao Node} ThemNOdeVaoCayNPTK(P, T1); {Them Node vao cay} end; end; begin TaoCayRong(T); NhapCayNhiPhan(T); writeln('Duyet truoc cay nhi phan ban dau:'); DuyetTruoc(T); writeln(); ChuyenSangCayNPTK(T, T1); writeln('Duyet truoc cay nhi phan tim kiem:'); DuyetTruoc(T1); writeln(); writeln('Duyet giua cay nhi phan tim kiem: '); DuyetGiua(T1); writeln(); writeln('Done.'); readln; end.
在在Pascal代码中的C代码
什么语言是这一切的. 😀
có thể hướng dẫn mình phần tìm kiếm một nút có xuất hiện trong cây hay không đc k ạ
Ban có thể xem bài này nhé https://cachhoc.net/2013/10/20/cay-mot-so-phep-toan-tren-cay-nhi-phan-tim-kiem/